[英]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)
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.