如何修复图像生成器中流返回值的尺寸?

2024-04-19 14:13:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用fit_generator。但我得到了错误

Error when checking input: expected sequential_1_input to have 3 dimensions, but got array with shape (20, 28, 28, 1)

代码如下:

data_flow = data_generator.flow(x_train, y_train,batch_size=20) 
generate = model.fit_generator(data_flow, steps_per_epoch=1400,epochs=10)

流中的每个批都有(20,28,28,1)的输出。但是fit_generator需要3个维度。既然它是一个产生元组的迭代器,我如何重塑流的返回函数呢。你知道吗


Tags: toinputdatahave错误trainerrorflow
1条回答
网友
1楼 · 发布于 2024-04-19 14:13:02

以下是一些可能的方法:

  • 如果可能,您可以将模型的InputShape更改为具有三个参数,如[28,28,1],以适合您的数据形状,而不是两个。

  • 在创建生成器之前,可以更改x_trainy_train的形状

x_train = tf.reshape(x_train, shape=[28,28]) # likewise for y_train
  • flow返回一个迭代器,因此可以使用map重塑其输出(未测试的代码)
# squeeze to remove the dimension with length 1
map(lambda x, y: tf.squeeze(x), y, data_flow) 
# otherwise one can use tf.reshape
map(lambda x, y: tf.reshape(x, shape=x.shape[0:-1]), y, data_flow) 

相关问题 更多 >