简体   繁体   English

ValueError:层顺序需要 1 个输入,但它收到 239 个输入张量

[英]ValueError: Layer sequential expects 1 input(s), but it received 239 input tensors

I am trying to test a simple convolutional layer where the input images (with 1 band= grayscale) are numpy arrays stored in a list and targets are stored in a pandas dataframe.我正在尝试测试一个简单的卷积层,其中输入图像(1 个波段 = 灰度)是存储在列表中的 numpy 数组,而目标存储在 Pandas 数据帧中。 The size of input images is 16x16.输入图像的大小为 16x16。 The output for the model.fit is an error of "Layer sequential expects 1 input(s), but it received 239 input tensors". model.fit 的输出是“层顺序需要 1 个输入,但它收到 239 个输入张量”的错误。 I also checked this link but still I couldn't find the answer.我也检查了这个链接,但我仍然找不到答案。 Can anyone help me to resolve this error?谁能帮我解决这个错误?

(trainY, testY, trainX, testX) = train_test_split(df, images, test_size=0.20, random_state=42)
print (np.shape(trainY),np.shape(testY),np.shape(trainX),np.shape(testX))

result: (239, 1) (60, 1) (239, 16, 16) (60, 16, 16)

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(16, 16, 1),name='Layer1'))
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu',name='layer2'))
model.add(layers.Dense(1,activation='linear',name='Layer3'))
model.summary()

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['mae'])

history = model.fit(trainX, trainY, epochs=10, 
                    validation_split=.2, batch_size=4)

The input expected by the model as defined in you model architecture is在您的模型架构中定义的模型预期的输入是

input_shape=(16, 16, 1)

So while training you can send a batch of 16X16 single channel ( X1 ) images.因此,在训练时,您可以发送一批16X16单通道 ( X1 ) 图像。 However your data shape is (239, 16, 16) .但是,您的数据形状是(239, 16, 16) ie you have a batch of 239 images of 16X16 .即您有一批 239 张16X16图像。 All you have to do is reshape 16X16 to 16X16X1 .您所要做的就是将16X16重塑为16X16X1 Since your data is in numpy array you can do this using expand_dim .由于您的数据位于 numpy 数组中,因此您可以使用expand_dim执行此操作。

trainX = np.expand_dims(trainX, -1)

Fixed code固定码

model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', 
                        input_shape=(16, 16, 1),name='Layer1'))
model.add(layers.Flatten())
model.add(layers.Dense(16, activation='relu',name='layer2'))
model.add(layers.Dense(1,activation='linear',name='Layer3'))

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['mae'])

trainY = np.random.randint(0,10, (239, 1))
trainX = np.random.randn(239, 16, 16)

trainX = np.expand_dims(trainX, -1)
history = model.fit(trainX, trainY, epochs=10, validation_split=.2, batch_size=4)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:“顺序”层需要 1 个输入,但它收到了 10 个输入张量 - ValueError: Layer "sequential" expects 1 input(s), but it received 10 input tensors ValueError:层顺序需要 1 个输入,但它接收到 250 个输入张量 - ValueError: Layer sequential expects 1 inputs, but it received 250 input tensors ValueError:层 sequential_20 需要 1 个输入,但它收到了 2 个输入张量 - ValueError: Layer sequential_20 expects 1 inputs, but it received 2 input tensors ValueError:层顺序_16 需要 1 个输入,但它收到 8 个输入张量 - ValueError: Layer sequential_16 expects 1 input(s), but it received 8 input tensors Python Keras Model -- ValueError: Layer sequential expects 1 input(s), but it received 16 input tensors - Python Keras Model -- ValueError: Layer sequential expects 1 input(s), but it received 16 input tensors Tensorflow & Keras 层“顺序”需要 1 个输入,但它接收到 2 个输入张量 - Tensorflow & Keras Layer "sequential" expects 1 input(s), but it received 2 input tensors ValueError:model 层需要 21 个输入,但它接收到 1 个输入张量 - ValueError: Layer model expects 21 input(s), but it received 1 input tensors ValueError:层鉴别器需要 1 个输入,但它收到 2 个输入张量 - ValueError: Layer Discriminator expects 1 input(s), but it received 2 input tensors 为什么在我拟合模型时代码会生成错误”ValueError:层“sequential_3”需要 1 个输入,但它接收到 2 个输入张量 - why may code generates the error while i fit the model" ValueError: Layer "sequential_3" expects 1 input(s), but it received 2 input tensors 使用 ValueError 构建自定义联合平均过程:层顺序需要 1 个输入,但它收到 3 个输入张量 - Build Custom Federated averaging process with ValueError: Layer sequential expects 1 inputs, but it received 3 input tensors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM