<jupyter_start><jupyter_text>Lesson Two<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()



#create model structure
model.add(Dense(256, activation='relu', input_shape=(new_dimension,)))
model.add(Dense(128, activation='relu'))
model.add(Dense(no_labels, activation='softmax'))

#compile the model

model.compile(optimizer='adam', loss=tf.keras.losses.categorical_crossentropy, metrics=['accuracy'])

#train the model

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=20, batch_size=1000)
#evaluate model with test set
test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print('Test loss: {}'.format(test_loss))
print('Test accuracy: {}'.format(test_accuracy))
plt.figure()
plt.plot(history.history['loss'], 'blue')
plt.plot(history.history['val_loss'], 'red')
plt.legend(['Training loss', 'Validation Loss'])
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Loss Curves - before regularisation')
#overfitting identified, now we import dropout for regularization
from tensorflow.keras.layers import Dropout
reg_model = Sequential()
reg_model.add(Dense(256, activation='relu', input_shape=(new_dimension,)))
reg_model.add(Dropout(0.4))
reg_model.add(Dense(128, activation='relu'))
reg_model.add(Dropout(0.4))
reg_model.add(Dense(no_labels, activation='softmax'))
#compile and train reg_model

reg_model.compile(optimizer='adam', loss=tf.keras.losses.categorical_crossentropy, 
              metrics=['accuracy'])

reg_history = reg_model.fit(X_train, y_train, validation_data=(X_val, y_val), 
                            epochs=20, batch_size=1000)
#evaluate reg_model
test_loss, test_accuracy = reg_model.evaluate(test_images, test_labels)
print('Test loss: {}'.format(test_loss))
print('Test accuracy: {}'.format(test_accuracy))
#check for overfitting
plt.figure()
plt.plot(reg_history.history['loss'], 'blue')
plt.plot(reg_history.history['val_loss'], 'red')
plt.legend(['Training loss', 'Validation Loss'])
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.title('Loss Curves - after regularisation')
"big" > "small"
((10 >= 5*2) and (10 <= 5*2))
1/0<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15514103730664242, 'fraction_numerical': 0.017288444040036398, 'mean_word_length': 7.457692307692308, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '161932', 'license': 'Apache-2.0', 'n_tokens_mistral': 801, 'n_tokens_neox': 743, 'n_words': 142, 'path': 'Stage_D/Stage_d_practical.ipynb', 'repo_name': 'josephdickson11/hamoye_AI_internship', 'size': '2198'}