pytorch how to set .requires_grad False

前端 未结 5 1818
长发绾君心
长发绾君心 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:14

    This tutorial may help.

    In short words, I think a good way for this question could be:

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

提交回复
热议问题