调用功能:张量“对象"不可调用 [英] Recalling function: Tensor 'object' is not callable

查看:62
本文介绍了调用功能:张量“对象"不可调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 test 的函数,如下所示:

Suppose I have a function named test as follows:

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

我用 model = test(X,W)来称呼

.

当我第一次调用该函数时,没有出现错误.但是,如果我再次调用该函数,则会收到错误'Tensor'对象不可调用.实质上,调用代码如下所示:

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)

,在调用 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?

推荐答案

如果您将变量命名为与函数相同(在".... more此处"部分中),则可能会发生这种情况.表示它在您第一次调用时会起作用,但第二次会失败.以下面的简化示例为例:

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.

这篇关于调用功能:张量“对象"不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