【问题标题】:NameError: name 'input_shape' is not defined, while creating Sequential model in keras [closed]NameError:名称'input_shape'未定义,同时在keras中创建顺序模型[关闭]
【发布时间】:2020-10-14 06:24:31
【问题描述】:

我正在尝试在 Keras 中构建顺序模型,但是当我编译模型时,我收到此错误,我该如何解决?

# Step 3: Model Creation
model = Sequential()
model.add(Conv2D(32, (3,3), Input_shape(32, 32, 3),  padding = "same", activation = 'relu',
                 kernel_constraint = maxnorm(3)))
model.add(Dropout(0.2))
model.add(MaxPooling2D(pool_size=(2,2)))
 
model.add(Flatten())
model.add(Dense(512, activation = 'relu',kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(num_classes,activation='relu'))

#Configure the optimizer
sgd = SGD(lr = 0.01, momentum =0.9, decay = (0.01/25),nesterov = False)

#compile the model
model.compile(loss = 'categorical_crossentropy',optimizer = sgd, metrics = ['accuracy'])

这是我得到的错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-32-c2c434db6c57> in <module>()
      1 # Step 3: Model Creation
      2 model = Sequential()
----> 3 model.add(Conv2D(32, (3,3), Input_shape(32, 32, 3),  padding = "same", activation = 'relu',
      4                  kernel_constraint = maxnorm(3)))
      5 model.add(Dropout(0.2))

NameError: name 'Input_shape' is not defined

【问题讨论】:

  • 什么是Input_shape你导入了还是建了一些功能
  • 在代码的第 3 行
  • 我使用的是 cifar10 图像数据集,所以 input_shape 是输入图像的尺寸

标签: python tensorflow keras keras-layer sequential


【解决方案1】:

您缺少 = 符号:Input_shape = (32, 32, 3) 在 model.add() 的参数中的第 3 行

【讨论】:

  • 请不要回答错字问题,它们应该被标记/投票关闭并最终被删除。您现在有足够的声誉在问题下发表评论以帮助 OP 并将其标记为错字。回答问题,特别是如果它被赞成或被接受,会阻止自动删除过程并迫使许多用户参与清理网站。
  • 了解@ThierryLathuille。我不知道。
猜你喜欢
  • 2023-01-23
  • 2020-06-01
  • 2021-08-23
  • 2016-12-20
  • 2023-03-05
  • 2020-12-23
  • 2014-03-18
  • 2021-07-27
  • 1970-01-01
相关资源
最近更新 更多