当前位置: 代码迷 >> 综合 >> with torch.no_grad()或@torch.no_grad() 用法
  详细解决方案

with torch.no_grad()或@torch.no_grad() 用法

热度:68   发布时间:2024-01-31 04:07:46.0
  • requires_grad=True 要求计算梯度

  • requires_grad=False 不要求计算梯度

  • with torch.no_grad()或者@torch.no_grad()中的数据不需要计算梯度,也不会进行反向传播

model.eval()                                # 测试模式
with torch.no_grad():pass
@torch.no_grad()
def eval():...