Question

Being new to theano, pls bear with me. I thought the shape of the tensor variable is already well defined out of the Conv2D layer since the input is specified, as follow,

from keras.layers import Input, Convolution2D
import theano
input_img = Input(shape=(1, 28, 28))
x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')   (input_img)
print type(x)
print theano.tensor.shape(x)

But the output is,

<class 'theano.tensor.var.TensorVariable'>
Shape.0

Since I'm taking the default stride of 1, and same border mode here means that padding is added so the output is the same as input. Using this information I could calculate by hand what the output shape should be.

Did I miss something here? The question is how to get the shape of the output of a convolution layer?

Was it helpful?

Solution

You can't get the shape of a theano tensor, because it is not fixed. The output of the convolutional layer is just a symbolic variable and its shape depends on whatever you put into the layer as input.

You can get the shape of the output for a specific input by making a theano function for the output of the layer, and feeding a numpy array through the function:

import numpy as np
input = np.ones(28*28).reshape(1, 1, 28, 28).astype('float32')
fn = theano.function([input_img], x)

print fn(input).shape
>>> (1, 16, 28, 28)
Licensed under: CC-BY-SA with attribution
Not affiliated with datascience.stackexchange
scroll top