본문 바로가기

machine learning/pytorch3

pytorch의 forward 함수는 어떻게 실행될까 pytorch에서 nn.Module 을 기반으로 만들어진 모듈은 forward 함수를 호출 하지도 않았는데 모델 instance을 호출하는 것 만으로 forward가 실행된다. 왜 이럴까? python의 __init__ 은 모두가 알듯이 생성자 함수이다. 클래스의 instance가 생성되면 __init__가 호출된다. __call__은 호출자 함수인데 instance가 호출되면 __call__가 실행된다. python은 클래스 인스턴스도 함수 처럼 호출할 수 있다 class test_class(): def __init__(self, n1,n2,n3): print('called __init__') self.n1 = n1 self.n2 = n2 self.n3 = n3 print(n1, n2, n3) def .. 2022. 5. 9.
[pytorch] Variable -> tensor 로 통합 - 원래 Variable는 tensor의 warpper 였음. tensor는 history tracking 이 안되고 variable는 되었었는데 이제 tensor가 가능함 - linux 에서 돌아가던 code를 win10에서 돌리니 이런 에러발생 RuntimeError: Expected tensor for argument #1 ‘indices’ to have scalar type Long 선언된 Variable이 int.32 타입임 int.64로 바꿔줘야함. a= Variable(~~~) 이던것을 a = torch.tensor(~~~,dtype =torch.int64 ) 로 바꿔줌 2020. 8. 21.
[pytorch] list of tensors 의 평균구하기 my_list = [tensor(0.8223, device='cuda:0'), tensor(1.8351, device='cuda:0'), tensor(1.4888, device='cuda:0'),] np.mean(my_list) 하면, TypeError: mean(): argument 'input' (position 1) must be Tensor, not list 에러가 난다. 해결> mean = torch.mean(torch.stack(my_list)) 2020. 8. 21.