pytorch requires_grad

情况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
#A = A.detach()

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)
#print('A after detach:  ', A.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('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)

输出4

A:   True
B:   False
C:   False
Loss:   True

情况5

#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
Loss = Loss.detach()

#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)

输出5

A:   True
B:   False
C:   True
Loss:   False

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