我不能让Keras预测任何事情。甚至在这个简约模型中:
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
inDim = 3
outDim = 1
model = Sequential()
model.add(Dense(5, input_dim=inDim, activation='relu'))
model.add(Dense(outDim, activation='sigmoid'))
model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])
test_input = np.zeros((1,inDim))
test_output = np.zeros((1,outDim))
model.fit(test_input, test_output)
prediction = model.predict(test_input)
一切都按预期进行,直到最后一行:
Epoch 1/1
1/1 [==============================] - 0s 448ms/step - loss: 0.2500 - acc: 1.0000
Traceback (most recent call last):
File "<ipython-input-24-ee244a6c7287>", line 16, in <module>
prediction = model.predict(test_input)
File "E:\Programme\Anaconda3\lib\site-packages\keras\engine\training.py", line 1172, in predict
steps=steps)
File "E:\Programme\Anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 304, in predict_loop
outs.append(np.zeros(shape, dtype=batch_out.dtype))
TypeError: data type not understood
我一遍又一遍地尝试使用不同的数组和列表组合,但是存在TypeError或ValueError,因为形状是错误的。几个答案(如here)建议使用类似的东西
model.predict(np.array([[0,0,0]]))
但这对我来说也不起作用。有人可以告诉我该怎么做对吗?
编辑:显然,代码不是问题,见下文。
事实证明代码不是问题,但我的软件出了问题。执行以下步骤后,上面的代码运行时没有错误或警告:
我把你的代码粘贴到https://colab.research.google.com,它没有给我一个错误。 (python2)
然而,我确实收到了关于int到float转换的警告。
我会尝试明确指定test_input dtype,如下所示:
test_input = np.zeros((1,inDim), dtype=float)
因为这似乎是正在输出的错误消息。
我再次卸载并安装了Anaconda Navigator并修复了它。