PytorchTask_2

numpy实现线性回归

import numpy as np 
x_data = np.array([1, 2, 3])
y_data = np.array([2, 4, 6])

epochs = 10
lr = 0.01
w = 0
cost = []

for epoch in range(epochs):
	yhat = x_data * w
	loss = np.average((yhat - y_data)**2)
	cost.append(loss)
	dw = -2*(y_data - yhat) @ x_data.T/(x_data.shape[0])
	w = w - lr*dw
print(w)

在这里插入图片描述

pytorch实现线性回归

import torch

num_samples = 64 # N
dim_in, dim_hid, dim_out = 1000, 100, 10  # IN H OUT
x = torch.randn(num_samples, dim_in)  #  N * IN
y = torch.randn(num_samples, dim_out) #  N * OUT

w1 = torch.randn(dim_in, dim_hid)     # IN * H
w2 = torch.randn(dim_hid, dim_out)    #  H * OUT

eta = 1e-6
for i in range(1000):
  #Forward pass
  h = x @ w1                              # N * H
  h_relu = h.clamp(min = 0)               # N * H
  y_pred = h_relu @ w2                    # N * OUT
  
  #Loss
  loss = (y_pred - y).pow(2).sum().item()
  print('times is {}, loss is {}'.format(i, loss))
  
  #Backward pass
  grad_y_pred = 2.0 * (y_pred - y)        # N * OUT
  grad_w2 = (h_relu.t()) @ (grad_y_pred) #H * OUT = (H * N) * (N * OUT),其中(H * N) = (N * H).T
  
  grad_h_relu = grad_y_pred @ ((w2.t()))# N * H = (N * OUT) * (OUT * H),其中(OUT * H) = (H * OUT).T
  grad_h = grad_h_relu.clone()
  grad_h[h < 0] = 0
 
  grad_w1 = (x.t()) @ (grad_h)  # IN * H = (IN * N) * (N * H)
  
  w1 = w1 - eta * grad_w1
  w2 = w2 - eta

在这里插入图片描述

pytorch实现简单的神经网络

import torch
import torch.nn as nn
import torch.functional as F

class Model(nn.Module):
	def __init__(self):
		super(Model, self).__init__()
		self.linear = nn.Linear(1,1,bias=False)

	def forward(self,x):
		y_pred = self.linear(x)
		return y_pred
model = Model()

x_data = torch.Tensor([[1.0], [2.0], [3.0]])
y_data = torch.Tensor([[2.0], [4.0], [6.0]])

criterion = nn.MSELoss(size_average= True) # 注意此处改为True#注意这里改为True

optimizer = torch.optim.SGD(model.parameters(),lr=0.1)
epoches = 20
cost = []
for epoch in range(epoches):
    y_pred = model(x_data)
    loss = criterion(y_pred,y_data)
    print(loss)
    cost.append(loss.item()) #由于只有一个值 直接item()
    
    optimizer.zero_grad()#清零梯度
    
    loss.backward()#计算梯度
    #参数更新
    optimizer.step()

list(model.parameters())

tensor(6.6497)
tensor(1.00000e-02 *
       2.9554)
tensor(1.00000e-04 *
       1.3136)
tensor(1.00000e-07 *
       5.8371)
tensor(1.00000e-09 *
       2.5887)
tensor(1.00000e-11 *
       1.2221)
tensor(1.00000e-14 *
       9.9476)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
tensor(0.)
[Finished in 0.8s]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值