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.
The Python "TypeError: object is not callable" occurs when we try to call a not-callable object (e.g. a list or dict) as a function using parenthesis () . To solve the error, make sure to use square brackets when accessing a list at index or a dictionary's key, e.g. my_list[0] .
Tensors are multi-dimensional arrays with a uniform type (called a dtype ). You can see all supported dtypes at tf. dtypes. DType . If you're familiar with NumPy, tensors are (kind of) like np.
The “int object is not callable” error occurs when you declare a variable and name it with a built-in function name such as int() , sum() , max() , and others. The error also occurs when you don't specify an arithmetic operator while performing a mathematical operation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With