<jupyter_start><jupyter_text>Plotting the training and validation loss<jupyter_code>import matplotlib.pyplot as plt

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) + 1)
# "bo" is for "blue dot"
plt.plot(epochs, loss, 'bo', label='Training loss')
# b is for "solid blue line"
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# "bo" is for "blue dot"
plt.plot(epochs, acc, 'bo', label='Training acc')
# b is for "solid blue line"
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation acc')
plt.xlabel('Epochs')
plt.ylabel('Acc')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Validation<jupyter_code>val_predict = model.predict(validate_data_features)
val_predict = categorical_probas_to_classes(val_predict)
from sklearn.metrics import classification_report
target_names = ['0', '1']
print(classification_report(y_val, val_predict, target_names=target_names))<jupyter_output>precision    recall  f1-score   support

          0       0.16      0.17      0.16      1231
          1       0.18      0.17      0.17      1293

avg / total       0.17      0.17      0.17      2524<jupyter_text>Prediction<jupyter_code>df_test = pd.read_csv("data/workshop_test.csv")
df_test['cleaned_text'] = df_test.apply (lambda row: text_to_words(row.review), axis=1)
test_data_features = vectorizer.transform(df_test['cleaned_text'].values.astype('U'))
test_data_features = test_data_features.toarray()
# Generating predictions for new data
prediction = model.predict(test_data_features)
print prediction
def categorical_probas_to_classes(p):
    res = []
    for i in p:
        if i[0] > 0.5:
            res.append(1)
        else:
            res.append(0)
    return res
#     return np.argmax(p, axis=1)
actual_prediction = categorical_probas_to_classes(prediction)
# actual_prediction<jupyter_output><empty_output><jupyter_text>Recurrent Neural Network<jupyter_code>from keras.preprocessing import sequence
from keras.layers import Dense, SimpleRNN, Embedding
from keras.models import Sequential
max_features = 5000
model = Sequential()
model.add(Embedding(max_features, 32))
model.add(SimpleRNN(32))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['acc'])
history = model.fit(train_data_features, y_train,
                    epochs=5,
                    batch_size=128,
                    validation_split=0.2)
from keras.callbacks import EarlyStopping, ModelCheckpoint
early_stopping = EarlyStopping(patience=1, verbose=1)
checkpointer = ModelCheckpoint(
    filepath='model-weights' + "-" + "{epoch:02d}-" + str(
        time.time()) + ".hdf5",
    verbose=2, save_best_only=True)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup