In [0]:
from __future__ import absolute_import, division, print_function, unicode_literals

# TensorFlow and tf.keras
# Have to install TensorFlow 2.0 with new session (but not each new runtime)
# !pip install tensorflow=="2.0.0"
import tensorflow as tf
from tensorflow import keras
#from keras.models import Sequential
#from keras.layers import Dense, Conv2D, Dropout, MaxPooling2D, Flatten
from keras.utils import np_utils

# Helper libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time

print(tf.__version__)
1.15.0
In [0]:
# Download data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
In [0]:
plt.figure(figsize=(10,10))
for i in range(25):
    plt.subplot(5,5,i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(x_train[i], cmap = plt.cm.binary)
    plt.xlabel(class_names[y_train[i]])
plt.show()
In [0]:
# Data prep
# Normalize data
x_train = x_train / 255.0
x_test = x_test / 255.0

# add empty color dimension
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
In [0]:
 
Out[0]:
(60000, 28, 28, 1)
In [0]:
# TensorFlow/Keras Model
# https://colab.research.google.com/github/tensorflow/tpu/blob/master/tools/colab/fashion_mnist.ipynb?fbclid=IwAR070qdCr-MYoVJs0ovnv8nQCta5-GNzEfXgj_sXl31T53PPjRc5nGLh-w0#scrollTo=pWEYmd_hIWg8
def create_model():
  model = tf.keras.models.Sequential()
  model.add(tf.keras.layers.BatchNormalization(input_shape=x_train.shape[1:]))
  model.add(tf.keras.layers.Conv2D(64, (5, 5), padding='same', activation='elu'))
  model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
  model.add(tf.keras.layers.Dropout(0.25))

  model.add(tf.keras.layers.BatchNormalization(input_shape=x_train.shape[1:]))
  model.add(tf.keras.layers.Conv2D(128, (5, 5), padding='same', activation='elu'))
  model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))
  model.add(tf.keras.layers.Dropout(0.25))

  model.add(tf.keras.layers.BatchNormalization(input_shape=x_train.shape[1:]))
  model.add(tf.keras.layers.Conv2D(256, (5, 5), padding='same', activation='elu'))
  model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
  model.add(tf.keras.layers.Dropout(0.25))

  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dense(256))
  model.add(tf.keras.layers.Activation('elu'))
  model.add(tf.keras.layers.Dropout(0.5))
  model.add(tf.keras.layers.Dense(10))
  model.add(tf.keras.layers.Activation('softmax'))
  return model
In [0]:
# Initialize TPUs
import os
tf.config.experimental_connect_to_host('grpc://' + os.environ['COLAB_TPU_ADDR'])
resolver = tf.distribute.cluster_resolver.TPUClusterResolver('grpc://' + os.environ['COLAB_TPU_ADDR'])
tf.tpu.experimental.initialize_tpu_system(resolver)
strategy = tf.distribute.experimental.TPUStrategy(resolver) 
tf.compat.v1.disable_eager_execution()
INFO:tensorflow:Initializing the TPU system: 10.7.199.194:8470
INFO:tensorflow:Clearing out eager caches
INFO:tensorflow:Finished initializing TPU system.
INFO:tensorflow:Found TPU system:
INFO:tensorflow:*** Num TPU Cores: 8
INFO:tensorflow:*** Num TPU Workers: 1
INFO:tensorflow:*** Num TPU Cores Per Worker: 8
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:localhost/replica:0/task:0/device:CPU:0, CPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:localhost/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:CPU:0, CPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:0, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:1, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:2, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:3, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:4, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:5, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:6, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU:7, TPU, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:TPU_SYSTEM:0, TPU_SYSTEM, 0, 0)
INFO:tensorflow:*** Available Device: _DeviceAttributes(/job:worker/replica:0/task:0/device:XLA_CPU:0, XLA_CPU, 0, 0)
In [0]:
with strategy.scope():
  keras_model = create_model()
  keras_model.compile(
      optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3, ),
      loss='sparse_categorical_crossentropy',
      metrics=['sparse_categorical_accuracy'])
