2

I'm trying to visualize the output of each convolutional layer in keras, following this link: MNIST Visualisation. I have modified some layers to remove errors, but now I'm stuck with the Dense Layer Error.

np.set_printoptions(precision=5, suppress=True)
np.random.seed(1337) # for reproducibility

nb_classes = 10

# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data("mnist.pkl")

X_train = X_train.reshape(X_train.shape[0], 1, 28, 28)
X_test = X_test.reshape(X_test.shape[0], 1, 28, 28)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

i = 4600
pl.imshow(X_train[i, 0], interpolation='nearest', cmap=cm.binary)
print("label : ", Y_train[i,:])

model = Sequential()

model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape = (1,28,28))) #changed border_mode from full -> valid
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(32, 32, 3))

convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(32*196, 128)) #ERROR HERE

Any comment or suggestion highly appreciated. Thank you.

1 Answer 1

2

If you check the documentation of a Dense layer then you'll notice that the first argument it accepts is the shape of output and second is init which describes the way how weights of the layer are initiated. In your case you provided the int as second positional argument and this caused error. You should change the code to (assuming that you want an output in a form of 128-dimensional vector):

model.add(Dense(128))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your comment Marcin, I changed it to model.add(Dense(128)), but I received "OverflowError: Range exceeds valid bounds" error. Do you perhaps know why?
the line that has the error. I changed it from model.add(Dense(32*196, 128)) to model.add(Dense(128))
Marcin, in addition to your code, I need to add "from keras import backend as K K.set_image_dim_ordering('th')" and change the order of input_shape = (28,28,1) instead of (1,28,28). So, your answer does work - Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.