pytorch 中 with torch.no_grad() 的作用域
Scope of with torch.no_grad() in pytorch
with torch.no_grad():
input = Variable(input).cuda()
target = Variable(target).cuda(non_blocking=True)
y=model(input)
# many things here
no_grad 是否在 "with" 范围之外继续生效?
no_grad
在 "with" 范围外无效。
根据来自 pytorch 博客版主的answer:
with torch.no_grad():
# No gradients in this block
x = self.cnn(x)
# Gradients as usual outside of it
x = self.lstm(x)
这是python中with
语句的目的。 with
(此处为 torch.no_grad()
)使用的变量仅在 with
上下文中有效,之后无效。有关完整详细信息,请参阅 python doc。
with torch.no_grad():
input = Variable(input).cuda()
target = Variable(target).cuda(non_blocking=True)
y=model(input)
# many things here
no_grad 是否在 "with" 范围之外继续生效?
no_grad
在 "with" 范围外无效。
根据来自 pytorch 博客版主的answer:
with torch.no_grad():
# No gradients in this block
x = self.cnn(x)
# Gradients as usual outside of it
x = self.lstm(x)
这是python中with
语句的目的。 with
(此处为 torch.no_grad()
)使用的变量仅在 with
上下文中有效,之后无效。有关完整详细信息,请参阅 python doc。