KEYWORD: pytorch / Linear / Dot Product
nn.Linear 와 같은 형식으로 사용# Dot Product 와 Linear Transformation 을 적용한 결과
import numpy as np
A_matrix = [
[1.0, 1.0, 1.0, 1.0],
[2.0, 2.0, 2.0, 2.0]
]
B_matrix = [
[1.0, 1.0, 1.0],
[2.0, 2.0, 2.0],
[3.0, 3.0, 3.0],
[4.0, 4.0, 4.0]
]
print(f"numpy Dot Product : {np.dot(A_matrix, B_matrix)}")
import torch
import torch.nn as nn
B = nn.Linear(4, 3, bias=False)
B.weight.data = torch.tensor(B_matrix).T
A = torch.tensor(A_matrix)
print(f"A Linear Layer Weight {B.weight}")
print(f"torch Linear Output : {B(A)}")
numpy Dot Product : [[10. 10. 10.]
[20. 20. 20.]]
A Linear Layer Weight Parameter containing:
tensor([[1., 2., 3., 4.],
[1., 2., 3., 4.],
[1., 2., 3., 4.]], requires_grad=True)
torch Linear Output : tensor([[10., 10., 10.],
[20., 20., 20.]], grad_fn=<MmBackward0>)
import torch
import torch.nn as nn
linear = nn.Linear(in_features=4, out_features=3, bias=False)
# input_tensor 는 1x4 인 Tensor 이고, nn.Linear 은 4x3 인 Layer
import torch
import torch.nn as nn
linear = nn.Linear(in_features=4, out_features=3, bias=False)
input_tensor = torch.tensor([[1.0, 2.0, 3.0, 4.0]])
print(f"torch Linear Output : {linear(input_tensor)}")
torch Linear Output : tensor([[-0.6756, 0.7807, -0.7505]], grad_fn=<MmBackward0>)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x5 and 4x3)# torch.Size([1, 1, 1, 4])
input_tensor = torch.tensor([
[
[
[1.0, 2.0, 3.0, 4.0]
]
]
])
print(input_tensor.shape)
print(f"torch Linear Output : {linear(input_tensor)}")
torch.Size([1, 1, 1, 4])
torch Linear Output : tensor([[[[ 2.3263, -2.5094, 1.5137]]]], grad_fn=<UnsafeViewBackward0>)
nput_tensor = torch.tensor([
[
[
[
[
[1.0, 2.0, 3.0, 4.0]
]
]
]
]
])
print(input_tensor.shape)
print(f"torch Linear Output : {linear(input_tensor)}")
torch.Size([1, 1, 1, 1, 1, 4])
torch Linear Output : tensor([[[[[[ 2.3263, -2.5094, 1.5137]]]]]], grad_fn=<UnsafeViewBackward0>)
import torch
import torch.nn as nn
import torch.optim as optim
X = torch.tensor(list(range(10)), dtype=torch.float32).reshape(-1, 1)
y = X * 10 + 5
class my_model(nn.Module):
def __init__(self):
super(my_model, self).__init__()
self.linear = nn.Linear(1, 1, bias=True)
# self.relu = nn.relu()
def forward(self, x):
out = self.linear(x)
return out
model = my_model()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
for epoch in range(1000):
outputs = model(X)
optimizer.zero_grad()
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
model.linear.weight, model.linear.bias
(Parameter containing:
tensor([[10.0017]], requires_grad=True),
Parameter containing:
tensor([4.9891], requires_grad=True))
→ weight 는 10.0017 그리고 bias 는 4.9891 로 y = X * 10 + 5 인 Train Dataset 과 유사한 결과
import torch
import torch.nn as nn
linear = nn.Linear(in_features=10, out_features=1, bias=True)
print(linear.weight)
print(linear.bias)
Parameter containing:
tensor([[-0.0365, 0.1214, -0.2508, -0.2174, -0.1147, -0.0663, -0.2425, -0.2478,
0.2966, -0.2276]], requires_grad=True)
Parameter containing:
tensor([0.0310], requires_grad=True)
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
dataset = torch.tensor([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15],
], dtype=torch.float32)
loader = DataLoader(dataset, batch_size=3, shuffle=False)
tensor = next(iter(loader))
# tensor([[1., 2., 3.],
# [4., 5., 6.],
# [7., 8., 9.]])
linear = nn.Linear(3, 6)
linear(tensor)
# tensor([[-0.5625, -1.7144, -0.0318, 0.9240, 1.7183, -0.2757],
# [-1.1148, -4.9342, 0.2390, 2.6098, 5.4051, -1.6341],
# [-1.6672, -8.1539, 0.5098, 4.2956, 9.0919, -2.9925]],
# grad_fn=<AddmmBackward0>)
import torch
import torchvision
from torchvision import datasets
from torchvision import transforms
from torch.utils.data import DataLoader
trainset = datasets.MNIST(
root="/tmp/mnist",
train=True,
transform=transforms.ToTensor(),
download=True
)
# trainset 의 첫번째 Tensor
trainset[0][0].shape
# torch.Size([1, 28, 28])
# 첫번째 데이터의 1번 값은 Label 에 해당함.
trainset[0][1]
# 3
loader = DataLoader(trainset, batch_size=3, shuffle=False)
train, label = next(iter(loader))
train.shape
# torch.Size([3, 1, 28, 28])
linear = torch.nn.Linear(28, 100)
linear(train).shape
# torch.Size([3, 1, 28, 100])



