Reputation: 53
Problem defining the NN architecture
I'm trying to create a CNN with Keras for the CIFAR-10 image dataset (https://keras.io/datasets/), but I can't get the Flatten function to work even though it appears in the Keras library: https://keras.io/layers/core/#flatten
Here is the error message:
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
I'm using Jupyter running Python 2.7 and Keras 1.1.1. Below is the code for the 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'))
Thanks in advance,
-Johan B.
Upvotes: 4
Views: 21907
Reputation: 4519
try to import the layer first:
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'))
Upvotes: 5