NLP lab/파이썬

NLP lab/파이썬

[pytorch] Batch 연산하면 값이 달라지는 이유

문제 상황 pytorch에서 텐서 연산 시 빠른 처리를 위해 batch로 연산하는 경우가 많다. 그런데 하나씩 하나씩 연산하는 것과, 배치로 한꺼번에 연산하면 결과값이 미세하게 달라지는 현상을 발견했다. 다음은 전체 코드이다. 코드 import torch from torch.nn import Linear, Module class NN(Module): def __init__(self): super().__init__() self.fc1 = Linear(64, 2) def forward(self, x): x = self.fc1(x) return x def main(): batch_size = 10 x = torch.rand((batch_size, 64)) nn = NN() # one by one l = []..

NLP lab/파이썬

[Solved] device-side assert triggered, Assertion `t >= 0 && t < n_classes` failed.

1. 에러 코드 RuntimeError: CUDA error: device-side assert triggered. For debugging consider passing CUDA_LAUNCH_BLOCKING=1. /opt/conda/conda-bld/pytorch_1656352657443/work/aten/src/ATen/native/cuda/Loss.cu:271: nll_loss_forward_reduce_cuda_kernel_2d: block: [0,0,0], thread: [0,0,0] Assertion `t >= 0 && t = 0 && t = 0 && t = 0 && t = 0 && t = 0 && t

NLP lab/파이썬

[numpy] + 연산과 += 연산의 차이점

0. 요약 import numpy as np a = np.array([1, 2]) print(id(a)) a += 1 print(id(a)) b = np.array([1, 2]) print(id(b)) b = b + 1 print(id(b)) 1906123730816 1906123730816 1906122615264 1906162448144 Process finished with exit code 0 위 코드를 실행 시 알 수 있는 것처럼, numpy 배열 연산 시 a += 1 a = a + 1 은 다르게 동작한다. 위의 코드(제자리 연산)는 변수 a의 주소를 바꾸지 않고, 아래의 코드는 a의 주소를 바꾼다. 이를 알지 못하면 함수에 매개변수로 전달 시 예상치 못한 결과를 가져올 수 있다. 1. 문제 인..

heavyteil
'NLP lab/파이썬' 카테고리의 글 목록