Last active
September 29, 2019 05:22
-
-
Save AFAgarap/86b2ba313d4a61c5470721dfad0c09d4 to your computer and use it in GitHub Desktop.
TensorFlow 2.0 Subclassing API implementation of LeNet-5 architecture. Link to blog: https://towardsdatascience.com/how-can-i-trust-you-fb433a06256c?source=friends_link&sk=0af208dc53be2a326d2407577184686b
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 LeNet(tf.keras.Model): | |
def __init__(self, **kwargs): | |
super(LeNet, self).__init__() | |
self.conv_layer_1 = tf.keras.layers.Conv2D( | |
filters=6, | |
kernel_size=(5, 5), | |
input_shape=(28, 28, 1), | |
padding='valid', | |
activation=tf.nn.relu | |
) | |
self.pool_layer_1 = tf.keras.layers.MaxPooling2D(padding='same') | |
self.conv_layer_2 = tf.keras.layers.Conv2D( | |
filters=16, | |
kernel_size=(5, 5), | |
padding='valid', | |
activation=tf.nn.relu | |
) | |
self.pool_layer_2 = tf.keras.layers.MaxPooling2D(padding='same') | |
self.flatten = tf.keras.layers.Flatten() | |
self.fc_layer_1 = tf.keras.layers.Dense( | |
units=120, | |
activation=tf.nn.relu | |
) | |
self.fc_layer_2 = tf.keras.layers.Dense( | |
units=84, | |
activation=tf.nn.relu | |
) | |
self.output_layer = tf.keras.layers.Dense( | |
units=kwargs['num_classes'], | |
activation=tf.nn.softmax | |
) | |
@tf.function | |
def call(self, features): | |
activation = self.conv_layer_1(features) | |
activation = self.pool_layer_1(activation) | |
activation = self.conv_layer_2(activation) | |
activation = self.pool_layer_2(activation) | |
activation = self.flatten(activation) | |
activation = self.fc_layer_1(activation) | |
activation = self.fc_layer_2(activation) | |
output = self.output_layer(activation) | |
return output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment