【发布时间】:2019-07-28 19:02:26
【问题描述】:
当我启动简单的神经网络时,我遇到了一个错误。顺便说一句,代码应该输出测试数组的第一个数字。
还有其他错误(与数据的 dtype 有关)。
import tensorflow as tf
import numpy as np
from tensorflow import keras
data = np.array([[0, 1, 1], [0, 0, 1], [1, 1, 1]])
labels = np.array([0, 0, 1])
data.dtype = float
print(data.dtype)
model = keras.Sequential([
keras.layers.Dense(3, activation=tf.nn.relu),
keras.layers.Dense(2, activation=tf.nn.softmax)])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(data, labels)
prediction = model.predict([0, 1, 0])
print(prediction)
我收到此错误:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [3,1], In[1]: [3,3]
[[{{node sequential/dense/Relu}}]]
【问题讨论】:
-
嘿,
the code should output the first number of the test array.是什么意思? -
如果是 [0, 0, 1] 它应该输出 . 0
-
我不明白。这不是你从神经网络中得到的输出。在第二个密集层中有
2,这意味着您将获得 2 个值作为输出。按照我下面的回答修改上面的代码后自己检查结果。 -
顺便说一句,
[0, 0, 1]中的每个元素都是不同数据特征的标签。对于上述情况,0 表示 [0, 1, 1],0 表示 [0, 0, 1],1 表示 [1, 1, 1]。
标签: python tensorflow keras