Analisis sentimen data berita berbahasa indonesia berjumlah 16669 baris dengan kode CNN-LSTM berikut masih menghasilkan hasil yang jelek. Tolong perbaiki kode agar akurasi naik dan tidak stuck di angka akurasi 0.51 !
import numpy as np
import pandas as pd
import pickle
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from keras.preprocessing.text import Tokenizer
from keras.utils import pad_sequences
from keras.models import Sequential
from keras.layers import Embedding, Conv1D, MaxPooling1D, LSTM, Dense
from keras.callbacks import EarlyStopping, ModelCheckpoint
import gensim
from gensim.models import Word2Vec

# Membaca dataset
data_list = pickle.load(open('pre_processed_beritaFIX_joined.pkl', 'rb'))
data = pd.DataFrame(data_list, columns=['judul', 'isi', 'pre_processed', 'Label'])
data['Isi Berita'] = data['pre_processed']

# Konversi label ke angka
encoder = LabelEncoder()
data['label_encoded'] = encoder.fit_transform(data['Label'])

# Memisahkan data menjadi data latih dan data uji
X_train, X_test, y_train, y_test = train_test_split(data['Isi Berita'], data['label_encoded'], test_size=0.25, random_state=42)

# Tokenisasi teks dan padding
max_words = 10000
sequence_length = 500
tokenizer = Tokenizer(num_words=max_words)
tokenizer.fit_on_texts(data['Isi Berita'])
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
X_train_padded = pad_sequences(X_train_seq, maxlen=sequence_length, padding='post', truncating='post')
X_test_padded = pad_sequences(X_test_seq, maxlen=sequence_length, padding='post', truncating='post')

# Membuka model word2vec
path = 'idwiki_word2vec_300.model'
id_w2v = gensim.models.word2vec.Word2Vec.load(path)
wv = id_w2v.wv

# Membuat embeding matrix
embedding_dim = id_w2v.vector_size
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

# Membangun model CNN-LSTM dengan pre-trained Word2Vec
num_filters = 64
lstm_units = 64

