gpt4 book ai didi

python - Keras,输入形状错误,但 x.shape 是正确的

转载 作者:行者123 更新时间:2023-11-30 09:17:40 24 4
gpt4 key购买 nike

尝试在 python 中使用 Keras 建立神经网络。

尝试使用神经网络进行预测时出现此错误:

ValueError:检查时出错:预期 input_1 具有形状 (12,),但得到的数组具有形状 (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

最佳答案

Keras 模型经常隐藏批量大小,因此实际上它是(samples, 12),每个样本有 12 个特征。在你的例子中,你有 12 个样本,每个样本都有一个特征;因此,它提供(1,)

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

关于python - Keras,输入形状错误,但 x.shape 是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50777658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com