Keras,输入形状错误,但x.shape是对的

时间:2018-06-09 19:15:08

标签: python machine-learning keras

尝试在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);
        #perform the prediction
        out = model.predict(x)
        print(out)
        print(np.argmax(out,axis=1))
        print ("debug3")
        #convert the response to a string
        response = np.array_str(np.argmax(out,axis=1))
        return response 

1 个答案:

答案 0 :(得分:0)

Keras模型通常会隐藏批量大小,因此实际上它是(samples, 12),每个样本都有12个功能。在你的情况下,你会得到12个样本,每个样本都有一个特征;因此,它提供(1,)

您的数据是单个数据点,您需要创建2D数组或更改模型input_shape=(1,)