【问题标题】:TypeError: predict() takes from 2 to 5 positional arguments but 10 were givenTypeError: predict() 接受 2 到 5 个位置参数,但给出了 10 个
【发布时间】:2019-03-12 19:14:46
【问题描述】:

在 Keras 功能 API 上运行具有 9 个输入和 9 个输出的模型。
该模型非常适合,但我在运行时遇到此错误
model.predict

模型:(注意这只是两个输入,输出——我还有 7 个相似的层)

one= Input(shape=(9216,))
hidden1 = Dense(dense_one)(one)
hidden1 = BatchNormalization()(hidden1)
hidden1 = Activation('relu')(hidden1)
hidden1= Dropout(drop_out)(hidden1)
hidden1 = Dense(dense_two)(hidden1)
hidden1 = BatchNormalization()(hidden1)
hidden1 = Activation('relu')(hidden1)
hidden1= Dropout(drop_out)(hidden1)
hidden1 = Dense(dense_three)(hidden1)
hidden1 = BatchNormalization()(hidden1)
hidden1 = Activation('relu')(hidden1)
hidden1= Dropout(drop_out)(hidden1)
hidden1 = Dense(dense_four)(hidden1)
hidden1 = BatchNormalization()(hidden1)
hidden1 = Activation('relu')(hidden1)
hidden1= Dropout(drop_out)(hidden1)
output1 = Dense(500, activation='softmax')(hidden1) 

two= Input(shape=(9216,))
hidden2 = Dense(dense_one)(two)
hidden2 = BatchNormalization()(hidden2)
hidden2 = Activation('relu')(hidden2)
hidden2= Dropout(drop_out)(hidden2)
hidden2 = Dense(dense_two)(hidden2)
hidden2 = BatchNormalization()(hidden2)
hidden2 = Activation('relu')(hidden2)
hidden2= Dropout(drop_out)(hidden2)
hidden2 = Dense(dense_three)(hidden2)
hidden2 = BatchNormalization()(hidden2)
hidden2 = Activation('relu')(hidden2)
hidden2= Dropout(drop_out)(hidden2)
hidden2 = Dense(dense_four)(hidden2)
hidden2 = BatchNormalization()(hidden2)
hidden2 = Activation('relu')(hidden2)
hidden2= Dropout(drop_out)(hidden2)
output2 = Dense(500, activation='softmax')(hidden2) 

model = Model(inputs=[one, two...],
              outputs=[output1, output2, output3,output4, output5, output6, output7,output8, output9])

这是我的拟合函数:

history = model.fit(x=[train1,train2,train3,train4,train5,train6,train7,train8,train9], 
          y=[y1,y2,y3,y4,y5,y6,y7,y8,y9], callbacks=callbacks,
          batch_size=40, epochs=50, verbose=1, validation_split=0.1, shuffle=False)  

它运行完美,我什至可以绘制历史。

那我就跑了:

model.predict(train1[1],train1[2],train1[3],train1[4],train1[5],train1[6],train1[7],train1[8],train1[9])  

并得到上述错误。
我检查了每个输入的形状与模型可以接受的形状相似(每个 train1[x] 具有相同的形状)

编辑:
我试着跑了

model.predict([train1[1],train1[2],train1[3],train1[4],train1[5],train1[6],train1[7],train1[8],train1[9]])   

并得到以下错误:

 ValueError: Error when checking input: expected input_1 to have shape (9216,) but got array with shape (1,)  

我也试过跑步:

model.predict(train1[1:9])  

得到了

ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 9 array(s), but instead got the following list of 8 arrays: [array([[255.]... –

我也试过跑步

model.predict(train1[1:10])

得到了

ValueError: Error when checking input: expected input_1 to have shape (9216,) but got array with shape (1,)

【问题讨论】:

  • 您正在拟合 9 个示例,从 train1 到 train9,但您尝试在 train1[0] 上进行预测。你确定这不是一个错误?
  • 不确定如何检查是否是错误
  • 你能分享你的代码示例吗?很难理解模型的真正输入是什么
  • 刚刚添加了示例代码

标签: machine-learning keras data-science predict


【解决方案1】:

我的理解是,你有一个模型,它是由 9 个其他模型组成的图。所以你有 9 个输入和 9 个输出。每个输入似乎都是维度 (9216,)。

当您拟合模型时,train1,train2,...,train9 是这 9 个模型中每一个模型的输入。所以在预测中,你有相同数量的输入是有道理的,我的意思是 train1,train2,...,train9。 当您尝试 train1[0] 时,就像您只想获取大小为 (9216,) 的张量的一个元素,这是不正确的

【讨论】:

  • 您对模型的理解正确。我对这个解释有点困惑。让我澄清一下,每一个 train1,train2...train9 都是一个大小为 (9216,) 的数组列表——>类似于在一个图像数组上训练模型的方式(假设你有 100 个图像,你会将它们放在一个名为 X_train 的数组中)。但是,当您预测一张新图像时,您不会输入相同大小 (100) 的列表——您输入的是一张图像。这就是为什么 train1[0], train1[1]... 是 9 个输入,每个输入的大小为 (9216,)。
  • 我猜线性代数有一些混淆。您的数据来自 n 的维度。对于大小为 10*10 的图像,在 RGB 模式下,使用平面图像,您将拥有每个图像作为大小为 10*10*3=300 的矢量。那么你给模型的东西是300的形状。当你定义模型时,你只会提到维度,而不是训练样本的计数。在训练中,如果你有 20 张图像,那么你对模型的输入将是 20*300 张量的形式。并且在测试中,如果你只有一张图片,你会发送一个 1*300 的张量给模型进行预测。
  • 没错!我正试图这样做。唯一的区别是我有 9 组图像(大小为 9216 而不是 300——你可以看到我只给出了那个尺寸)。我仍然不太明白如何发送“1*300 张量”或在我的情况下为 9*9216 张量,因为我有 9 个输入
  • 但是 train1 的维度是多少?如果它的形状像 20*9216,您将看不到上述错误。你可以制作一个 train1.shape 来检查一下吗?
  • train1 是一个长度为 992(992 张图片)的列表,每张图片都在一个大小为 (9216,) 的数组中
猜你喜欢
  • 2019-04-29
  • 2021-05-20
  • 1970-01-01
  • 2021-02-02
  • 2018-06-21
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
相关资源
最近更新 更多