tensorflow upgrade and testing | tensorflow 升级和测试

It has been over a year since I last used tensorflow. Not only library versions have changed, but also the syntax. My old jupyter notebook was throwing errors all over the place this morning. It is time to update everything.

For tensorflow, I am using Python 3.6.3 (I used to use Python 3.5), and numpy 1.16.1. TensorFlow has a few dependencies. numpy is one of them. Note the mnist dataset has 70,000 images.

python -c ‘import tensorflow as tf; print(tf.__version__)

pip show tensorflow

pip install numpy –upgrade

Below is my ‘testing code’ using minist dataset for digit classification.
import tensorflow as tf
import matplotlib.pyplot as plt
minist = tf.keras.datasets.minist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
print(x_train[0])
x_train.shape #(60000, 28, 28)
x_test.shape #(10000, 28, 28)
plt.imshow(x_train[0], cmap = plt.cm.binary)
#normalize numpy array
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)
plt.imshow(x_train[0], cmap = plt.cm.binary)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Faltten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
# output is 10 because of 10 digits
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(optimizer=’adam’, loss=’sparse_categorical_crossentropy’, metrics=[‘accuracy’])
model.fit(x_train, y_train,epochs=3)
#test
val_loss, val_acc = model.evaluate(x_test,y_test)
print(val_loss, val_acc)

Leave a Reply

Your email address will not be published.