Kode analisis sentimen LSTM-CNN ini menggunakan FastText sebagai vektor representasi kata. Ganti menjadi vektor representasi kata IndoBERT! 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
from sklearn.model_selection import StratifiedKFold

# Inisialisasi StratifiedKFold
n_splits = 5
skf = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=42)

# Konversi kategori target ke nilai numerik
y_categorical = data['Label'].astype('category').cat.codes

# Menyimpan akurasi dan report untuk setiap lipatan
accuracies = []
class_reports = []

iter_num = 1

histories = []

for train_index, test_index in skf.split(X, y_categorical):
    print("\nTraining and evaluating model for fold", iter_num)

    # Membagi data berdasarkan indeks dari lipatan saat ini
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    # Membangun model untuk lipatan saat ini
    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)
    
    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])
    histories.append(history)
    model.save('LSTM-CNN_Word2Vec_Tertinggi_LSTM200_CNN256_' + str(iter_num) + '.h5')
    
    # Evaluasi model pada lipatan saat ini
    y_pred = model.predict(X_test)
    y_pred_labels = np.argmax(y_pred, axis=1)
    y_test_labels = np.argmax(y_test, axis=1)

    # Menghitung akurasi
    accuracy = accuracy_score(y_test_labels, y_pred_labels)
    print("Fold", iter_num, "Accuracy: {:.2f}%".format(accuracy * 100))

    # Classification report
    class_report = classification_report(y_test_labels, y_pred_labels)

    # Menambahkan akurasi dan report ke daftar
    accuracies.append(accuracy)
    class_reports.append(class_report)

    iter_num += 1

# Hitung rata-rata akurasi dan cetak report klasifikasi untuk setiap lipatan
average_accuracy = np.mean(accuracies)
print("\nAverage accuracy across folds: {:.2f}%".format(average_accuracy * 100))

for i, report in enumerate(class_reports):
    print("\nClassification report for fold", i + 1)
    print(report)"