Created
October 20, 2019 22:50
-
-
Save sausax/d9d736ec4f37a38f090a2acd8732c55d to your computer and use it in GitHub Desktop.
Triplet loss on MNIST using TF tfa.losses.triplet_semihard_loss
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
import tensorflow as tf | |
import tensorflow_addons as tfa | |
from tensorflow.keras.optimizers import Adam | |
# load and normalize data | |
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data() | |
X_train = X_train.astype('float32') | |
X_train /= 255. | |
X_test = X_test.astype('float32') | |
X_test /= 255. | |
# reshape input data | |
X_train = X_train.reshape( | |
X_train.shape[0], 28, 28, 1) | |
X_test = X_test.reshape( | |
X_test.shape[0], 28, 28, 1) | |
def create_model(image_input_shape): | |
input_image = tf.keras.layers.Input(shape=image_input_shape) | |
x = tf.keras.layers.Conv2D(32, | |
(3, 3), | |
input_shape=(28, 28, 1), | |
activation='relu')(input_image) | |
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x) | |
x = tf.keras.layers.Dropout(0.3)(x) | |
x = tf.keras.layers.Conv2D(64, 3, activation='relu')(x) | |
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x) | |
x = tf.keras.layers.Dropout(0.3)(x) | |
x = tf.keras.layers.Conv2D(128, 3, activation='relu')(x) | |
x = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(x) | |
x = tf.keras.layers.Dropout(0.3)(x) | |
x = tf.keras.layers.Flatten()(x) | |
x = tf.keras.layers.Dense(128, activation='relu')(x) | |
x = tf.keras.layers.Dense(64)(x) | |
x = tf.math.l2_normalize(x, axis=1) | |
return tf.keras.models.Model(inputs=input_image, outputs=x) | |
model = create_model((28, 28, 1)) | |
model.compile(loss=tfa.losses.triplet_semihard_loss, optimizer=Adam(lr=0.001), | |
metrics=['accuracy'], weighted_metrics=['accuracy']) | |
model.fit(X_train, y_train, validation_data=(X_test, y_test), | |
epochs=20, | |
batch_size=1024, | |
shuffle=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
What is the training accuracy that got with this model?