목록pytorch (16)
rueki
import torch x = torch.tensor(2.0, requires_grad=True) #requires_grad -> 텐서 연산 추적 y = 2*x**4 + x**3 + 3*x**2 + 5*x + 1 print(y) # tensor(63., grad_fn=) # y의 방정식에 2.0이 들어가서 연산된 값 출력 y.backward() #y에 대한 역전파 수행 x.grad #기울기 출력 x = torch.tensor([[1.,2.,3.],[3.,2.,1.]],requires_grad=True) y = 3*x + 2 print(y) ''' tensor([[ 5., 8., 11.], [11., 8., 5.]], grad_fn=) ''' z = 2*y**2 ''' tensor([[ 50., 128.,..
import numpy as np import torch x = torch.arange(6).reshape(3,2) x ''' tensor([[0, 1], [2, 3], [4, 5]]) ''' x[:,1] # tensor([1, 3, 5]), 1번째 열 원소만 슬라이싱 x = torch.arange(10) x # tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # view 함수 x.view(2,5) ''' tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) ''' # reshape 함수 x.reshape(2,5) ''' tensor([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]) ''' a = torch.tensor([1.,2.,3.]) b=..
import torch import numpy new_arr = np.array([1,2,3]) new_arr.dtype # dtype('int32') torch.tensor(new_arr) # tensor([1, 2, 3], dtype=torch.int32) my_tensor = torch.FloatTensor(new_arr) my_tensor.dtype # torch.float32 # 빈 행렬 생성 torch.empty(2,2) ''' tensor([[0.0000e+00, 2.8026e-45], [7.0295e+28, 6.1949e-04]]) ''' #영행렬 생성 torch.zeros(4,3, dtype = torch.int64) ''' tensor([[0, 0, 0], [0, 0, 0], [0, 0..
import torch # 파이토치 import numpy as np # 행렬 연산 위함 # 배열 생성 arr = np.array([1,2,3,4,5]) # [1 2 3 4 5] arr.dtype # 배열 타입 확인 # dtype('int32') #토치를 이용한 배열 생성 x = torch.from_numpy(arr) x # tensor([1, 2, 3, 4, 5], dtype=torch.int32) torch.as_tensor(arr) # tensor([1, 2, 3, 4, 5], dtype=torch.int32) #2차원 배열 생성 arr2d = np.arange(0.0,12.0) # 0부터 12까지의 수로 배열 생성 x2 = torch.from_numpy(arr2d) print(x2) ''' ten..