In [0]:
# Send model to TPUs
t1 = time.time()
keras_model.fit(
    x_train.astype(np.float32), y_train.astype(np.float32),
    epochs = 20,
    steps_per_epoch = 1500,
    # steps_per_epoch = 60, for some reason it crashes if I don't specify steps_per_epoch or if steps = nrow(train)/epochs
    validation_data = (x_test.astype(np.float32), y_test.astype(np.float32)),
    validation_freq = 1
)
t2 = time.time()
# We can save our model with: not sure where it is going
keras_model.save('model.h5')
# and reload it with:
# keras_model = tf.keras.models.load_model('model.h5')
train_time = t2 - t1
Epoch 1/20
1499/1500 [============================>.] - ETA: 0s - loss: 0.6552 - sparse_categorical_accuracy: 0.7892INFO:tensorflow:Running validation at fit epoch: 0
250/250 [==============================] - 4s 15ms/step
250/250 [==============================] - 4s 15ms/step
1500/1500 [==============================] - 17s 11ms/step - loss: 0.6551 - sparse_categorical_accuracy: 0.7892 - val_loss: 0.3462 - val_sparse_categorical_accuracy: 0.8757
Epoch 2/20
1494/1500 [============================>.] - ETA: 0s - loss: 0.3947 - sparse_categorical_accuracy: 0.8603INFO:tensorflow:Running validation at fit epoch: 1
250/250 [==============================] - 4s 16ms/step
250/250 [==============================] - 4s 16ms/step
1500/1500 [==============================] - 16s 10ms/step - loss: 0.3942 - sparse_categorical_accuracy: 0.8605 - val_loss: 0.3295 - val_sparse_categorical_accuracy: 0.8898
Epoch 3/20
1492/1500 [============================>.] - ETA: 0s - loss: 0.3482 - sparse_categorical_accuracy: 0.8761INFO:tensorflow:Running validation at fit epoch: 2
250/250 [==============================] - 4s 18ms/step
250/250 [==============================] - 4s 18ms/step
1500/1500 [==============================] - 16s 11ms/step - loss: 0.3482 - sparse_categorical_accuracy: 0.8760 - val_loss: 0.3190 - val_sparse_categorical_accuracy: 0.8836
Epoch 4/20
1495/1500 [============================>.] - ETA: 0s - loss: 0.3288 - sparse_categorical_accuracy: 0.8826INFO:tensorflow:Running validation at fit epoch: 3
250/250 [==============================] - 5s 19ms/step
250/250 [==============================] - 5s 19ms/step
1500/1500 [==============================] - 17s 12ms/step - loss: 0.3286 - sparse_categorical_accuracy: 0.8826 - val_loss: 0.2983 - val_sparse_categorical_accuracy: 0.8958
Epoch 5/20
1496/1500 [============================>.] - ETA: 0s - loss: 0.3100 - sparse_categorical_accuracy: 0.8896INFO:tensorflow:Running validation at fit epoch: 4
250/250 [==============================] - 5s 22ms/step
250/250 [==============================] - 5s 22ms/step
1500/1500 [==============================] - 18s 12ms/step - loss: 0.3095 - sparse_categorical_accuracy: 0.8898 - val_loss: 0.2898 - val_sparse_categorical_accuracy: 0.9027
Epoch 6/20
1491/1500 [============================>.] - ETA: 0s - loss: 0.2871 - sparse_categorical_accuracy: 0.8987INFO:tensorflow:Running validation at fit epoch: 5
250/250 [==============================] - 6s 22ms/step
250/250 [==============================] - 6s 22ms/step
1500/1500 [==============================] - 18s 12ms/step - loss: 0.2873 - sparse_categorical_accuracy: 0.8986 - val_loss: 0.2749 - val_sparse_categorical_accuracy: 0.9030
Epoch 7/20
1493/1500 [============================>.] - ETA: 0s - loss: 0.2813 - sparse_categorical_accuracy: 0.8994INFO:tensorflow:Running validation at fit epoch: 6
250/250 [==============================] - 6s 25ms/step
250/250 [==============================] - 6s 25ms/step
1500/1500 [==============================] - 19s 13ms/step - loss: 0.2814 - sparse_categorical_accuracy: 0.8994 - val_loss: 0.2919 - val_sparse_categorical_accuracy: 0.9067
Epoch 8/20
1494/1500 [============================>.] - ETA: 0s - loss: 0.2632 - sparse_categorical_accuracy: 0.9054INFO:tensorflow:Running validation at fit epoch: 7
250/250 [==============================] - 7s 26ms/step
250/250 [==============================] - 7s 26ms/step
1500/1500 [==============================] - 20s 13ms/step - loss: 0.2634 - sparse_categorical_accuracy: 0.9054 - val_loss: 0.2795 - val_sparse_categorical_accuracy: 0.9035
Epoch 9/20
1491/1500 [============================>.] - ETA: 0s - loss: 0.2509 - sparse_categorical_accuracy: 0.9102INFO:tensorflow:Running validation at fit epoch: 8
250/250 [==============================] - 7s 28ms/step
250/250 [==============================] - 7s 28ms/step
1500/1500 [==============================] - 21s 14ms/step - loss: 0.2505 - sparse_categorical_accuracy: 0.9103 - val_loss: 0.2699 - val_sparse_categorical_accuracy: 0.9120
Epoch 10/20
1497/1500 [============================>.] - ETA: 0s - loss: 0.2429 - sparse_categorical_accuracy: 0.9121INFO:tensorflow:Running validation at fit epoch: 9
250/250 [==============================] - 8s 32ms/step
250/250 [==============================] - 8s 32ms/step
1500/1500 [==============================] - 21s 14ms/step - loss: 0.2430 - sparse_categorical_accuracy: 0.9121 - val_loss: 0.3174 - val_sparse_categorical_accuracy: 0.9052
Epoch 11/20
1493/1500 [============================>.] - ETA: 0s - loss: 0.2353 - sparse_categorical_accuracy: 0.9168INFO:tensorflow:Running validation at fit epoch: 10
250/250 [==============================] - 8s 33ms/step
250/250 [==============================] - 8s 33ms/step
1500/1500 [==============================] - 23s 15ms/step - loss: 0.2351 - sparse_categorical_accuracy: 0.9169 - val_loss: 0.2584 - val_sparse_categorical_accuracy: 0.9150
Epoch 12/20
1492/1500 [============================>.] - ETA: 0s - loss: 0.2245 - sparse_categorical_accuracy: 0.9193INFO:tensorflow:Running validation at fit epoch: 11
250/250 [==============================] - 9s 36ms/step
250/250 [==============================] - 9s 36ms/step
1500/1500 [==============================] - 24s 16ms/step - loss: 0.2244 - sparse_categorical_accuracy: 0.9194 - val_loss: 0.2740 - val_sparse_categorical_accuracy: 0.9164
Epoch 13/20
1499/1500 [============================>.] - ETA: 0s - loss: 0.2153 - sparse_categorical_accuracy: 0.9222INFO:tensorflow:Running validation at fit epoch: 12
250/250 [==============================] - 10s 40ms/step
250/250 [==============================] - 10s 40ms/step
1500/1500 [==============================] - 26s 17ms/step - loss: 0.2152 - sparse_categorical_accuracy: 0.9222 - val_loss: 0.2792 - val_sparse_categorical_accuracy: 0.9130
Epoch 14/20
1497/1500 [============================>.] - ETA: 0s - loss: 0.2077 - sparse_categorical_accuracy: 0.9253INFO:tensorflow:Running validation at fit epoch: 13
250/250 [==============================] - 11s 44ms/step
250/250 [==============================] - 11s 44ms/step
1500/1500 [==============================] - 28s 18ms/step - loss: 0.2077 - sparse_categorical_accuracy: 0.9252 - val_loss: 0.2981 - val_sparse_categorical_accuracy: 0.9146
Epoch 15/20
1494/1500 [============================>.] - ETA: 0s - loss: 0.2057 - sparse_categorical_accuracy: 0.9256INFO:tensorflow:Running validation at fit epoch: 14
250/250 [==============================] - 12s 46ms/step
250/250 [==============================] - 12s 46ms/step
1500/1500 [==============================] - 28s 19ms/step - loss: 0.2056 - sparse_categorical_accuracy: 0.9257 - val_loss: 0.2861 - val_sparse_categorical_accuracy: 0.9181
Epoch 16/20
1494/1500 [============================>.] - ETA: 0s - loss: 0.1940 - sparse_categorical_accuracy: 0.9310INFO:tensorflow:Running validation at fit epoch: 15
250/250 [==============================] - 12s 49ms/step
250/250 [==============================] - 12s 49ms/step
1500/1500 [==============================] - 31s 21ms/step - loss: 0.1941 - sparse_categorical_accuracy: 0.9309 - val_loss: 0.3064 - val_sparse_categorical_accuracy: 0.9134
Epoch 17/20
1499/1500 [============================>.] - ETA: 0s - loss: 0.1864 - sparse_categorical_accuracy: 0.9327INFO:tensorflow:Running validation at fit epoch: 16
250/250 [==============================] - 13s 51ms/step
250/250 [==============================] - 13s 51ms/step
1500/1500 [==============================] - 31s 21ms/step - loss: 0.1864 - sparse_categorical_accuracy: 0.9327 - val_loss: 0.3084 - val_sparse_categorical_accuracy: 0.9175
Epoch 18/20
1498/1500 [============================>.] - ETA: 0s - loss: 0.1822 - sparse_categorical_accuracy: 0.9342INFO:tensorflow:Running validation at fit epoch: 17
250/250 [==============================] - 14s 54ms/step
250/250 [==============================] - 14s 54ms/step
1500/1500 [==============================] - 32s 21ms/step - loss: 0.1822 - sparse_categorical_accuracy: 0.9342 - val_loss: 0.2946 - val_sparse_categorical_accuracy: 0.9177
Epoch 19/20
1497/1500 [============================>.] - ETA: 0s - loss: 0.1798 - sparse_categorical_accuracy: 0.9351INFO:tensorflow:Running validation at fit epoch: 18
250/250 [==============================] - 14s 57ms/step
250/250 [==============================] - 14s 57ms/step
1500/1500 [==============================] - 33s 22ms/step - loss: 0.1799 - sparse_categorical_accuracy: 0.9351 - val_loss: 0.3491 - val_sparse_categorical_accuracy: 0.9052
Epoch 20/20
1497/1500 [============================>.] - ETA: 0s - loss: 0.1715 - sparse_categorical_accuracy: 0.9374INFO:tensorflow:Running validation at fit epoch: 19
250/250 [==============================] - 16s 62ms/step
250/250 [==============================] - 16s 62ms/step
1500/1500 [==============================] - 34s 23ms/step - loss: 0.1716 - sparse_categorical_accuracy: 0.9373 - val_loss: 0.2853 - val_sparse_categorical_accuracy: 0.9196
In [0]:
print(keras_model.summary())
print(train_time)
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
batch_normalization (BatchNo multiple                  4         
_________________________________________________________________
conv2d (Conv2D)              multiple                  1664      
_________________________________________________________________
max_pooling2d (MaxPooling2D) multiple                  0         
_________________________________________________________________
dropout (Dropout)            multiple                  0         
_________________________________________________________________
batch_normalization_1 (Batch multiple                  256       
_________________________________________________________________
conv2d_1 (Conv2D)            multiple                  204928    
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_1 (Dropout)          multiple                  0         
_________________________________________________________________
batch_normalization_2 (Batch multiple                  512       
_________________________________________________________________
conv2d_2 (Conv2D)            multiple                  819456    
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 multiple                  0         
_________________________________________________________________
dropout_2 (Dropout)          multiple                  0         
_________________________________________________________________
flatten (Flatten)            multiple                  0         
_________________________________________________________________
dense (Dense)                multiple                  590080    
_________________________________________________________________
activation (Activation)      multiple                  0         
_________________________________________________________________
dropout_3 (Dropout)          multiple                  0         
_________________________________________________________________
dense_1 (Dense)              multiple                  2570      
_________________________________________________________________
activation_1 (Activation)    multiple                  0         
=================================================================
Total params: 1,619,470
Trainable params: 1,619,084
Non-trainable params: 386
_________________________________________________________________
None
485.14945220947266
In [0]:
def training_curves(training, validation, title, subplot):
  ax = plt.subplot(subplot)
  ax.plot(training)
  ax.plot(validation)
  ax.set_title('model '+ title)
  ax.set_ylabel(title)
  ax.set_xlabel('epoch')
  ax.legend(['training', 'validation'])

plt.subplots(figsize=(10,10))
plt.tight_layout()
training_curves(keras_model.history.history['sparse_categorical_accuracy'], 
                        keras_model.history.history['val_sparse_categorical_accuracy'], 'accuracy', 211)
training_curves(keras_model.history.history['loss'], 
                        keras_model.history.history['val_loss'], 'loss', 212)
In [0]:
# https://gist.github.com/yufengg/2b2fd4b81b72f0f9c7b710fa87077145
from sklearn import datasets, linear_model
from sklearn.metrics import accuracy_score
from sklearn.metrics import r2_score
from sklearn.metrics import f1_score

x_train_flat = np.array(x_train).reshape((-1, 28*28))
x_test_flat = np.array(x_test).reshape((-1, 28*28))
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(x_train_flat, y_train)
Out[0]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
In [0]:
y_pred = regr.predict(x_test_flat).round(0)
In [0]:
accuracy_score(y_test, y_pred)
Out[0]:
0.3688
In [0]:
r2_score(y_test, y_pred)
Out[0]:
0.7518424242424242
In [0]:
y_pred.min()
Out[0]:
-2.0