本文介绍了类型错误:添加的图层必须是类图层的实例.发现:<keras.layers.core.Dropout 对象在 0x000001622999A5F8>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
导入库和模型,
from __future__ import print_function
import keras
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
#from tensorflow.keras.layers import backend as k
batch_size = 128
num_classes = 10
epochs = 12
在书面代码下方,
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu", input_shape=(28, 28, 1) ))
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
在类型错误之下,我遇到了严重的问题,我无法解决,
Below the type error, which I badly faced and i can't make the solution,
TypeError Traceback (most recent call last)
<ipython-input-6-6c99a01e13d4> in <module>
7 model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
8
----> 9 model.add(Dropout(0.5))
10 model.add(Flatten())
TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dropout object at 0x000001622999A5F8>
现在,我应该如何解决此类错误?需要帮助,
推荐答案
使用 Keras 或 tensorflow.keras
,不要同时使用它们.
Use Keras or tensorflow.keras
, don't use both of them.
from __future__ import print_function
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import backend as k
batch_size = 128
num_classes = 10
epochs = 12
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu", input_shape=(28, 28, 1) ))
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
这篇关于类型错误:添加的图层必须是类图层的实例.发现:<keras.layers.core.Dropout 对象在 0x000001622999A5F8>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!