与 Keras 中输入张量数量相关的错误
Error related to the number of input tensors in Keras
我将一系列 float32 灰度图像作为具有 16*16
形状的列表输入到 python 并尝试使用从 Pandas 数据框输入的标签执行回归任务。
这里是图片的形状和 df:
print(np.shape(images))
(2000, 16, 16)
print(np.shape(df))
(2000, 1)
我使用了 sklearn
中的 train_test_split
来拆分数据进行训练和测试:
print (np.shape(trainY),np.shape(testY),np.shape(trainX),np.shape(testX))
(1700, 1) (300, 1) (1700, 16, 16) (300, 16, 16)
我正在使用以下模型进行预测,但是 model.fit
returns 错误并且没有 运行 训练。
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(16 * 16 * 1,)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
history = model.fit(trainX, trainY, epochs=50, validation_split=.2, batch_size=128,verbose=1)
ValueError: Layer sequential_18 expects 1 input(s), but it received 1700 input tensors
我也在model.fit
之前测试了trainX = np.expand_dims(trainX, -1)
,但它仍然给出另一个错误。谁能帮我解决这个问题?
ValueError: Input 0 of layer sequential_18 is incompatible with the layer: expected axis -1 of input shape to have value 256 but received input with shape (None, 16, 16, 1)
你的下一层只是 Dense
,所以在你的网络顶部添加一个 Flatten
层就可以完成这项工作(不需要额外操作输入图像)
trainX = np.random.uniform(0,1, (1700, 16, 16))
trainY = np.random.uniform(0,1, (1700, 1))
model = models.Sequential()
model.add(layers.Flatten(input_shape=(16,16)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
history = model.fit(trainX, trainY, epochs=50,
validation_split=.2, batch_size=128, verbose=1)
还要注意正确处理图像...
图像存储在数组列表中。您必须将列表转换为单个形状数组 (n_sample, 16, 16)
.
这可以简单地完成:
images = np.asarray(images)
我将一系列 float32 灰度图像作为具有 16*16
形状的列表输入到 python 并尝试使用从 Pandas 数据框输入的标签执行回归任务。
这里是图片的形状和 df:
print(np.shape(images))
(2000, 16, 16)
print(np.shape(df))
(2000, 1)
我使用了 sklearn
中的 train_test_split
来拆分数据进行训练和测试:
print (np.shape(trainY),np.shape(testY),np.shape(trainX),np.shape(testX))
(1700, 1) (300, 1) (1700, 16, 16) (300, 16, 16)
我正在使用以下模型进行预测,但是 model.fit
returns 错误并且没有 运行 训练。
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(16 * 16 * 1,)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
history = model.fit(trainX, trainY, epochs=50, validation_split=.2, batch_size=128,verbose=1)
ValueError: Layer sequential_18 expects 1 input(s), but it received 1700 input tensors
我也在model.fit
之前测试了trainX = np.expand_dims(trainX, -1)
,但它仍然给出另一个错误。谁能帮我解决这个问题?
ValueError: Input 0 of layer sequential_18 is incompatible with the layer: expected axis -1 of input shape to have value 256 but received input with shape (None, 16, 16, 1)
你的下一层只是 Dense
,所以在你的网络顶部添加一个 Flatten
层就可以完成这项工作(不需要额外操作输入图像)
trainX = np.random.uniform(0,1, (1700, 16, 16))
trainY = np.random.uniform(0,1, (1700, 1))
model = models.Sequential()
model.add(layers.Flatten(input_shape=(16,16)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(optimizer='adam',
loss='mean_squared_error',
metrics=['mae'])
history = model.fit(trainX, trainY, epochs=50,
validation_split=.2, batch_size=128, verbose=1)
还要注意正确处理图像...
图像存储在数组列表中。您必须将列表转换为单个形状数组 (n_sample, 16, 16)
.
这可以简单地完成:
images = np.asarray(images)