RuntimeError: grad can be implicitly created only for scalar outputs

2023. 1. 30. 22:40·Error Note
728x90
반응형
반응형

- 전체 에러 문구

RuntimeError: grad can be implicitly created only for scalar outputs

​

파이토치에서 모델 학습 중 backporpagation을 시도하는 도중에 발생하는 에러입니다. loss.backward()는 기본적으로 1개의 스칼라 데이터에 대해 backporpagation를 수행하도록 되어있습니다. 그러나, loss 변수 내에는 배치에 포함된 모든 데이터 각각에 대한 loss 가 저장되어 있습니다. 이 값들을 sum이나 mean을 통해 하나로 합쳐주어야 합니다. 

​

​

​

- 해결 방법

배치 안에 포함된 모든 데이터들에 대한 loss를 모아서 역전파를 수행해야하기 때문에, sum이나 mean을 통해서 값을 하나로 만들어 주면 됩니다. loss함수에 인자로 reduction='mean'또는 reduction='sum'을 추가하면 해결할 수 있습니다.

self.loss(logits, relation_labels.view([-1,1]).float(), alpha=0.75, reduction='mean')
 
728x90
반응형
저작자표시 (새창열림)

'Error Note' 카테고리의 다른 글

[Bug]Invalid SOS parameters for sequential JPEG  (0) 2023.02.05
RuntimeError: view size is not compatible with input tensor's size and stride ~  (0) 2023.01.30
RuntimeError: result type Float can't be cast to the desired output type Long  (0) 2023.01.30
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor,,,  (0) 2023.01.30
[Pytorch] RuntimeError: CUDA error: device-side assert triggered  (0) 2023.01.29
'Error Note' 카테고리의 다른 글
  • [Bug]Invalid SOS parameters for sequential JPEG
  • RuntimeError: view size is not compatible with input tensor's size and stride ~
  • RuntimeError: result type Float can't be cast to the desired output type Long
  • TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor,,,
ga.0_0.ga
ga.0_0.ga
    반응형
    250x250
  • ga.0_0.ga
    ##뚝딱뚝딱 딥러닝##
    ga.0_0.ga
  • 전체
    오늘
    어제
    • 분류 전체보기 (181)
      • Paper Review (51)
        • Video Scene Graph Generation (6)
        • Image Scene Graph Generation (18)
        • Graph Model (5)
        • Key Information Extraction (4)
        • Fake Detection (2)
        • Text to Image (1)
        • Diffusion Personalization (4)
        • etc (11)
      • AI Research (49)
        • Deep Learning (30)
        • Artificial Intelligence (15)
        • Data Analysis (4)
      • Pytorch (10)
      • ONNX (5)
      • OpenCV (2)
      • Error Note (34)
      • Linux (2)
      • Docker (3)
      • Etc (7)
      • My Study (16)
        • Algorithm (10)
        • Project (4)
        • Interview (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    permute
    linear regression
    contiguous
    활성화 함수
    i3d
    pandas
    dataloader
    정규화
    차원의 저주
    tensorflow
    그래프신경망
    GCN
    오차 역전파
    JNI
    forch.nn.functional
    알고리즘
    ONNX
    dataset
    transformer
    HRNet
    Logistic regression
    TypeError
    Activation Function
    pytorch
    Inductive bias
    나이브 베이즈 분류
    3dinput
    fine tuning
    torch.nn
    RuntimeError
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
ga.0_0.ga
RuntimeError: grad can be implicitly created only for scalar outputs
상단으로

티스토리툴바