Reputation: 5450
I'm trying to display the output of each layer of the convolutions neural network. The backend I'm using is TensorFlow. Here is the code:
import ....
from keras import backend as K
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape = (1,28,28)))
convout1 = Activation('relu')
model.add(convout1)
(X_train, y_train), (X_test, y_test) = mnist_dataset = mnist.load_data("mnist.pkl")
reshaped = X_train.reshape(X_train.shape[0], 1, X_train.shape[1], X_train.shape[2])
from random import randint
img_to_visualize = randint(0, len(X_train) - 1)
# Generate function to visualize first layer
# ERROR HERE
convout1_f = K.function([model.input(train=False)], convout1.get_output(train=False)) #ERROR HERE
convolutions = convout1_f(reshaped[img_to_visualize: img_to_visualize+1])
The full Error is:
convout1_f = K.function([model.input(train=False)], convout1.get_output(train=False)) TypeError: 'Tensor' object is not callable
Any comment or suggestion is highly appreciated. Thank you.
Upvotes: 9
Views: 52923
Reputation: 40516
Both get_output
and get_input
methods return either Theano
or TensorFlow
tensor. It's not callable because of the nature of this objects.
In order to compile a function you should provide only layer tensors and a special Keras tensor called learning_phase
which sets in which option your model should be called.
Following this answer your function should look like this:
convout1_f = K.function([model.input, K.learning_phase()], convout1.get_output)
Remember that you need to pass either True
or False
when calling your function in order to make your model computations in either learning or training phase mode.
Upvotes: 2