| import tensorflow as tf |
| import pandas as pd |
| import numpy as np |
| import matplotlib.pyplot as plt |
| from tensorflow.keras.preprocessing.text import Tokenizer |
| from tensorflow.keras.preprocessing.sequence import pad_sequences |
|
|
| |
| physical_devices = tf.config.experimental.list_physical_devices('GPU') |
| if physical_devices: |
| tf.config.experimental.set_memory_growth(physical_devices[0], True) |
| print("GPU is available and will be used.") |
| else: |
| print("No GPU found. Using CPU.") |
|
|
| |
| df = pd.read_csv("dataset/imdb.csv") |
|
|
| |
| print("Dataset columns:", df.columns) |
|
|
| |
| df.rename(columns={'review': 'text', 'sentiment': 'label'}, inplace=True) |
|
|
| |
| df['label'] = df['label'].apply(lambda x: 1 if x.strip().lower() == 'positive' else 0) |
|
|
| |
| tokenizer = Tokenizer(num_words=20000, oov_token="<OOV>") |
| tokenizer.fit_on_texts(df['text']) |
| sequences = tokenizer.texts_to_sequences(df['text']) |
| padded_sequences = pad_sequences(sequences, maxlen=250) |
|
|
| |
| X_train, y_train = padded_sequences[:20000], df['label'][:20000] |
| X_test, y_test = padded_sequences[20000:], df['label'][20000:] |
|
|
| |
| model = tf.keras.Sequential([ |
| tf.keras.layers.Embedding(20000, 64, input_length=250), |
| tf.keras.layers.Conv1D(128, 5, activation='relu'), |
| tf.keras.layers.MaxPooling1D(pool_size=2), |
| tf.keras.layers.LSTM(128, return_sequences=True), |
| tf.keras.layers.LSTM(64), |
| tf.keras.layers.Dense(64, activation='relu'), |
| tf.keras.layers.Dense(1, activation='sigmoid') |
| ]) |
|
|
| |
| model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
|
|
| |
| epochs = 5 |
| history = model.fit(X_train, y_train, epochs=epochs, validation_data=(X_test, y_test), batch_size=128) |
|
|
| |
| model.save("src/sentiment_model_final.h5") |
|
|
| |
| for i in range(10, epochs + 1, 10): |
| model.save(f"src/sentiment_model_epoch_{i}.h5") |
|
|
| |
| def smooth_curve(points, factor=0.8): |
| """Applies exponential moving average smoothing to a curve.""" |
| smoothed_points = [] |
| for point in points: |
| if smoothed_points: |
| smoothed_points.append(smoothed_points[-1] * factor + point * (1 - factor)) |
| else: |
| smoothed_points.append(point) |
| return smoothed_points |
|
|
| plt.figure(figsize=(12, 6)) |
|
|
| |
| plt.subplot(1, 2, 1) |
| plt.plot(range(epochs), history.history['loss'], label='Training Loss', marker='o', alpha=0.3) |
| plt.plot(range(epochs), history.history['val_loss'], label='Validation Loss', marker='o', alpha=0.3) |
| plt.plot(range(epochs), smooth_curve(history.history['loss']), label='Smoothed Training Loss', linewidth=2) |
| plt.plot(range(epochs), smooth_curve(history.history['val_loss']), label='Smoothed Validation Loss', linewidth=2) |
| plt.xlabel("Epochs") |
| plt.ylabel("Loss") |
| plt.legend() |
| plt.title("Training vs. Validation Loss (Detailed)") |
|
|
| |
| plt.subplot(1, 2, 2) |
| plt.plot(range(epochs), history.history['accuracy'], label='Training Accuracy', marker='o', alpha=0.3) |
| plt.plot(range(epochs), history.history['val_accuracy'], label='Validation Accuracy', marker='o', alpha=0.3) |
| plt.plot(range(epochs), smooth_curve(history.history['accuracy']), label='Smoothed Training Accuracy', linewidth=2) |
| plt.plot(range(epochs), smooth_curve(history.history['val_accuracy']), label='Smoothed Validation Accuracy', linewidth=2) |
| plt.xlabel("Epochs") |
| plt.ylabel("Accuracy") |
| plt.legend() |
| plt.title("Training vs. Validation Accuracy (Detailed)") |
|
|
| |
| plt.savefig("src/training_results_detailed.png") |
| plt.show() |
|
|