pytorch -----.데이터와.detach(),

1117 단어 pytorch
0.4.0 버전 이전에는..데이터의 의미는Variable의 내부 Tensor를 가져오는 것입니다. 0.4.0버전에서Variable와Tensor merge를 가져오면.데이터는 이전과 유사한 의미를 가지며 내부의Tensor 개념이기도 하다.
x.data와 x.detach()가 되돌아오는 tensor는 같은 부분도 있고 다른 부분도 있다.
동일:
  • 모두 x와 동일한 데이터 공유
  • 모두 x의 계산 역사와 무관하다
  • requires_grad = False

  • 다름:
  • y=x.data는 어떤 상황에서 안전하지 않습니다.
    import torch
    x = torch.FloatTensor([[1., 2.]])
    w1 = torch.FloatTensor([[2.], [1.]])
    w2 = torch.FloatTensor([3.])
    w1.requires_grad = True
    w2.requires_grad = True
    
    d = torch.matmul(x, w1)
    
    d_ = d.data
    
    f = torch.matmul(d, w2)
    d_[:] = 1
    
    f.backward()
    
    #  ,  
    #     w2.grad  ,     1,   4.
    상기 코드는 다음과 같이 잘못 보고해야 합니다.
  • d_ d와 동일한 데이터 공유
  • 개dd를 바꾼 셈이다
  • 따라서release note에서detach의 효과를 원하면detach()가 안전하다고 지적했다.
  • 그러나 코드는 틀리지 않았지만 계산상 틀렸다
  • import torch
    x = torch.FloatTensor([[1., 2.]])
    w1 = torch.FloatTensor([[2.], [1.]])
    w2 = torch.FloatTensor([3.])
    w1.requires_grad = True
    w2.requires_grad = True
    
    d = torch.matmul(x, w1)
    
    d_ = d.detach() #   .detach(),    ...
    
    f = torch.matmul(d, w2)
    d_[:] = 1
    f.backward()

    좋은 웹페이지 즐겨찾기