我正在尝试在 pytorch 中学习反向传播,在那里我看到了这段代码:
def backward(ctx, grad_output):
"""
In the backward pass we receive a Tensor containing the gradient of the loss
with respect to the output, and we need to compute the gradient of the loss
with respect to the input.
"""
i, = ctx.saved_tensors
grad_output = 2*i
return grad_output
我不明白这里的i,
是什么。
最佳答案
Python 可以解包值,例如,如果我们有一个元组
t = (2,3,'wat')
我们可以像这样将它的值赋给变量
coolnumber, othernumber, word = t
这将导致 word
被设置为 'wat'
。
如果我们的元组长度为1,我们需要区分将整个元组分配给一个变量
a = (1,)
# a is now (1,)
解压那个值
a, = (1,)
# a is now 1
关于python - Python 中的逗号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71851255/