PyTrch深度学习简明实战1 - 线性回归

2023-03-03  本文已影响0人  薛东弗斯

清华大学出版社-图书详情-《PyTorch深度学习简明实战》 (tsinghua.edu.cn)

数据集

income1.csv
    Unnamed: 0  Education     Income
0            1  10.000000  26.658839
1            2  10.401338  27.306435
2            3  10.842809  22.132410
3            4  11.244147  21.169841
4            5  11.645485  15.192634
5            6  12.086957  26.398951
6            7  12.488294  17.435307
7            8  12.889632  25.507885
8            9  13.290970  36.884595
9           10  13.732441  39.666109
10          11  14.133779  34.396281
11          12  14.535117  41.497994
12          13  14.976589  44.981575
13          14  15.377926  47.039595
14          15  15.779264  48.252578
15          16  16.220736  57.034251
16          17  16.622074  51.490919
17          18  17.023411  61.336621
18          19  17.464883  57.581988
19          20  17.866221  68.553714
20          21  18.267559  64.310925
21          22  18.709030  68.959009
22          23  19.110368  74.614639
23          24  19.511706  71.867195
24          25  19.913043  76.098135
25          26  20.354515  75.775218
26          27  20.755853  72.486055
27          28  21.157191  77.355021
28          29  21.598662  72.118790
29          30  22.000000  80.260571
import torch
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from torch import nn

# print(torch.__version__)

data = pd.read_csv('./income1.csv')
# print(data.head())   # 数据集只有2列,分别是education和income
# print(data.info())   # 都是float类型,可以直接进行运算。如果是字符串类型,则需要进行更改。

# plt.scatter(data.Education,data.Income)
# plt.xlabel('Education')
# plt.ylabel('Income')
# plt.show()

# X = data.Education.values   # 取出1维的ndarray数据。 data.Education是Series
# X = data.Education.values.reshape(-1,1)  # 第一个维度-1 表示自动计算,第2个维度指定为1; 生成30个维度为1的输入。
# torch.from_numpy(data.Education.values.reshape(-1,1)   # torch.from_numpy 从ndarray创建tensor
X = torch.from_numpy(data.Education.values.reshape(-1,1)).type(torch.FloatTensor)  #转换为FloatTensor类型
Y = torch.from_numpy(data.Income.values.reshape(-1,1)).type(torch.FloatTensor)
# print(X.shape)
# print(Y.shape)

# pytorch创建模型,必须自己新定义一个类。这个类必须继承自nn.Module
class EIModel(nn.Module):   # 创建一个新的类EIModel,这个类必须继承至父类nn.Module
    def __init__(self):     # 必须重新定义初始化方法。
        super(EIModel, self).__init__()
        # 我们的权重 f=wx+b,对应pytorch是linear层,初始化w和b,对输入进行加权。
        # linear层就是自己初始化w,乘以输入,再加b
        self.linear = nn.Linear(in_features=1,out_features=1)
    def forward(self,inputs):  # 定义forward方法。 定义如何进行前向传播
        logits = self.linear(inputs)  # 再self.linear层进行调用,返回输入logits
        return logits

model = EIModel()   # 类的实例化
loss_fn = nn.MSELoss()  # 定义均方差损失函数
opt = torch.optim.SGD(model.parameters(),lr=0.0001)
# 通过model.parameters返回模型的可训练参数. lr 学习率。

for epoch in range(5000):
    for x,y in zip(X,Y):  # zip函数,X与Y同时迭代
        y_pred = model(x)  # 调用forward方法,得到输出
        loss = loss_fn(y_pred, y)
        opt.zero_grad()  # 1. 将之前的梯度清零
        loss.backward()  # 2. 损失反向传播
        opt.step()  # 3.优化器
print('Down!')

print(list(model.named_parameters()))  #输出优化后的参数
# print(model.linear.weight, model.linear.bias)

plt.scatter(data.Education, data.Income, label='real data') #绘制原始散点图
plt.plot(X,model(X).detach().numpy(),c='r',label='predicted line')
plt.xlabel('Education')
plt.ylabel('Income')
plt.legend()
plt.show()
image.png
import torch
import pandas as pd

data = pd.read_csv('./income1.csv')
X = torch.from_numpy(data.Education.values.reshape(-1,1)).type(torch.FloatTensor)
Y = torch.from_numpy(data.Income.values.reshape(-1,1)).type(torch.FloatTensor)

w = torch.randn(1, requires_grad=True)
b = torch.zeros(1, requires_grad=True)
# 模型公式 y = w@x + b

learning_rate=0.0001

for epoch in range(5000):
    for x,y in zip(X,Y):
        y_pred = torch.matmul(x, w) + b           # 1. 使用模型进行预测
        loss = (y - y_pred).pow(2).mean()         # 2. 根据预算结果计算损失
        if not w.grad is None:                    # 3. 将梯度清零
            w.grad.data.zero_()
        if not b.grad is None:
            b.grad.data.zero_()
        loss.backward()                           # 4. 求梯度
        with torch.no_grad():
            w.data -= w.grad.data*learning_rate   # 5. 优化模型参数
            b.data -= b.grad.data*learning_rate

print(w)
print(b)

tensor([4.9734], requires_grad=True)
tensor([-28.3475], requires_grad=True)
上一篇 下一篇

猜你喜欢

热点阅读