model = Sequential([
    Embedding(input_dim=max_words,
              output_dim=embedding_dim,
              input_length=sequence_length,
              weights=[embedding_matrix],  # Menggunakan pre-trained Word2Vec
              trainable=False),
    Conv1D(num_filters, 5, activation='relu', kernel_regularizer=l2(l2_coeff)),
    MaxPooling1D(pool_size=4),
    LSTM(lstm_units, return_sequences=False, kernel_regularizer=l2(l2_coeff), recurrent_regularizer=l2(0.01)),  # Menambahkan regularisasi L2
    Dropout(0.5),  # Menambahkan Dropout
    Dense(3, activation='softmax', kernel_regularizer=l2(l2_coeff))  # Menambahkan regularisasi L2
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# Melatih model dengan early stopping dan model checkpoint
checkpoint = ModelCheckpoint('model_cnn_lstm.h5', save_best_only=True, monitor='val_accuracy', mode='max')
early_stopping = EarlyStopping(monitor='val_loss', patience=10, verbose=1)

history = model.fit(X_train_padded, y_train, validation_split=0.2, epochs=50, batch_size=128)

# Evaluasi model pada data uji
y_pred = np.argmax(model.predict(X_test_padded), axis=-1)

# Hitung akurasi
accuracy = accuracy_score(y_test, y_pred)
print('Akurasi: {:.2f}%'.format(accuracy * 100))

# Print classification report
print(classification_report(y_test, y_pred, target_names=encoder.classes_))

Hasilnya adalah sebagai berikut
Epoch 1/100
79/79 [==============================] - 18s 204ms/step - loss: 1.8177 - accuracy: 0.5304 - val_loss: 1.2715 - val_accuracy: 0.5186
Epoch 2/100
79/79 [==============================] - 16s 207ms/step - loss: 1.1284 - accuracy: 0.5312 - val_loss: 1.0541 - val_accuracy: 0.5174
Epoch 3/100
79/79 [==============================] - 16s 202ms/step - loss: 1.0284 - accuracy: 0.5320 - val_loss: 1.0240 - val_accuracy: 0.5174
Epoch 4/100
79/79 [==============================] - 16s 205ms/step - loss: 1.0137 - accuracy: 0.5317 - val_loss: 1.0142 - val_accuracy: 0.5174
Epoch 5/100
79/79 [==============================] - 16s 203ms/step - loss: 1.0067 - accuracy: 0.5339 - val_loss: 1.0117 - val_accuracy: 0.5174
Epoch 6/100
79/79 [==============================] - 16s 197ms/step - loss: 1.0029 - accuracy: 0.5343 - val_loss: 1.0111 - val_accuracy: 0.5174
Epoch 7/100
79/79 [==============================] - 16s 203ms/step - loss: 1.0015 - accuracy: 0.5351 - val_loss: 1.0083 - val_accuracy: 0.5186
Epoch 8/100
79/79 [==============================] - 16s 202ms/step - loss: 1.0003 - accuracy: 0.5348 - val_loss: 1.0079 - val_accuracy: 0.5182
Epoch 9/100
79/79 [==============================] - 16s 205ms/step - loss: 0.9982 - accuracy: 0.5351 - val_loss: 1.0071 - val_accuracy: 0.5174
Epoch 10/100
79/79 [==============================] - 16s 207ms/step - loss: 0.9970 - accuracy: 0.5358 - val_loss: 1.0071 - val_accuracy: 0.5174
Epoch 11/100
79/79 [==============================] - 16s 199ms/step - loss: 0.9974 - accuracy: 0.5350 - val_loss: 1.0068 - val_accuracy: 0.5186
Epoch 12/100
79/79 [==============================] - 16s 204ms/step - loss: 0.9966 - accuracy: 0.5360 - val_loss: 1.0059 - val_accuracy: 0.5178
Epoch 13/100
79/79 [==============================] - 16s 205ms/step - loss: 0.9950 - accuracy: 0.5362 - val_loss: 1.0069 - val_accuracy: 0.5174
Epoch 14/100
79/79 [==============================] - 16s 204ms/step - loss: 0.9932 - accuracy: 0.5366 - val_loss: 1.0071 - val_accuracy: 0.5182
Epoch 15/100
79/79 [==============================] - 16s 200ms/step - loss: 0.9945 - accuracy: 0.5364 - val_loss: 1.0048 - val_accuracy: 0.5182
Epoch 16/100
79/79 [==============================] - 16s 209ms/step - loss: 0.9936 - accuracy: 0.5366 - val_loss: 1.0054 - val_accuracy: 0.5186
Epoch 17/100
79/79 [==============================] - 16s 207ms/step - loss: 0.9929 - accuracy: 0.5366 - val_loss: 1.0052 - val_accuracy: 0.5182
Epoch 18/100
79/79 [==============================] - 16s 204ms/step - loss: 0.9932 - accuracy: 0.5366 - val_loss: 1.0052 - val_accuracy: 0.5178
Epoch 19/100
79/79 [==============================] - 16s 203ms/step - loss: 0.9933 - accuracy: 0.5367 - val_loss: 1.0069 - val_accuracy: 0.5170
Epoch 20/100
79/79 [==============================] - 16s 197ms/step - loss: 0.9925 - accuracy: 0.5368 - val_loss: 1.0049 - val_accuracy: 0.5186
Epoch 21/100
79/79 [==============================] - 16s 201ms/step - loss: 0.9921 - accuracy: 0.5369 - val_loss: 1.0054 - val_accuracy: 0.5178
Epoch 22/100
79/79 [==============================] - 16s 203ms/step - loss: 0.9934 - accuracy: 0.5365 - val_loss: 1.0084 - val_accuracy: 0.5166
Epoch 23/100
79/79 [==============================] - 16s 206ms/step - loss: 0.9934 - accuracy: 0.5367 - val_loss: 1.0055 - val_accuracy: 0.5174
Epoch 24/100
79/79 [==============================] - 16s 205ms/step - loss: 0.9905 - accuracy: 0.5370 - val_loss: 1.0048 - val_accuracy: 0.5178
Epoch 25/100
79/79 [==============================] - 16s 199ms/step - loss: 0.9929 - accuracy: 0.5369 - val_loss: 1.0046 - val_accuracy: 0.5174
Epoch 26/100
79/79 [==============================] - 16s 208ms/step - loss: 0.9920 - accuracy: 0.5371 - val_loss: 1.0052 - val_accuracy: 0.5166
Epoch 27/100
79/79 [==============================] - 17s 211ms/step - loss: 0.9909 - accuracy: 0.5372 - val_loss: 1.0054 - val_accuracy: 0.5178
Epoch 28/100
79/79 [==============================] - 17s 212ms/step - loss: 0.9912 - accuracy: 0.5371 - val_loss: 1.0042 - val_accuracy: 0.5178
Epoch 29/100
79/79 [==============================] - 17s 214ms/step - loss: 0.9906 - accuracy: 0.5369 - val_loss: 1.0049 - val_accuracy: 0.5174
Epoch 30/100
79/79 [==============================] - 16s 205ms/step - loss: 0.9901 - accuracy: 0.5371 - val_loss: 1.0048 - val_accuracy: 0.5174
Epoch 31/100
79/79 [==============================] - 16s 207ms/step - loss: 0.9910 - accuracy: 0.5373 - val_loss: 1.0053 - val_accuracy: 0.5182
Epoch 32/100
79/79 [==============================] - 16s 204ms/step - loss: 0.9909 - accuracy: 0.5371 - val_loss: 1.0047 - val_accuracy: 0.5178
Epoch 33/100
79/79 [==============================] - 16s 209ms/step - loss: 0.9900 - accuracy: 0.5369 - val_loss: 1.0076 - val_accuracy: 0.5170
Epoch 34/100
79/79 [==============================] - 17s 211ms/step - loss: 0.9904 - accuracy: 0.5373 - val_loss: 1.0060 - val_accuracy: 0.5182
Epoch 35/100
79/79 [==============================] - 14s 180ms/step - loss: 0.9904 - accuracy: 0.5371 - val_loss: 1.0042 - val_accuracy: 0.5170
Epoch 36/100
79/79 [==============================] - 12s 149ms/step - loss: 0.9891 - accuracy: 0.5374 - val_loss: 1.0052 - val_accuracy: 0.5174
Epoch 37/100
79/79 [==============================] - 12s 150ms/step - loss: 0.9900 - accuracy: 0.5376 - val_loss: 1.0069 - val_accuracy: 0.5170
Epoch 38/100
79/79 [==============================] - 15s 187ms/step - loss: 0.9886 - accuracy: 0.5380 - val_loss: 1.0042 - val_accuracy: 0.5178
Epoch 39/100
79/79 [==============================] - 12s 153ms/step - loss: 0.9907 - accuracy: 0.5378 - val_loss: 1.0074 - val_accuracy: 0.5166
Epoch 40/100
79/79 [==============================] - 12s 151ms/step - loss: 0.9898 - accuracy: 0.5375 - val_loss: 1.0048 - val_accuracy: 0.5170
Epoch 41/100
79/79 [==============================] - 12s 151ms/step - loss: 0.9912 - accuracy: 0.5375 - val_loss: 1.0044 - val_accuracy: 0.5174
Epoch 42/100
79/79 [==============================] - 15s 185ms/step - loss: 0.9901 - accuracy: 0.5374 - val_loss: 1.0055 - val_accuracy: 0.5166
Epoch 43/100
79/79 [==============================] - 14s 171ms/step - loss: 0.9908 - accuracy: 0.5375 - val_loss: 1.0049 - val_accuracy: 0.5170
Epoch 44/100
79/79 [==============================] - 12s 153ms/step - loss: 0.9899 - accuracy: 0.5381 - val_loss: 1.0058 - val_accuracy: 0.5170
Epoch 45/100
79/79 [==============================] - 12s 154ms/step - loss: 0.9895 - accuracy: 0.5378 - val_loss: 1.0064 - val_accuracy: 0.5166
Epoch 46/100
79/79 [==============================] - 14s 172ms/step - loss: 0.9887 - accuracy: 0.5380 - val_loss: 1.0066 - val_accuracy: 0.5170
Epoch 47/100
79/79 [==============================] - 18s 231ms/step - loss: 0.9885 - accuracy: 0.5381 - val_loss: 1.0047 - val_accuracy: 0.5170
Epoch 48/100
79/79 [==============================] - 18s 226ms/step - loss: 0.9894 - accuracy: 0.5379 - val_loss: 1.0080 - val_accuracy: 0.5170
Epoch 49/100
79/79 [==============================] - 18s 228ms/step - loss: 0.9902 - accuracy: 0.5380 - val_loss: 1.0074 - val_accuracy: 0.5166
Epoch 50/100
79/79 [==============================] - 18s 227ms/step - loss: 0.9889 - accuracy: 0.5381 - val_loss: 1.0051 - val_accuracy: 0.5174
Epoch 51/100
79/79 [==============================] - 18s 228ms/step - loss: 0.9895 - accuracy: 0.5381 - val_loss: 1.0052 - val_accuracy: 0.5174
Epoch 52/100
79/79 [==============================] - 18s 232ms/step - loss: 0.9882 - accuracy: 0.5381 - val_loss: 1.0058 - val_accuracy: 0.5166
Epoch 53/100
79/79 [==============================] - 19s 235ms/step - loss: 0.9883 - accuracy: 0.5382 - val_loss: 1.0048 - val_accuracy: 0.5166
Epoch 54/100
79/79 [==============================] - 18s 231ms/step - loss: 0.9901 - accuracy: 0.5380 - val_loss: 1.0048 - val_accuracy: 0.5174
Epoch 55/100
79/79 [==============================] - 18s 231ms/step - loss: 0.9901 - accuracy: 0.5381 - val_loss: 1.0070 - val_accuracy: 0.5174
Epoch 56/100
79/79 [==============================] - 18s 230ms/step - loss: 0.9879 - accuracy: 0.5381 - val_loss: 1.0071 - val_accuracy: 0.5162
Epoch 57/100
79/79 [==============================] - 18s 226ms/step - loss: 0.9895 - accuracy: 0.5385 - val_loss: 1.0060 - val_accuracy: 0.5178
Epoch 58/100
79/79 [==============================] - 18s 228ms/step - loss: 0.9886 - accuracy: 0.5383 - val_loss: 1.0066 - val_accuracy: 0.5158
Epoch 59/100
79/79 [==============================] - 18s 229ms/step - loss: 0.9884 - accuracy: 0.5385 - val_loss: 1.0079 - val_accuracy: 0.5166
Epoch 60/100
79/79 [==============================] - 18s 228ms/step - loss: 0.9896 - accuracy: 0.5384 - val_loss: 1.0069 - val_accuracy: 0.5162
Epoch 61/100
79/79 [==============================] - 18s 230ms/step - loss: 0.9900 - accuracy: 0.5387 - val_loss: 1.0066 - val_accuracy: 0.5174
Epoch 62/100
79/79 [==============================] - 18s 227ms/step - loss: 0.9886 - accuracy: 0.5384 - val_loss: 1.0102 - val_accuracy: 0.5170
Epoch 63/100
79/79 [==============================] - 12s 157ms/step - loss: 0.9890 - accuracy: 0.5387 - val_loss: 1.0068 - val_accuracy: 0.5170
Epoch 64/100
79/79 [==============================] - 12s 146ms/step - loss: 0.9880 - accuracy: 0.5386 - val_loss: 1.0063 - val_accuracy: 0.5170
Epoch 65/100
79/79 [==============================] - 12s 147ms/step - loss: 0.9891 - accuracy: 0.5383 - val_loss: 1.0084 - val_accuracy: 0.5174
Epoch 66/100
79/79 [==============================] - 12s 146ms/step - loss: 0.9885 - accuracy: 0.5389 - val_loss: 1.0085 - val_accuracy: 0.5190
Epoch 67/100
79/79 [==============================] - 12s 155ms/step - loss: 0.9891 - accuracy: 0.5394 - val_loss: 1.0193 - val_accuracy: 0.5198
Epoch 68/100
79/79 [==============================] - 12s 155ms/step - loss: 1.0157 - accuracy: 0.5293 - val_loss: 1.0112 - val_accuracy: 0.5170
Epoch 69/100
79/79 [==============================] - 12s 150ms/step - loss: 0.9965 - accuracy: 0.5374 - val_loss: 1.0070 - val_accuracy: 0.5166
Epoch 70/100
79/79 [==============================] - 12s 153ms/step - loss: 0.9922 - accuracy: 0.5382 - val_loss: 1.0055 - val_accuracy: 0.5174
Epoch 71/100
79/79 [==============================] - 12s 154ms/step - loss: 0.9914 - accuracy: 0.5385 - val_loss: 1.0052 - val_accuracy: 0.5190
Epoch 72/100
79/79 [==============================] - 12s 152ms/step - loss: 0.9905 - accuracy: 0.5383 - val_loss: 1.0060 - val_accuracy: 0.5170
Epoch 73/100
79/79 [==============================] - 13s 159ms/step - loss: 0.9903 - accuracy: 0.5383 - val_loss: 1.0062 - val_accuracy: 0.5170
Epoch 74/100
79/79 [==============================] - 12s 152ms/step - loss: 0.9886 - accuracy: 0.5389 - val_loss: 1.0056 - val_accuracy: 0.5186
Epoch 75/100
79/79 [==============================] - 12s 154ms/step - loss: 0.9888 - accuracy: 0.5386 - val_loss: 1.0054 - val_accuracy: 0.5178
Epoch 76/100
79/79 [==============================] - 12s 155ms/step - loss: 0.9994 - accuracy: 0.5350 - val_loss: 1.0121 - val_accuracy: 0.5182
Epoch 77/100
79/79 [==============================] - 13s 160ms/step - loss: 0.9956 - accuracy: 0.5372 - val_loss: 1.0064 - val_accuracy: 0.5174
Epoch 78/100
79/79 [==============================] - 12s 158ms/step - loss: 0.9907 - accuracy: 0.5383 - val_loss: 1.0057 - val_accuracy: 0.5166
Epoch 79/100
79/79 [==============================] - 12s 156ms/step - loss: 0.9887 - accuracy: 0.5387 - val_loss: 1.0065 - val_accuracy: 0.5170