Tolong save/ekspor model analisis sentimen ke .h5 atau file ekstension lain yang bisa diimport di lain waktu! Kode:
import pickle
import pandas as pd
import numpy as np
from keras.preprocessing.text import Tokenizer
from keras.utils import pad_sequences
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense, Dropout, Conv1D, GlobalMaxPooling1D, Reshape
from keras.layers import Flatten
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import gensim
from keras.regularizers import l2 
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
# Membaca dataset
data_list = pickle.load(open('pre_processed_berita_121_joined_FIX_parpolheuristic_added.pkl', 'rb'))
data = pd.DataFrame(data_list, columns=['judul', 'isi', 'pre_processed', 'Label', 'Partai_Politik_Heuristic'])
data['Isi Berita'] = data['pre_processed']

# Tokenisasi dan Padding
max_words = 10000  # mengurangi jumlah kata maksimum
max_len =  500     # mengurangi panjang input
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(data['Isi Berita'])
sequences = tokenizer.texts_to_sequences(data['Isi Berita'])
X = pad_sequences(sequences, maxlen=max_len)
y = to_categorical(data['Label'].astype('category').cat.codes)

# Membagi data menjadi data latih dan data uji
X_train, X_test, y_train, y_test, idx_train, idx_test = train_test_split(X, y, data.index, test_size=0.25, random_state=42)

# Menggunakan pre-trained word2vec Bahasa Indonesia
path = 'idwiki_word2vec_300.model'
id_w2v = gensim.models.word2vec.Word2Vec.load(path)
wv = id_w2v.wv

# Membuat matriks embedding
embedding_dim = 300
embedding_matrix = np.zeros((max_words, embedding_dim))
for word, i in tokenizer.word_index.items():
    if i < max_words:
        try:
            embedding_vector = wv[word]
            embedding_matrix[i] = embedding_vector
        except KeyError:
            pass

# Membuat model LSTM-CNN
model = Sequential()
model.add(Embedding(max_words, embedding_dim, weights=[embedding_matrix], input_length=max_len, trainable=False))
model.add(LSTM(200, return_sequences=True, kernel_regularizer=l2(0.01), recurrent_regularizer=l2(0.01)))
model.add(Conv1D(256, 3, activation='relu', kernel_regularizer=l2(0.01)))
model.add(GlobalMaxPooling1D())
model.add(Dropout(0.1))
Reshape((-1, 256)),
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(32, activation='relu', kernel_regularizer=l2(0.01)))
model.add(Dropout(0.2))
model.add(Dense(3, activation='softmax'))

# Buat callback early stopping
callback_es = EarlyStopping(monitor='val_loss', patience=5, verbose=1, restore_best_weights=True)

# Inisialisasi optimizer dengan learning rate awal
initial_learning_rate = 0.0001
optimizer = Adam(learning_rate=initial_learning_rate)

# Kompilasi model
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])

# Latih model dengan menggunakan learning rate scheduler dan early stopping dalam callbacks
history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=50, batch_size=128, callbacks=[callback_es])
