情况1
import torch
default_requires_grad = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float)
A = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float, requires_grad = True)
B = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float,requires_grad = False)
C = A+B
Loss=C**2
print('default_requires_grad: ',default_requires_grad.requires_grad)
print('A: ', A.requires_grad)
print('B: ',B.requires_grad)
print('C: ',C.requires_grad)
print('Loss: ',Loss.requires_grad)
输出1:
default_requires_grad: False
A: True
B: False
C: True
Loss: True
情况2
A = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float, requires_grad = True)
B = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float,requires_grad = False)
C = A+B
Loss=C**2
A.detach()
print('A: ', A.requires_grad)
print('B: ',B.requires_grad)
print('C: ',C.requires_grad)
print('Loss: ',Loss.requires_grad)
输出2
A: True
B: False
C: True
Loss: True
情况3
A = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float, requires_grad = True)
B = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float,requires_grad = False)
C = A+B
Loss=C**2
A = A.detach()
print('A: ', A.requires_grad)
print('B: ',B.requires_grad)
print('C: ',C.requires_grad)
print('Loss: ',Loss.requires_grad)
输出3
A: False
B: False
C: True
Loss: True
情况4
A = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float, requires_grad = True)
B = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float,requires_grad = False)
C = A+B
Loss=C**2
C = C.detach()
print('A: ', A.requires_grad)
print('B: ',B.requires_grad)
print('C: ',C.requires_grad)
print('Loss: ',Loss.requires_grad)
输出4
A: True
B: False
C: False
Loss: True
情况5
A = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float, requires_grad = True)
B = torch.tensor([1.,2.,0.,0,-1,3],dtype=torch.float,requires_grad = False)
C = A+B
Loss=C**2
Loss = Loss.detach()
print('A: ', A.requires_grad)
print('B: ',B.requires_grad)
print('C: ',C.requires_grad)
print('Loss: ',Loss.requires_grad)
输出5
A: True
B: False
C: True
Loss: False