前馈神经网络 torch.nn实现回归模型

数据集

import torch
import torch.nn as nn
from IPython import display
from matplotlib import pyplot as plt
import numpy as np
import random
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
train_sample = 500
train_examples = 7000
w = torch.ones(train_sample,1)*0.0056
train_x = torch.tensor(np.random.normal(0, 1, (train_examples, train_sample)), dtype=torch.float)
labels = torch.mm(train_x,w) + 0.028
print(train_x.size())
print(labels.size())
print(w.size())
plt.figure(figsize=(10,8))
plt.scatter(train_x[:,0],labels)
plt.xlabel("x")
plt.ylabel("y")
plt.show()

在这里插入图片描述

def layer_sizes(X , Y):
    n_x = X.shape[1] #输入层
    n_h = 5 
    n_y = Y.shape[1] #输出层
    print(n_x)
    print(n_h)
    print(n_y)
    return n_x,n_h,n_y

class Net(nn.Module):
  def __init__(self, n_feature, n_hidden, n_output):
    super(Net, self).__init__()
    self.hidden = nn.Linear(n_feature, n_hidden)
    self.out = nn.Linear(n_hidden, n_output)
 
  def forward(self, x):
    A1 = self.hidden(x)
    A2 = self.out(A1)
    return A2

learning_rate = 0.05
n_0, n_1, n_2 = layer_sizes(train_x, labels)
model = Net(n_feature=n_0, n_hidden=n_1, n_output=n_2)
criterion = torch.nn.MSELoss(reduction='mean')
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)

# 迭代开始
epoch_nums = 100
ls = []
for epoch in range(epoch_nums):
    y_hat = model(train_x)
    loss = criterion(y_hat, labels)
    ls.append(loss)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if epoch % 10 == 0:
        print("epoch:{}, loss:{:.5}".format(epoch, loss))
plt.plot(ls)
plt.xlabel('x')
plt.ylabel('y')
plt.show()

结果

在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值