gpt4 book ai didi

python - 预期形状 (None, 8) 但得到形状为 (8,1) 的数组

转载 作者:太空狗 更新时间:2023-10-30 00:59:04 26 4
gpt4 key购买 nike

我有以下代码,

from keras.models import Sequential
from keras.layers import Dense
import numpy as np

# load dataset
dataset = np.loadtxt("data.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:, 0:8]
Y = dataset[:, 8]
# create model
model = Sequential()
model.add(Dense(8, activation="relu", input_dim=8, kernel_initializer="uniform"))
model.add(Dense(12, activation="relu", kernel_initializer="uniform"))
model.add(Dense(1, activation="sigmoid", kernel_initializer="uniform"))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=2)
# calculate predictions
test = np.array([6,148,72,35,0,33.6,0.627,50])
predictions = model.predict(test)
# round predictions
rounded = [round(x[0]) for x in predictions]
print(rounded)

当我运行该程序时,出现以下错误。

ValueError: Error when checking : expected dense_1_input to have shape (None, 8) but got array with shape (8,1)

我知道这个问题有很多重复,我已经尝试了所有这些,但它仍然给我同样的错误。我该如何解决?

最佳答案

尽管我们没有看到完整的错误跟踪,但我认为模型已经学习并且错误出现在以下行:

predictions = model.predict(test)

请确认。

预测失败,因为您应该始终向网络提供形状为 (number_of_samples_to_predict, input_shape) 的 numpy 数组。一开始总是有一个额外的维度,这是你堆积所有你想要预测的样本的地方。当只有一个样本时,你仍然需要输入一个[1, input_shape]数组。

要解决此问题,请像这样定义您的测试输入:

test = np.array([[6,148,72,35,0,33.6,0.627,50]])

现在测试的形状为 (1,8),它应该按照模型预期的方式运行 (?,8)

关于python - 预期形状 (None, 8) 但得到形状为 (8,1) 的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45499757/

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