RuntimeError:a leaf Variable that requires grad is being used is an in-place operation.

没有加with torch.no_grad()
RuntimeError:a leaf Variable that requires grad is being used is an in-place operation._第1张图片
RuntimeError:a leaf Variable that requires grad is being used is an in-place operation._第2张图片

with torch.no_grad()对于推断非常有用,如果确定不会调用,它将减少计算的内存消耗,否则会有requires_grad=True。
在这种模式下,每次计算的结果都将具有requires_grad=False,即使输入具有requires_grad=True。

RuntimeError:a leaf Variable that requires grad is being used is an in-place operation._第3张图片

你可能感兴趣的:(深度学习,python学习,pytorch,人工智能,python)