尝试使用Python中的Keras设置神经网络。
当我尝试使用我的神经网络进行预测时,遇到了这个错误:
ValueError: Error when checking : expected input_1 to have shape (12,) but got array with shape (1,)
然而,当我print(x.shape)
时,返回的是(12,)。
这是代码块:
def predict(str): y = convert(str) x = data = np.array(y, dtype='int64') with graph.as_default(): print(x.shape); #进行预测 out = model.predict(x) print(out) print(np.argmax(out,axis=1)) print ("debug3") #将响应转换为字符串 response = np.array_str(np.argmax(out,axis=1)) return response
回答:
Keras模型通常隐藏批量大小,所以实际上它是(samples, 12)
,每个样本有12个特征。在你的情况下,发生了你有12个样本,每个样本有一个特征;因此,它输入的是(1,)
。
要么你的数据是一个单一的数据点,你需要创建一个二维数组,要么更改你的模型input_shape=(1,)
。