Recalling function: Tensor 'object' is not callable

ぐ巨炮叔叔 提交于 2020-07-18 21:08:16

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/48819022/recalling-function-tensor-object-is-not-callable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!