当前位置: 首页 > news >正文

【PyTorch】张量操作与线性回归

张量的操作

Tensor Operation

拼接与切分

1.1 torch.cat()

torch.cat(tensors, dim=0, out=None)

功能:将张量按维度dim进行拼接

  • tensors:张量序列
  • dim:要拼接的维度

1.2 torch.stacok()

torch.stack(tensors, dim=0, out=None)

功能:在新创建的维度dim上进行拼接

  • tensors:张量序列
  • 要拼接的维度

1.3 torch.chunk()

torch.chunk(input, chunks, dim=0)

功能:将张量按维度dim进行平均切分
返回值:张量列表
注意事项:若不能整除,最后一份张量小于其他张量

  • input:要切分的张量
  • chunks:要切分的份数
  • dim:要切分的维度

1.4 torch.split()

torch.chunk(input, chunks, dim=0)

功能:将张量按维度dim进行切分
返回值:张量列表

  • tensor:要切分的张量
  • split_size_or_sections:为int时,表示每一份的长度;为list时,按list元素切分
  • dim:要切分的维度

索引

2.1 torch.index_select()

torch.index_select(input, dim, index, out=None)

功能:在维度dim上,按index索引数据返回值(依index索引数据拼接的张量)

  • input:要索引的张量
  • dim:要索引的维度
  • index:要索引数据的序号

2.2 torch.masked_select()

torch.masked_select(input, mask, out=None)

功能:按mask中的True进行索引
返回值:一维张量

  • input:要索引的张量
  • mask:与input同形状的布尔类型张量

变换

3.1 torch.reshape()

torch.reshape(input, shape)

功能:变换张量形状
注意事项:当张量在内存中是连续时,新张量与input共享数据内存(改变一个变量时,另一个变量也会被改变)。

  • input:要变换的张量
  • shape:新张量的形状

3.2 torch.transpose()

torch.transpose(input, dim0, dim1)

功能:交换张量的两个维度

  • input:要变换的张量
  • dim0:要交换的维度
  • dim1:要交换的维度

3.3 torch.t()

功能:2维张量转置,对矩阵而言,等价于

torch.transpose(input, 0, 1)

3.4 torch.squeeze()

torch.squeeze(input, dim=None, out=None)

功能:压缩长度为1的维度(轴)

  • dim:若为None,移除所有长度为1 的轴;若指定维度,当且仅当该轴长度为1时,可以被移除

3.5 torch.unsqueeze()

torch.unsqueeze(input, dim)

功能:依据dim扩展维度

  • dim:扩展的维度

张量的数学运算

Tensor Math Operations

加减乘除

torch.add()
torch.addcidv()
torch.addcmul()
torch.sub()
torch.div()
torch.mul()

对数、指数、幂函数

torch.log(input, out=None)
torch.log10(input, out=None)
torch.log2(input, out=None)
torch.exp(input, out=None)
torch.pow()

三角函数

torch.abs(input, out=None)
torch.acos(input, out=None)
torch.cosh(input, out=None)
torch.cos(input, out=None)
torch.asin(input, out=None)
torch.atan(input, out=None)
torch.atan2(input, other, out=None)

实例

torch.add()

torch.add(input, other, out=None)
torch.add(input, other, *, alpha=1, out=None)

功能:逐元素计算 input + alpha × other

  • input:第一个张量
  • alpha:乘项因子
  • other:第二个张量

Pythonic:
torch.addcdiv()

torch.addcdiv(input, tensor1, tensor2, *, value=1, out=None)

在这里插入图片描述

torch.addcmul()

torch.addcmul(input, tensor1, tensor2, *, value=1, out=None)

在这里插入图片描述

线性回归

Linear Regression

基本概念

线性回归是分析一个变量与另外一(多)个变量之间关系的方法。

因变量:y
自变量:x
关系:线性

y = wx + b

分析:求解w,b

求解步骤

  1. 确定模型
    Model:y = wx + b
  2. 选择损失函数
    MSE
    在这里插入图片描述
  3. 求解梯度并更新w,b
    w = w - LR * w.grad
    b = b - LR * w.grad

完整代码

import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)lr = 0.05  # 学习率# 创建训练数据
x = torch.rand(20, 1) * 10  # x data (tensor), shape=(20, 1)
# torch.randn(20, 1) 用于添加噪声
y = 2*x + (5 + torch.randn(20, 1))  # y data (tensor), shape=(20, 1)# 构建线性回归参数
w = torch.randn((1), requires_grad=True) # 设置梯度求解为 true
b = torch.zeros((1), requires_grad=True) # 设置梯度求解为 true# 迭代训练 1000 次
for iteration in range(1000):# 前向传播,计算预测值wx = torch.mul(w, x)y_pred = torch.add(wx, b)# 计算 MSE lossloss = (0.5 * (y - y_pred) ** 2).mean()# 反向传播loss.backward()# 更新参数b.data.sub_(lr * b.grad)w.data.sub_(lr * w.grad)# 每次更新参数之后,都要清零张量的梯度w.grad.zero_()b.grad.zero_()# 绘图,每隔 20 次重新绘制直线if iteration % 20 == 0:plt.scatter(x.data.numpy(), y.data.numpy())plt.plot(x.data.numpy(), y_pred.data.numpy(), 'r-', lw=5)plt.text(2, 20, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color':  'red'})plt.xlim(1.5, 10)plt.ylim(8, 28)plt.title("Iteration: {}\nw: {} b: {}".format(iteration, w.data.numpy(), b.data.numpy()))plt.pause(0.5)# 如果 MSE 小于 1,则停止训练if loss.data.numpy() < 1:break

参考链接

PyTorch 学习笔记

http://www.lryc.cn/news/442205.html

相关文章:

  • 情感类智能体——你的微信女神
  • 基于SpringBoot+Vue+MySQL的养老院管理系统
  • 大数据Flink(一百二十二):阿里云Flink MySQL连接器介绍
  • FutureTask源码分析
  • Highcharts甘特图基本用法(highcharts-gantt.js)
  • 【Linux庖丁解牛】—Linux基本指令(上)!
  • node.js 中的进程和线程工作原理
  • Qt/C++ TCP调试助手V1.1 新增图像传输与接收功能(附发布版下载链接)
  • DNS解析流程
  • [PTA]7-1 藏头诗
  • 每日OJ题_牛客_WY22 Fibonacci数列(斐波那契)
  • SQL 查询语句汇总
  • 封装一个语言识别文字的方法
  • 解决 iOS App Tracking Transparency 权限问题
  • ClickHouse 的底层架构和原理
  • rtmp推流
  • 【数据库】死锁排查方式
  • 去耦合的一些建议
  • SpringBoot+Thymeleaf图书管理系统
  • TDengine 签约前晨汽车,解锁智能出行的无限潜力
  • 模板字符串中定义方法并传参
  • Numpy 数组元素添加与元素删除函数详解
  • 【Python】高效图像处理库:pyvips
  • java项目之在线考试与学习交流网页平台源码(springboot)
  • 【Android源码】屏蔽系统通知出现在系统栏中
  • MySQL索引测试
  • 【软件设计】常用设计模式--观察者模式
  • 东北非国企就职体验
  • 经典sql题(二)求连续登录最多天数用户
  • A. Closest Point