This file contains hidden or 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
| # Compile | |
| model.compile(optimizer='adam', | |
| loss='categorical_crossentropy', | |
| metrics=['accuracy']) | |
| # Train | |
| model.fit(train_data, train_labels, epochs=10, callbacks=callbacks, | |
| validation_data=(test_data, test_labels)) | |
| # check the prediction for the 500th test example | |
| check_pred(500) |
This file contains hidden or 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
| # Writing a learning rate reducer function, takes epoch as parameter | |
| def reducer(epoch): | |
| # increase epoch by 1 as epochs are counted from 0 | |
| epoch = epoch + 1 | |
| if epoch<5: | |
| return 0.001 | |
| else: | |
| # the return of the scheduler must be a float | |
| return 0.001 * np.exp(0.1 * (5 - epoch)) | |
This file contains hidden or 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
| # Create the model instance | |
| model = MyModel(num_classes=10) | |
| # Compile | |
| model.compile(optimizer=tf.train.RMSPropOptimizer(0.001), | |
| loss='categorical_crossentropy', | |
| metrics=['accuracy']) | |
| # Trains for 5 epochs. | |
| model.fit(train_data, train_labels, batch_size=32, epochs=5) | |
| model.evaluate(test_data, test_labels) |
This file contains hidden or 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
| class MyModel(tf.keras.Model): | |
| def __init__(self, num_classes): | |
| super(MyModel, self).__init__(name='my_model') | |
| self.num_classes = num_classes | |
| # Defining the layers | |
| self.conv_1 = layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)) | |
| self.maxpool_1 = layers.MaxPooling2D(pool_size=(2,2)) | |
| self.conv_2 = layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu') | |
| self.maxpool_2 = layers.MaxPooling2D(pool_size=(2,2)) | |
| self.conv_3 = layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu') |
This file contains hidden or 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
| def check_pred(index): | |
| # Testing the prediction for the 1st number | |
| plt.figure() | |
| plt.imshow(np.squeeze(test_data[index]), cmap='gray') | |
| # We are using np.argmax since the labels are in categorical form | |
| plt.title('Number: {}'.format(np.argmax(results[index])), color='magenta') | |
| plt.axis('off') | |
| plt.show() | |
| # check the prediction for the 0th test example |
This file contains hidden or 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
| # Compile | |
| model.compile(optimizer=tf.train.AdamOptimizer(learning_rate=0.001), | |
| loss=tf.keras.losses.categorical_crossentropy, | |
| metrics=['accuracy']) | |
| # Train | |
| model.fit(train_data, train_labels, batch_size=32, epochs=5) | |
| # Evaluate | |
| model.evaluate(test_data, test_labels) | |
| # check the prediction for the 1st test example | |
| check_pred(1) |
This file contains hidden or 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
| # Make an input tensor | |
| inputs = tf.keras.Input(shape=(28,28,1)) | |
| # A layer instance is callable on a tensor, and returns a tensor. | |
| x = layers.Conv2D(filters=32, kernel_size=(3,3), strides=2, activation='relu')(inputs) | |
| # We have to provide the input for each layer manually | |
| # This is how we can make arbitrary connections | |
| x = layers.Conv2D(filters=64, kernel_size=(3,3),strides= 2, activation='relu')(x) | |
| # Notice how we are using the same variable x, as python performs the operation first then the assignment | |
| x = layers.Conv2D(filters=64, kernel_size=(3,3),strides=2, padding='same', activation='relu')(x) |
This file contains hidden or 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
| # Make Predictions | |
| results = model.predict(test_data) | |
| print('Prediction shape: ', results.shape) | |
| # Evaluate the model | |
| print('Model evaluation: ') | |
| model.evaluate(test_data, test_labels, batch_size=32) |
This file contains hidden or 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
| model.fit(train_data, | |
| train_labels, | |
| epochs=5, | |
| batch_size=32, | |
| validation_data=(test_data, test_labels)) |
This file contains hidden or 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
| # We can configure the model training in a variety of ways | |
| model.compile(optimizer=tf.train.AdamOptimizer(0.01), | |
| loss='categorical_crossentropy', | |
| metrics=['accuracy']) | |
| # Configure a model for categorical classification. | |
| model.compile(optimizer=tf.train.RMSPropOptimizer(0.01), | |
| loss=tf.keras.losses.categorical_crossentropy, | |
| metrics=[tf.keras.metrics.categorical_accuracy]) |
NewerOlder