pytorch how to set .requires_grad False

前端 未结 5 1828
长发绾君心
长发绾君心 2021-02-03 23:44

I want to set some of my model frozen. Following the official docs:

with torch.no_grad():
    linear = nn.Linear(1, 1         


        
5条回答
  •  庸人自扰
    2021-02-04 00:17

    Here is the way;

    linear = nn.Linear(1,1)
    
    for param in linear.parameters():
        param.requires_grad = False
    
    with torch.no_grad():
        linear.eval()
        print(linear.weight.requires_grad)
    

    OUTPUT: False

提交回复
热议问题