Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'Tensor' object is not callable

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.

like image 402
matchifang Avatar asked Jan 28 '17 16:01

matchifang


People also ask

How do I fix this object is not callable?

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] .

What is tensor object?

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.

Why is something not callable in Python?

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.


1 Answers

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.

like image 146
Marcin Możejko Avatar answered Nov 15 '22 07:11

Marcin Możejko