为什么keras不允许以这种方式添加卷积层?
作者:互联网
以下代码
from tensorflow import keras
from keras.layers import Conv2D
model = keras.Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))
执行时抛出错误:
TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.convolutional.Conv2D object at 0x7fea0c002d10>
我也尝试使用Convolutional2D,但出现了相同的错误.为什么?
解决方法:
尝试这个:
from tensorflow import keras
from tensorflow.keras.layers import Conv2D
model = keras.Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))
您正在将tf.keras顺序模型与keras Conv2D层(而不是tf.keras Conv2D层)混合.
或者,如下所述,使用实际的Keras:
import keras
from keras.models import Sequential
from keras.layers import Conv2D
model = Sequential()
model.add(Conv2D(1, (3, 3), padding='same', input_shape=(28, 28, 1)))
标签:python,tensorflow,keras,conv-neural-network 来源: https://codeday.me/bug/20191010/1883509.html