Learn practical skills, build real-world projects, and advance your career
Updated 5 years ago
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
import matplotlib.pyplot as plt
imgplot = plt.imshow(trainx[0])
plt.show()
<Figure size 640x480 with 1 Axes>
print(trainx.shape)
print(trainy.shape)
print(testx.shape)
print(testy.shape)
(60000, 28, 28)
(60000,)
(10000, 28, 28)
(10000,)
# 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))
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
_________________________________________________________________