我的张量流有问题吗?

时间:2019-02-06 10:45:29

标签: python deep-learning

编写深度学习代码以识别数字0-9,经过几次训练后,在tensorflow / python上获得我的测试值的零读数。

我已经尝试测试其他值,但是我的矩阵返回0,我的输出值固定为零。

输出还显示:

  

I tensorflow / core / platform / cpu_feature_guard.cc:141]您的CPU支持此TensorFlow二进制文件未编译为使用的指令:AVX2。

真的不知道这意味着什么。

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])

该代码应该输出7,但我无法弄清楚它出了什么问题。

snapshot of code + output

1 个答案:

答案 0 :(得分:0)

不要在模型的第一层中使用flatten。第一层是密集层,最后一层是Relu。