Created
July 12, 2020 11:21
-
-
Save SphericalKat/0914fa0abf465033df266e9af4fb3b52 to your computer and use it in GitHub Desktop.
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
# ====================================================================== | |
# There are 5 questions in this exam with increasing difficulty from 1-5. | |
# Please note that the weight of the grade for the question is relative | |
# to its difficulty. So your Category 1 question will score significantly | |
# less than your Category 5 question. | |
# | |
# Don't use lambda layers in your model. | |
# You do not need them to solve the question. | |
# Lambda layers are not supported by the grading infrastructure. | |
# | |
# You must use the Submit and Test button to submit your model | |
# at least once in this category before you finally submit your exam, | |
# otherwise you will score zero for this category. | |
# ====================================================================== | |
# | |
# Basic Datasets Question | |
# | |
# Create and train a classifier for the MNIST dataset. | |
# Note that the test will expect it to classify 10 classes and that the | |
# input shape should be the native size of the MNIST dataset which is | |
# 28x28 monochrome. Do not resize the data. Your input layer should accept | |
# (28,28) as the input shape only. If you amend this, the tests will fail. | |
# | |
import tensorflow as tf | |
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint | |
from tensorflow.keras.layers import Dense, Dropout, Flatten | |
from tensorflow.keras.models import Sequential | |
physical_devices = tf.config.list_physical_devices('GPU') | |
try: | |
tf.config.experimental.set_memory_growth(physical_devices[0], True) | |
except: | |
pass | |
def solution_model(): | |
mnist = tf.keras.datasets.mnist | |
(training_images, training_labels), (test_images, test_labels) = mnist.load_data() | |
training_images = training_images / 255.0 | |
test_images = test_images / 255.0 | |
callbacks = [ | |
EarlyStopping( | |
monitor='val_accuracy', | |
min_delta=1e-4, | |
patience=3, | |
verbose=1 | |
), | |
ModelCheckpoint( | |
filepath='mymodel.h5', | |
monitor='val_accuracy', | |
mode='max', | |
save_best_only=True, | |
save_weights_only=False, | |
verbose=1 | |
) | |
] | |
model = Sequential() | |
model.add(Flatten()) | |
model.add(Dense(64, activation='relu')) | |
model.add(Dropout(0.2)) | |
model.add(Dense(10, activation='softmax')) | |
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy']) | |
model.fit( | |
training_images, | |
training_labels, | |
batch_size=128, | |
epochs=20, | |
verbose=1, | |
validation_data=(test_images, test_labels), | |
callbacks=callbacks | |
) | |
# YOUR CODE HERE | |
return model | |
# Note that you'll need to save your model as a .h5 like this. | |
# When you press the Submit and Test button, your saved .h5 model will | |
# be sent to the testing infrastructure for scoring | |
# and the score will be returned to you. | |
if __name__ == '__main__': | |
model = solution_model() | |
# model.save("mymodel.bak.h5") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment