John
John

Reputation: 93

Recalling function: Tensor 'object' is not callable

Suppose I have a function named test as follows:

def test(X,W):
    ..do stuff
    return stuff

which I call using model = test(X,W).

When I call the function the first time, I do not get an error. But, if I call the function again, I get the error 'Tensor' object is not callable. Essentially the calling code looks like this:

model = test(X,W)
model1 = test(X,W)

and I get the error at the call for model1.

I would like to not need to redefine the function again before making another call to that function. After quite a while of researching this, I still have not found a solution.

How can I modify my function or calls to it in order to be able to recall the function?

Upvotes: 4

Views: 7075

Answers (1)

DavidG
DavidG

Reputation: 25363

I could see a situtation in which this happens if you name a variable the same as your function (within the "....more stuff here" section) meaning it would work the first time you call it, but would fail the second time. Take the following simplified example:

def test(x,y):
    global test
    test = x / 2 # random calculation
    return x + y

model = test(5,5)
model1 = test(10,10)

This would produce an error very similar to the one in the question:

Traceback (most recent call last):
  File "SO.py", line 43, in <module>
    mode2 = test(10,10)
TypeError: 'float' object is not callable

The solution would be to avoid naming variables the same as your functions.

Upvotes: 2

Related Questions