%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.optimizers import SGD, Adam
!pip install -U tensorflow-addons
from tensorflow_addons.losses import TripletSemiHardLoss
Requirement already satisfied: tensorflow-addons in /usr/local/lib/python3.7/dist-packages (0.16.1) Requirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.7/dist-packages (from tensorflow-addons) (2.7.1)
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28*28)/255
x_test = x_test.reshape(-1, 28*28)/255
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(784,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(2))
model.compile(optimizer=Adam(0.001),
loss=TripletSemiHardLoss(margin=0.87))
history = model.fit(x_train, y_train, epochs=5)
Epoch 1/5 1875/1875 [==============================] - 8s 4ms/step - loss: 0.0483 Epoch 2/5 1875/1875 [==============================] - 8s 4ms/step - loss: 0.0364 Epoch 3/5 1875/1875 [==============================] - 7s 4ms/step - loss: 0.0296 Epoch 4/5 1875/1875 [==============================] - 7s 4ms/step - loss: 0.0257 Epoch 5/5 1875/1875 [==============================] - 7s 4ms/step - loss: 0.0219
def representation(x, y):
y_pred = model.predict(x)
idx = np.random.randint(x.shape[0], size=2000)
colors = ['r', 'g', 'b', 'c', 'm', 'y', 'k', 'lime', 'orange', 'purple']
for label, c in enumerate(colors):
label_idx = idx[y[idx]==label]
plt.scatter(y_pred[label_idx, 0], y_pred[label_idx, 1],
c=c, label=str(label))
plt.legend();
plt.figure(figsize=(12, 6))
plt.subplot(1, 2, 1)
representation(x_train, y_train)
plt.title("Training Data")
plt.subplot(1, 2, 2)
representation(x_test, y_test)
plt.title("Testing Data");