LinearRegression

3.1.2 벡터 계산
#            
import torch
from time import time

a = torch.ones(1000)
b = torch.ones(1000)
# method 1
start = time()
c = torch.zeros(1000)
for i in range(1000):
    c[i] = a[i] + b[i]
print("%f sec" % (time() - start))
0.030923 sec
# method 2
start = time()
d = a + b
print("%f sec" % (time() - start))
0.000000 sec
# broadcast mechanism
a = torch.ones(3)
b = 10
print(a + b)
tensor([11., 11., 11.])

3.2 선형 회귀 0 에서 실현
# import packages
%matplotlib inline
import torch
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random

3.2.1 데이터 세트 생 성
num_inputs = 2  #    
num_examples = 1000  #    
true_w = [2, -3.4]  # weight
true_b = 4.2  # bias
features= torch.from_numpy(np.random.normal(0, 1, (num_examples, num_inputs)))
labels = true_w[0] * features[:, 0] + true_w[1] + features[:, 1] + b
labels += torch.from_numpy(np.random.normal(0, 0.01, size=labels.size()))
print(features[0], labels[0])
tensor([ 0.9043, -0.7762], dtype=torch.float64) tensor(7.6342, dtype=torch.float64)
#               
def use_svg_display():
    #         
    display.set_matplotlib_formats('svg')

def set_figsize(figsize=(3.5, 2.5)):
    use_svg_display()
    #       
    plt.rcParams['figure.figsize'] = figsize
    
set_figsize()
plt.scatter(features[:, 1].numpy(), labels.numpy(), 1)


[외부 체인 이미지 저장 에 실 패 했 습 니 다. 원본 사이트 에 도 난 방지 체인 메커니즘 이 있 을 수 있 습 니 다. 그림 을 저장 해서 바로 업로드 하 는 것 을 권장 합 니 다 (img - fIQGtnfo - 1581680924814) (output 10 1. svg)]
3.2.2 데이터 읽 기
#   batch_size           
def data_iter(batch_size, features, labels):
    num_examples = len(features) #    

    indices = list(range(num_examples))
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        j = torch.LongTensor(indices[i : min(i + batch_size, num_examples)])
        yield features.index_select(0, j), labels.index_select(0, j)
#    Tensor FloatTensor,LongTensor      long Tensor
# torch.index(input, dim, index, out=None)       input    
# input(Tensor) -      
# dim(int) -     ,0   ,       
# index(LongTensor) -          Tensor
batch_size = 10

for X, y in data_iter(batch_size, features, labels):
    print(X, y)
    break
tensor([[-0.8234,  2.4055],
        [ 2.1394, -1.4180],
        [ 0.8803,  0.9584],
        [ 0.2310, -1.4648],
        [-1.6114, -0.2770],
        [-0.1147, -0.3696],
        [ 0.3489,  1.0079],
        [-0.5462,  1.3363],
        [-1.5867,  0.6097],
        [-0.1388, -1.1018]], dtype=torch.float64) tensor([7.3576, 9.4671, 9.3204, 5.5955, 3.1126, 5.9952, 8.3144, 6.8455, 4.0234,
        5.2047], dtype=torch.float64)

3.2.3 모델 매개 변수 초기 화
#   、  
w = torch.tensor(np.random.normal(0, 0.01, (num_inputs, 1)), dtype= torch.float32)
b = torch.zeros(1, dtype=torch.float32)
w.requires_grad_(requires_grad=True)
b.requires_grad_(requires_grad=True)
tensor([0.], requires_grad=True)

3.2.4 정의 모델
# y = w * x + b
def linreg(X, w, b):
    return torch.mm(X, w.double()) + b

3.2.5 손실 함수 정의
# loss function
def squared_loss(y_hat, y):
    print(y_hat.size())
    print(y.size())
    return (y_hat - y.view(y_hat.size())) ** 2 / 2

3.2.6 정의 최적화 알고리즘
#          
def sgd(params, lr, batch_size):
    for param in params:
        param.data -= lr * param.grad / batch_size

3.2.7 훈련 모델
lr = 0.03 # learning rate
num_epochs = 3 #     
net = linreg 
loss = squared_loss
for epoch in range(num_epochs):
    for X, y in data_iter(batch_size, features, labels):
        l = loss(net(X, w, b), y).sum()
        l.backward()
        sgd([w, b], lr, batch_size)
        
        w.grad.data.zero_()
        b.grad.data.zero_()
    train_l = loss(net(features, w, b), labels)
    print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))
    # tensor.item()   tensor      , tensor   scalar;    
print(true_w, '
'
, w) print(true_b, '
'
, b)
[2, -3.4] 
 tensor([[1.9989],
        [0.9996]], requires_grad=True)
4.2 
 tensor([6.5995], requires_grad=True)

좋은 웹페이지 즐겨찾기