【问题标题】:ValueError: Error when checking input: expected conv2d_36_input to have shape (3, 32, 32) but got array with shape (1, 10, 10)ValueError:检查输入时出错:预期 conv2d_36_input 的形状为 (3, 32, 32) 但得到的数组的形状为 (1, 10, 10)
【发布时间】:2020-01-22 14:00:39
【问题描述】:

当我尝试运行我的训练模型时出现错误,我应该怎么做才能通过错误报告?

我的模型或重塑部分有什么问题吗?

这是我的重塑部分

# Reshape and normalize training data
trainX = train.reshape(train.shape[0], 1, 10, 10).astype( 'float32' )
x_train = trainX / 255.0
y_train = train[:,99]
# print(y_train)
# # # Reshape and normalize test data
testX = test.reshape(test.shape[0], 1, 10, 10).astype(     'float32' )
x_test = testX / 255.0
y_test = test[:,99]
# print(y_test)

这是我的模型:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 32, 32), 
activation='relu', padding='same'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(BatchNormalization())

然后如果我将我的数据重塑为 3*32*32 则出现值错误报告:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-31-36bf3e556ae8> in <module>()
----> 1 trainX = train.reshape(train.shape[0], 3, 32, 32).astype( 'float32' )
  2 x_train = trainX / 255.0
  3 y_train = train[:,10]
  4 # print(y_train)
  5 # # # Reshape and normalize test data

【问题讨论】:

    标签: python tensorflow keras training-data


    【解决方案1】:

    您的训练和测试数据集的输入形状与您的模型不同。使用 input_shape() 时,您需要记住所需输入的大小。在您的情况下,您的形状看起来像 (1,10,10),这是一个 10x10 图像,具有一级通道/深度(即黑白图像)。但是,您使用的模型需要 (3,32,32) 形状,该形状被转换为 32x32 尺寸的彩色图像(即 3 通道代表 RGB 颜色)。

    因此,如果您按照以下代码更改模型,它可能会起作用,但您还需要优化其他参数,例如 Conv2D 所需的特征图:

    model.add(Conv2D(32, (3, 3), **input_shape=(1, 10, 10)**,activation='relu', padding='same'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2020-02-21
      • 1970-01-01
      • 2020-04-11
      相关资源
      最近更新 更多