由于错误的环境设置而与Keras一起预测失败 [英] predict with Keras fails due to faulty environment setup

查看:52
本文介绍了由于错误的环境设置而与Keras一起预测失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让Keras预测任何事情.甚至在这种简约模型中也没有:

I can't get Keras to predict anything. Not even in this minimalistic model:

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)

一切按预期进行,直到最后一行:

Everything goes as expected until the last line:

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. 一些答案(例如此处)建议使用类似的

I tried over and over again with different combinations of arrays and lists, but either there is that TypeError or a ValueError, because the shape is wrong. Several answers (like here) suggest using something like

model.predict(np.array([[0,0,0]]))

但这对我也不起作用. 谁能告诉我该怎么做?

But this didn't work for me, either. Could anyone please tell me how to do this right?

显然,代码不是问题,请参阅下文.

Apparently, the code was not the problem, see below.

推荐答案

事实证明代码不是问题所在,但是我的软件出了点问题.执行以下步骤后,上面的代码将运行且没有错误或警告:

It turned out the code wasn't the problem, but there was something wrong with my software. After the following steps, the above code runs without errors or warnings:

  1. 卸载anaconda
  2. 安装anaconda
  3. 创建新环境
  4. 将所需的软件包安装到该环境中(keras,tensorflow, 间谍...)
  5. 在该环境中运行代码
  1. uninstall anaconda
  2. install anaconda
  3. create new environment
  4. install required packages into that environment (keras, tensorflow, spyder...)
  5. run code in that environment

这篇关于由于错误的环境设置而与Keras一起预测失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