Created
February 5, 2021 02:10
-
-
Save PhilipPurwoko/1f87a9599d92e090f35c9094f90d000a to your computer and use it in GitHub Desktop.
Create MNIST model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow import keras | |
from sklearn.metrics import classification_report | |
import numpy as np | |
# Load data provided by keras | |
data = keras.datasets.mnist | |
(x_train, y_train), (x_test, y_test) = data.load_data() | |
# Create model using sequential API | |
model = keras.Sequential([ | |
# Input Layers | |
keras.layers.Flatten(input_shape=(28, 28, )), | |
# Hidden Layers | |
keras.layers.Dense(128, activation='relu'), | |
keras.layers.Dense(128, activation='relu'), | |
keras.layers.Dense(128, activation='relu'), | |
keras.layers.Dense(128, activation='relu'), | |
keras.layers.Dense(128, activation='relu'), | |
keras.layers.Dropout(0.5), | |
# Output Layers | |
keras.layers.Dense(10, activation='softmax') | |
]) | |
# Compile model | |
model.compile( | |
optimizer = keras.optimizers.Adam(), | |
loss = keras.losses.SparseCategoricalCrossentropy(), | |
metrics = ['accuracy'] | |
) | |
# Model training | |
model.fit(x_train, y_train, verbose=1, epochs=50, batch_size=32) | |
# Make prediction from test data | |
prediction_probability = model.predict(x_test) | |
prediction = np.array([np.argmax(pred) for pred in prediction_probability]) | |
# Display the model performance | |
print(classification_report(y_test, prediction) | |
# Save model | |
model.save('mnist_classification.h5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment