torch.nn.Module 신경망에서 초기가중치 변경하기

torch.nn.Module에서 초기가중치 변경
# Import Numpy & PyTorch
import numpy as np
import torch
 
# Imports
import torch.nn as nn
 
# Import tensor dataset & data loader
from torch.utils.data import TensorDataset, DataLoader
 
# Import nn.functional
import torch.nn.functional as F
 
# to view module
import inspect
 
# 입력
inputs = np.array([[3.0, 13], [4, 14], [5, 15]], dtype='float32') # 입력변수가 1
targets = np.array([[6.0, 26], [8, 28], [10, 30]], dtype='float32') # 타깃이 1
 
inputs = torch.from_numpy(inputs)
targets = torch.from_numpy(targets)
 
# Define dataset
train_ds = TensorDataset(inputs, targets)
print(train_ds[0:1])
 
# Define data loader
batch_size = 3
train_dl = DataLoader(train_ds, batch_size, shuffle=False)
 
# Draw data loader
for xb, yb in train_dl:
print("xb =", xb)
print("xb =", xb)
--------------------------------------
(tensor([[ 3., 13.]]), tensor([[ 6., 26.]]))
xb = tensor([[ 3., 13.],
[ 4., 14.],
[ 5., 15.]])
xb = tensor([[ 3., 13.],
[ 4., 14.],
[ 5., 15.]])
---------------------------------------

# 가중치 세팅
torch.manual_seed(777)
a = torch.randn(2,4).t()
a
---------------------------------------
tensor([[-0.4015, -0.9433],
[-0.5934, -0.6758],
[ 1.6885, 1.4425],
[ 0.5554, -1.4711]])
-----------------------------------
class SimpleNet(nn.Module):
    
    # Initialize the layers
    def __init__(self):
        super().__init__()  # 
        self.linear1 = nn.Linear(2, 4, bias=False)
        self.linear2 = nn.Linear(4, 2, bias=False)
        
        # 가중치를 변경할 경우 important !!! 이거 알려고 하루가 걸림 !!!
        with torch.no_grad():
            torch.manual_seed(777)
            a = torch.randn(2,4, requires_grad=True).t()
            b = torch.randn(4,2, requires_grad=True).t()
            self.linear1.weight.copy_(a)
            self.linear2.weight.copy_(b)
            
    # Perform the computation
    def forward(self, x):
        x = self.linear1(x)
        x = self.linear2(x)
        return x

model2 = SimpleNet()
model2(inputs)
 
# model2(inputs)와 동일한 기능
model2.forward(inputs)
 
# 모델, 옵티마이저, 비용함수
model4 = SimpleNet()
opt = torch.optim.SGD(model4.parameters(), 1e-3)
loss_fn = F.mse_loss
 
# Define a utility function to train the model
def fit(num_epochs, model, loss_fn, opt):
   for epoch in range(num_epochs):
   for xb, yb in train_dl:

   # Generate predictions
   pred = model(xb)
   loss = loss_fn(pred, yb)
   print(epoch, "loss =", loss)
 
   # Perform gradient descent
   loss.backward()
   opt.step() # 가중치를 update하는 것 (update rule에 의거)
   opt.zero_grad()  # 미분결과 축적을 방지하기 위해 초기화
 
fit(1, model4, loss_fn, opt)
-----------------------------------------------------
0 loss = tensor(2957.1760, grad_fn=<MseLossBackward>)


 

댓글