nn.Module (소스코드)__init__()과 forward()를 overriding__init__() method에서 super().__init__()을 입력해 주어야 함super(class_name, self).__init__() : super().__init__()와 기능의 차이는 없으며, 단지 파생클래스를 명시함import torch
import torch.nn as nn
x = torch.FloatTensor(4, 3)
class MyLinear(nn.Module):
def __init__(self, input_dim=3, output_dim=2):
self.input_dim = input_dim
self.outpu_dim = output_dim
super().__init__()
self.W = torch.FloatTensor(input_dim, output_dim)
self.b = torch.FloatTensor(output_dim)
def forward(self, x):
y = torch.matmul(x, self.W)+self.b
return y
linear = MyLinear(3, 2)
y = linear(x)
nn.Module을 상속받은 객체는 __call__ 함수가 forward 함수와 mapping 되어 있어 foward 를 따로 호출할 필요가 없음__call__과 mapping 되어 있지 않아서 따로 호출해야 할 경우: y = linear.forward(x) class MyLinear(nn.Module):
def __init__(self, input_dim=3, output_dim=2):
self.input_dim = input_dim
self.outpu_dim = output_dim
super().__init__()
self.W = nn.Parameter(torch.FloatTensor(input_dim, output_dim))
self.b = nn.Parameter(torch.FloatTensor(output_dim))
def forward(self, x):
y = torch.matmul(x, self.W)+self.b
return y

torch.nn.Linear(in_features,out_features,bias = True, device = None,dtype = None)

class MyLinear(nn.Module):
def __init__(self, input_dim=3, output_dim=2):
self.input_dim = input_dim
self.outpu_dim = output_dim
super().__init__()
self.linear = nn.Linear(input_dim, output_dim)
def forward(self, x):
y = self.linear(x)
return y



x = x.cuda(device=1) 과 같이 tensor를 원하는 device에 복사
cpu = torch.device('cpu')
gpu = torch.device('cuda:0')
x = torch.cuda.FloatTensor(2, 2)
print(x)
x = x.to(gpu)
print(x)
x = x.to(cpu)
print(x)
tensor([[0.0000e+00, 1.0418e+03],
[0.0000e+00, 1.7157e-05]], device='cuda:0')
tensor([[0.0000e+00, 1.0418e+03],
[0.0000e+00, 1.7157e-05]], device='cuda:0')
tensor([[0.0000e+00, 1.0418e+03],
[0.0000e+00, 1.7157e-05]])
x = torch.cuda.FloatTensor(2, 2)
x.device
device(type='cuda', index=0)
layer = nn.Linear(2, 2)
next(layer.parameters()).device
device(type='cpu')