1

Writing deep learning code to recognize numbers 0 - 9, getting zero readings for my test values on tensorflow/python after training several epochs.

I already tried testing other values but my matrix returns 0 and my output value is stuck on zero.

The output also says:

I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2.

Couldn't really figure out what this means.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist #handwritten numbers
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.keras.utils.normalize(x_train,axis=1)
x_test = tf.keras.utils.normalize(x_test,axis=1)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation = tf.nn.relu))

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

model.fit(x_train, y_train, epochs = 8)

val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)
# model.save("epic_num_reader.model")
# new_model = tf.keras.models.load_model("epic_num_reader.model")
predictions = model.predict([x_test])
print(predictions)
print(np.argmax(predictions[0]))

"""show image"""
plt.imshow(x_test[0])
plt.show()
# print(x_train[0])

The code is supposed to output 7, and I can't figure out what is wrong with it.

snapshot of code + output

Carlos Mermingas
  • 3,822
  • 2
  • 21
  • 40
Tochi Bedford
  • 354
  • 1
  • 9

1 Answers1

0

Don't use flatten in the the first layer of model. Go with a Dense layer for the first layer and drop Relu from the last layer.

TulakHord
  • 422
  • 7
  • 15
  • thanks, do you mean like this? **`model.add(tf.keras.layers.Dense(128))`** and like this **`model.add(tf.keras.layers.Dense(10, activation = tf.nn))`** for the last layer? – Tochi Bedford Feb 07 '19 at 12:44
  • Yes. Check prediction using this model. Flattening is a function that transforms the pooled feature map to a single column which is then passed to fully connected layer. So the input layer is not an appropriate position for a flatten function. If the accuracy is unsatisfactory try building a more complex model or increase epochs or both. – TulakHord Feb 07 '19 at 17:50
  • You can remove activation or set it to None if you want a linear activation in the last layer – TulakHord Feb 07 '19 at 18:06