Jovian
⭐️
Sign In
In [1]:
import tensorflow as tf
from tensorflow import keras

# double check your tf version
print(tf.__version__)

# get sampling data set
fashion_mnist = keras.datasets.fashion_mnist
(trainx, trainy), (testx, testy) = fashion_mnist.load_data()
2.0.0-alpha0
In [2]:
import matplotlib.pyplot as plt
imgplot = plt.imshow(trainx[0])
plt.show()
<Figure size 640x480 with 1 Axes>
In [3]:
print(trainx.shape)
print(trainy.shape)
print(testx.shape)
print(testy.shape)
(60000, 28, 28) (60000,) (10000, 28, 28) (10000,)
In [4]:
# you will see the value were in range 0~255, normalize it
trainx = trainx / 255.0
testx  = testx / 255.0

trainx = trainx.reshape((60000,28,28,1))
testx  =  testx.reshape((10000,28,28,1))
In [5]:
import numpy as np

# in tf 2.0 you may create model like this.
model = keras.Sequential([
    keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28,28,1)),
    keras.layers.MaxPooling2D((2,2)),
    keras.layers.Conv2D(64, (3,3), activation='relu'),
    keras.layers.MaxPooling2D((2,2)),
    keras.layers.Conv2D(64, (3,3), activation='relu'),
    keras.layers.Flatten(),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(10, activation='softmax')    
])

model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ max_pooling2d (MaxPooling2D) (None, 13, 13, 32) 0 _________________________________________________________________ conv2d_1 (Conv2D) (None, 11, 11, 64) 18496 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ conv2d_2 (Conv2D) (None, 3, 3, 64) 36928 _________________________________________________________________ flatten (Flatten) (None, 576) 0 _________________________________________________________________ dense (Dense) (None, 64) 36928 _________________________________________________________________ dense_1 (Dense) (None, 10) 650 ================================================================= Total params: 93,322 Trainable params: 93,322 Non-trainable params: 0 _________________________________________________________________
In [6]:
model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',
             metrics=['accuracy'])

model.fit(trainx, trainy, epochs=5)
Epoch 1/5 60000/60000 [==============================] - 36s 602us/sample - loss: 0.4875 - accuracy: 0.8213 Epoch 2/5 60000/60000 [==============================] - 36s 599us/sample - loss: 0.3177 - accuracy: 0.8844 Epoch 3/5 60000/60000 [==============================] - 36s 594us/sample - loss: 0.2712 - accuracy: 0.9007 Epoch 4/5 60000/60000 [==============================] - 36s 595us/sample - loss: 0.2447 - accuracy: 0.9099 Epoch 5/5 60000/60000 [==============================] - 36s 601us/sample - loss: 0.2205 - accuracy: 0.9173
Out[6]:
<tensorflow.python.keras.callbacks.History at 0x269fbccb9b0>
In [7]:
loss, acc = model.evaluate(testx, testy)
10000/10000 [==============================] - 2s 177us/sample - loss: 0.2612 - accuracy: 0.9038
In [ ]:
from tensorflow.keras import datasets, layers, models

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

model.summary()

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(trainx, trainy, epochs=5)
model.evaluate(testx, testy)
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_3 (Conv2D) (None, 26, 26, 32) 320 _________________________________________________________________ max_pooling2d_2 (MaxPooling2 (None, 13, 13, 32) 0 _________________________________________________________________ conv2d_4 (Conv2D) (None, 11, 11, 64) 18496 _________________________________________________________________ max_pooling2d_3 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________ conv2d_5 (Conv2D) (None, 3, 3, 64) 36928 _________________________________________________________________ flatten_1 (Flatten) (None, 576) 0 _________________________________________________________________ dense_2 (Dense) (None, 64) 36928 _________________________________________________________________ dense_3 (Dense) (None, 10) 650 ================================================================= Total params: 93,322 Trainable params: 93,322 Non-trainable params: 0 _________________________________________________________________ Epoch 1/5 60000/60000 [==============================] - 37s 620us/sample - loss: 0.5025 - accuracy: 0.8171 Epoch 2/5 5408/60000 [=>............................] - ETA: 34s - loss: 0.3593 - accuracy: 0.8678- ETA: 36s