定义NN架构的问题
我正在尝试使用Keras为CIFAR-10图像数据集(https://keras.io/datasets/)创建一个CNN,但即使它出现在Keras库中,我也无法使Flatten函数工作:{{ 3}}
以下是错误消息:
NameError Traceback (most recent call last)
<ipython-input-9-aabd6bce9082> in <module>()
12 nn.add(Conv2D(64, 3, 3, activation='relu'))
13 nn.add(MaxPooling2D(pool_size=(2, 2)))
---> 14 nn.add(Flatten())
15 nn.add(Dense(128, activation='relu'))
16 nn.add(Dense(10, activation='softmax'))
NameError: name 'Flatten' is not defined
我正在使用运行Python 2.7和Keras 1.1.1的Jupyter。以下是NN的代码:
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation
nn = Sequential()
nn.add(Conv2D(32, 3, 3, activation='relu', input_shape=(32, 32, 3)))
# Max-pool reduces the size of inputs, by taking the largest pixel-value from a grid
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Flatten())
nn.add(Dense(128, activation='relu'))
nn.add(Dense(10, activation='softmax'))
提前致谢,
-Johan B。
答案 0 :(得分:1)
首先尝试导入图层:
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.models import Sequential
from keras.layers import Dense, Activation, Flatten
nn = Sequential()
nn.add(Conv2D(32, 3, 3, activation='relu', input_shape=(32, 32, 3)))
# Max-pool reduces the size of inputs, by taking the largest pixel-value from a grid
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Conv2D(64, 3, 3, activation='relu'))
nn.add(MaxPooling2D(pool_size=(2, 2)))
nn.add(Flatten())
nn.add(Dense(128, activation='relu'))
nn.add(Dense(10, activation='softmax'))