TypeError: close() missing 1 required positional argument: 'self'

TypeError: close() missing 1 required positional argument: 'self'

所以我尝试使用函数 InteractiveSession.close() 如果我这样做说:

TypeError: close() missing 1 required positional argument: 'self'

但是当我这样做的时候 InteractiveSession.close(self) 它给了我错误

NameError: name 'self' is not defined

我不知道该怎么办,有人能帮忙吗?提前致谢!!!

您应该在 InteractiveSession class 的 实例上调用 .close(),而不是 class 本身。

例如,如果您在变量 session 中有一个 InteractiveSession 的实例,您将写成 session.close().

另请注意,TF 开发人员在此处提供了一个有用的示例:https://www.tensorflow.org/api_docs/python/tf/compat/v1/InteractiveSession

这个问题反映了对 classes 在 Python 中如何工作的基本但常见的误解。有很多文档和指南可以帮助您解决这个问题,例如这是我刚刚通过 DDG 搜索找到的:https://medium.com/quick-code/understanding-self-in-python-a3704319e5f0