【问题标题】:Adding fully connected layers to CNN [closed]将全连接层添加到 CNN [关闭]
【发布时间】:2020-07-07 14:08:53
【问题描述】:

我想在这个 CNN 架构中添加一个全局平均池化层,然后是几个全连接层:

img_input = layers.Input(shape=(img_size, img_size, 1))
x = layers.Conv2D(16, (3,3), activation='relu', strides = 1, padding = 'same')(img_input)
x = layers.MaxPool2D(pool_size=2)(x)
x = layers.Conv2D(32, (3,3), activation='relu', strides = 2)(x)
x = layers.MaxPool2D(pool_size=2)(x)
x = layers.Conv2D(64, (3,3), activation='relu', strides = 2)(x)
x = layers.MaxPool2D(pool_size=2)(x)
x = layers.Conv2D(3, 5, activation='relu', strides = 2)(x)

x = layers.Dense(200,activation='relu')
x = layers.Dropout(0.1)

output = layers.Flatten()(x)

model = Model(img_input, output)
model.summary()

但是每当我尝试在 las Conv2D 层之后添加一个全连接层时,我都会收到以下错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-370-1cf54963b964> in <module>
     11 x = layers.Dropout(0.1)
     12 
---> 13 output = layers.Flatten()(x)
     14 
     15 model = Model(img_input, output)

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    885         # Eager execution on data tensors.
    886         with backend.name_scope(self._name_scope()):
--> 887           self._maybe_build(inputs)
    888           cast_inputs = self._maybe_cast_inputs(inputs)
    889           with base_layer_utils.autocast_context_manager(

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2120     if not self.built:
   2121       input_spec.assert_input_compatibility(
-> 2122           self.input_spec, inputs, self.name)
   2123       input_list = nest.flatten(inputs)
   2124       if input_list and self._dtype_policy.compute_dtype is None:

/usr/local/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    161         spec.min_ndim is not None or
    162         spec.max_ndim is not None):
--> 163       if x.shape.ndims is None:
    164         raise ValueError('Input ' + str(input_index) + ' of layer ' +
    165                          layer_name + ' is incompatible with the layer: '

AttributeError: 'Dropout' object has no attribute 'shape'

我的数据集如下所示:

print(X_train.shape, X_test.shape, Y_train.shape, Y_test.shape)
(1600, 200, 200, 1) (400, 200, 200, 1) (1600, 3) (400, 3)

我在这里错过了什么?

【问题讨论】:

  • 你忘了给Dropout打电话

标签: python machine-learning keras deep-learning neural-network


【解决方案1】:

当您使用函数式 API 时,您想使用:

x = layers.Dense(200, activation='relu')(x)
x = layers.Dropout(0.1)(x)

【讨论】:

  • 谢谢!效果很好。
  • 以及如何在最后一个 Conv2D 层之后添加全局平均池化层?
  • 只需在Conv2D层后添加x = GlobalAveragePooling2D()(x)即可,最后不需要Flatten
猜你喜欢
  • 2017-12-14
  • 1970-01-01
  • 2018-03-26
  • 1970-01-01
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2013-05-31
相关资源
最近更新 更多