pytorch 基本操作 和 训练回归模型

import  torch

a = torch.empty(5,3) #创建一个空矩阵
#print(a)
b = torch.rand(5,3) #创建一个随机矩阵
#print(b)

#注意在pytorch中所有的操作都是对tensor操作的

c = torch.zeros(5,3)#创建一个全0矩阵
#print(c)

d = torch.zero_(b) #将输入的矩阵设置为0
#print(d)

f = torch.tensor([5.3, 3])
e = f.new_ones(5, 3, dtype = torch.double)
e = torch.randn_like(e, dtype = torch.float)
# print(e)
# print(e.size())

#矩阵加法 e+f or torch.add(x, y)
#索引类似np

#view操作可以改变tensor的形状
x = torch.randn(4,4, requires_grad=True)
y = x.view(16)
z = x.view(-1, 8) #注意这里的 -1 表示自动计算形状
#print(x.size(), y.size(), z.size())

#numpy协同操作, 原因:数据集的格式大多数为np.ndarray, 需要转化成tensor才能进行pytorch运算
a = torch.ones(3,3)
b = a.numpy()  #将tensor 转化成 np
c = torch.from_numpy(b) #将np 转化成 tensor
#print(type(b))
#print(c)

#pytorch 的亮点就是自动求梯度机制
w = torch.randn(1, requires_grad=True)
x = torch.randn(1)
y = w * x
b = torch.randn(1, requires_grad=True)
z = y + b
#print(x.requires_grad,w.requires_grad,y.requires_grad,z.requires_grad) #False True True True
#print(x.is_leaf,w.is_leaf,y.is_leaf,z.is_leaf) #True True False False

z.backward(retain_graph = True) #如果不清空梯度,每次执行会累加前一次执行的结果
#print(w.grad, b.grad)

#pytorch线性回归例子
#第一步 构造输入数据X和其对应的标签Y, 可以尝试从网上找一些数据集
import numpy as np
x_values = [i for i in range(11)]
x_train = np.array(x_values, dtype = np.float32)
x_train = x_train.reshape(-1,1)
print(x_train.shape)

y_values = [2*i+1 for i in x_values]
y_train = np.array(y_values, dtype=np.float32)
y_train = y_train.reshape(-1, 1)
print(y_train.shape)

#第二步 构建一个模型类,线性回归就是一个不加激活函数的全连接层
import torch.nn as nn
class LinerRegressionModel(nn.Module):
    def __init__(self, input_dim, out_dim):
        super(LinerRegressionModel, self).__init__()
        self.linear = nn.Linear(input_dim, out_dim)

    def forward(self, x):
        out = self.linear(x)
        return out

input_dim = 1
output_dim= 1

model = LinerRegressionModel(input_dim, output_dim)

#如果需要gpu训练则多出来两步,1.将模型放入gpu中 2.将数据和标签放入gpu中
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model.to(device)

#第三步 指定好参数、优化器和损失函数
epochs = 1000
learning_rate = 0.01
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
criterion = nn.MSELoss()

#第四步 训练模型,
for epoch in range(epochs):
    epoch+=1

    #注意将数据和标签转为tensor,同时放入gpu中
    inputs = torch.from_numpy(x_train).to(device)
    labels = torch.from_numpy(y_train).to(device)

    #梯度需要清零每次迭代
    optimizer.zero_grad()

    #前向传播
    outputs = model(inputs)

    #计算损失
    loss = criterion(outputs, labels)

    #反向传播
    loss.backward()

    #更新权重参数
    optimizer.step()

    #打印训练过程
    if epoch % 50 ==0:
        print("epoch: {}, loss: {}".format(epoch, loss.item()))

#第五步 测试模型预测结果
predicted = model(torch.from_numpy(x_train).requires_grad()).data.numpy() #这步报错,下面有单独的加载模型测试
print(predicted)

#第六步 模型的保存预读取
torch.save(model.state_dict(), "model.pkl")
model.load_state_dict(torch.load("model.pkl"))

####加载验证保存的模型
####
# import numpy as np
# import torch.nn as nn
# class LinerRegressionModel(nn.Module):
#     def __init__(self, input_dim, out_dim):
#         super(LinerRegressionModel, self).__init__()
#         self.linear = nn.Linear(input_dim, out_dim)
#
#     def forward(self, x):
#         out = self.linear(x)
#         return out
#
# input_dim = 1
# output_dim= 1
#
# model = LinerRegressionModel(input_dim, output_dim)
#
# model.load_state_dict(torch.load("model.pkl"))
# x_values = [3,4]
# x_train = np.array(x_values, dtype = np.float32)
# x_train = x_train.reshape(-1,1)
# inputs = torch.from_numpy(x_train)
# y = model(inputs)
# print(y)

#tensor 常见的形式有哪些 1.scalar(0d) 2.vector(1d) 3.matrix(2d) 4.n-dimensional tensor(3d及以上)

#hub模块有已经训练好的模型,可以直接调用,github网站和pytorch网址 https://github.com/pytorch/hub  https://pytorch.org/hub/research-models

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值