符号 self(x) 的作用是什么?
What does the notation self(x) do?
我正在从 'Deep Reinforcement Learning Hands On' code 学习分布式强化学习。模型class中有一个方法:
def both(self, x):
cat_out = self(x)
probs = self.apply_softmax(cat_out)
weights = probs * self.supports
res = weights.sum(dim=2)
return cat_out, res
那个 self(x) do/mean 是什么?
它将在实例上调用 __call__
方法。请参阅此演示:
class A:
def __call__(self, x):
print("called instance")
return x + 3
def both(self, x):
val = self(x)
print("value:", val)
a = A()
a.both(5)
print()
a(123)
a.__call__(123)
Output:
called instance
value: 8
called instance
called instance
val = self(x)
与val = self.__call__(x)
相同
我正在从 'Deep Reinforcement Learning Hands On' code 学习分布式强化学习。模型class中有一个方法:
def both(self, x):
cat_out = self(x)
probs = self.apply_softmax(cat_out)
weights = probs * self.supports
res = weights.sum(dim=2)
return cat_out, res
那个 self(x) do/mean 是什么?
它将在实例上调用 __call__
方法。请参阅此演示:
class A:
def __call__(self, x):
print("called instance")
return x + 3
def both(self, x):
val = self(x)
print("value:", val)
a = A()
a.both(5)
print()
a(123)
a.__call__(123)
Output:
called instance value: 8 called instance called instance
val = self(x)
与val = self.__call__(x)
相同