TypeError:添加的层必须是类Layer的实例。在构建 keras 分类器时

问题描述 投票:0回答:1

我正在尝试使用 keras 构建二元分类模型,对图像进行分类。 我试图建立一个模型,但我遇到了问题。我写了这段代码:

from tensorflow.keras.models import Sequential
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

model = Sequential()
model.add(Conv2D(32,3,3, input_shape = (64,64,3), activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten)
model.add(Dense(output_dim = 128, activation= 'relu'))
model.add(Dense(output_dim = 1, activatiocn = 'sigmoid'))
model.compile(optimizer='adam', loss= 'binary_crossentropy', metrics= ['accuracy'])

我遇到了这个错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_22380\391641818.py in <module>
      2 model.add(Conv2D(32,3,3, input_shape = (64,64,3), activation = 'relu'))
      3 model.add(MaxPooling2D(pool_size=(2,2)))
----> 4 model.add(Flatten)
      5 model.add(Dense(output_dim = 128, activation= 'relu'))
      6 model.add(Dense(output_dim = 1, activatiocn = 'sigmoid'))

c:\Users\vbuck\anaconda3\envs\py37\lib\site-packages\tensorflow\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

c:\Users\vbuck\anaconda3\envs\py37\lib\site-packages\tensorflow\python\keras\engine\sequential.py in add(self, layer)
    180       raise TypeError('The added layer must be '
    181                       'an instance of class Layer. '
--> 182                       'Found: ' + str(layer))
    183 
    184     tf_utils.assert_no_legacy_layers([layer])

TypeError: The added layer must be an instance of class Layer. Found: <class 'tensorflow.python.keras.layers.core.Flatten'>

请告诉我我的代码有什么问题?

python python-3.x python-3.7 tf.keras keras-layer
1个回答
0
投票

Flatten 中缺少括号:

model.add(展平())

© www.soinside.com 2019 - 2024. All rights reserved.