Berikut merupakan kode python analisis sentimen LSTM yang memanfaatkan Gaussian Process Optimization untuk mendapatkan kombinasi parameter dengan akurasi model terbaik.
"import pickle
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.preprocessing.text import Tokenizer
from keras.utils import pad_sequences
from keras.models import Sequential
from keras.layers import LSTM, Dense, Embedding, SpatialDropout1D, Dropout
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from sklearn.metrics import classification_report
from skopt import gp_minimize
from skopt.space import Real, Integer, Categorical
from skopt.utils import use_named_args

def create_model(vocab_size, sequence_length, embedding_dim, lstm_units, dropout_rate, learning_rate):
    model = Sequential()
    model.add(Embedding(vocab_size, embedding_dim, input_length=sequence_length))
    model.add(SpatialDropout1D(dropout_rate))
    model.add(LSTM(lstm_units, dropout=dropout_rate, recurrent_dropout=dropout_rate))
    model.add(Dense(3, activation=‘softmax’))

    optimizer = Adam(lr=learning_rate)

    model.compile(loss=‘sparse_categorical_crossentropy’, optimizer=optimizer, metrics=[‘accuracy’])

    return model

# Your existing code to prepare the dataset and perform train-test split goes here…

space  = [Integer(50, 150, name=‘embedding_dim’),
          Integer(50, 100, name=‘lstm_units’),
          Real(0.1, 0.5, name=‘dropout_rate’),
          Real(1e-4, 1e-1, “log-uniform”, name=‘learning_rate’)]

@use_named_args(space)
def objective(**params):
    print(“Trying parameters:”, params)

    model = create_model(max_vocab_size, max_sequence_length,
                         params[“embedding_dim”], params[“lstm_units”],
                         params[“dropout_rate”], params[“learning_rate”])

    history = model.fit(X_train_padded, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor=‘val_loss’, patience=3, min_delta=0.0001)])

    # Evaluate model on the test set
    acc = model.evaluate(X_test_padded, y_test)[1]
    print(“Accuracy for parameter combination:”, acc)

    # Minimize the negative accuracy (as gp_minimize tries to minimize the objective)
    return -acc

# Search for the best parameter combination
res_gp = gp_minimize(objective, space, n_calls=10, random_state=0, verbose=True)

print(“Best parameters found:”, res_gp.x)

# Train the model with the best parameters
best_model = create_model(max_vocab_size, max_sequence_length,
                          res_gp.x[0], res_gp.x[1],
                          res_gp.x[2], res_gp.x[3])

best_model_history = best_model.fit(X_train_padded, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.1, callbacks=[EarlyStopping(monitor=‘val_loss’, patience=3, min_delta=0.0001)])

best_model_acc = best_model.evaluate(X_test_padded, y_test)[1]
print(‘Best model accuracy on test data:’, best_model_acc)

# Save the best model to .h5 file
best_model.save(“best_model.h5”)

# Save the tokenizer to a .pkl file
with open(‘tokenizer.pkl’, ‘wb’) as f:
    pickle.dump(tokenizer, f)

Modifikasi kode tersebut dengan mengubahnya menjadi menggunakan Bayesian Optimization dengan Gaussian Process untuk mengoptimalkan parameter analisis sentimen LSTM untuk mendapatkan akurasi model terbaik. Parameter yang dicoba-coba adalah:
1. Jumlah unit LSTM
2. Jumlah lapisan LSTM
3. Dimensi Embedding
4. Learning rate
5. Dropout rate
6. Ukuran batch
7. Optimizer (misalnya SGD, Adam, RMSprop)
8. Ukuran vocab
10. Panjang maksimum urutan
11. Fungsi loss (misalnya binary_crossentropy atau categorical_crossentropy)