pytorch教程(学习笔记)——tensor操作+模型实例(线性回归、LR、MLP)

这篇博客介绍了PyTorch的基础,包括张量操作、自动微分,以及线性回归、逻辑回归(LR)和多层感知机(MLP)的实现。内容涵盖张量的创建、转换、运算及形状变换,还讲解了张量的自动微分功能,并提供了模型实例的代码示例。
摘要由CSDN通过智能技术生成

突然翻出了蛮早之前做的pytorch学习笔记,整理了一下决定发成博客方便复习,当时根据视频资料和网页资料整理的,视频资料现在找不到了,后面找到了再进行补充;网上的博客资料标在文中对应位置。

一.pytorch安装

安装的话就不细说了,随便找个博客照着装一下就行。我一般都是先装包环境管理工具anaconda,然后搭建虚拟环境,在虚拟环境中安装pytorch。
教程:https://blog.csdn.net/qq_32863549/article/details/107698516

二.pytorch基础

1.pytorch张量与数据类型

pytorch最基本的操作单位是:张量(tensor),它表示一个多维矩阵。
tensor类似于numpy的adarray;
pytorch类似于能让tensor操作在GPU上加速运行的numpy。

#使用dtype规定,使用tensor_a.type()查看
torch.float32 #等价于torch.FloatTensor
torch.float64
torch.int16
torch.int32
torch.int64
#张量进行类型转换 tensor.type()
tensor = torch.ones(2,3,dtype=torch.float64)
print(tensor)
print(tensor.type(torch.int16))
#output
tensor([[1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[1, 1, 1],
        [1, 1, 1]], dtype=torch.int16)

2.张量基本操作

(1).构建张量

构建一个随机初始化张量:torch.rand()

a = torch.rand(2,3,dtype=torch.float64)
#output
tensor([[0.7320, 0.7675, 0.8107],
        [0.0359, 0.9635, 0.0936]], dtype=torch.float64

全0矩阵:torch.zeros()
全1矩阵:torch.ones()

a = torch.zeros(2,3)
a = torch.ones(2,3)
#ouput
tensor([[0., 0., 0.],
        [0., 0., 0.]])
tensor([[1., 1., 1.],
        [1., 1., 1.]])

直接从数据构造张量:torch.tensor()

tensor = torch.tensor([1,2,3])
#output
tensor([1, 2, 3])

按顺序构建:torch.range()torch.arange()
这两个方法的区别:
torch.arange(start=1, end=6)的范围是[0,6),类型是torch.LongTensor。
torch.range(start=1, end=6) 的范围是[0,6],类型是torch.FloatTensor。
注意:torch.range会提示在后续版本弃用,如下:

import torch
x = torch.arange(0, 6)
print(x)
print(x.type())
y = torch.range(0, 6)
print(y)
print(y.type())
#output
tensor([0, 1, 2, 3, 4, 5])
torch.LongTensor
tensor([0., 1., 2., 3., 4., 5., 6.])
torch.FloatTensor
D:/project/mine_project/test.py:17: UserWarning: torch.range is deprecated and will be removed in a future release because its behavior is inconsistent with Python's range builtin. Instead, use torch.arange, which produces values in [start, end).
  y = torch.range(0, 6)

(2).size()、shape

查看张量size/shape:a.shape / a.size()

a = torch.ones(2,3)
print(a.shape)
print(a.size())
#output
torch.Size([2, 3])
torch.Size([2, 3])

(3).adarray与tensor之间转换

adarray转tensor:torch.from_numpy(numpy矩阵)
tensor转adarray:tensor矩阵.numpy()

a = np.random.rand(2,3)
a = torch.from_numpy(a)
print(a)
a = a.numpy()
print(a)
#output
tensor([[0.2882, 0.0426, 0.2148],
        [0.1452, 0.7684, 0.3942]], dtype=torch.float64)
[[0.28821183 0.04255934 0.214766  ]
 [0.14523209 0.768359   0.39420861]]

3.张量运算与形状变换

(1)张量运算——乘法

参考文章https://blog.csdn.net/da_kao_la/article/details/87484403,里面有详细示例,这里就简单整理一下
tensor乘法一共有四种:

  • 1.点乘 *
    (1)标量
    tensor与标量k相乘:tensor1 * k
    结果是tensor中每个数都与k相乘
    (2)一维向量
    tensor与行向量相乘:
    结果是每列乘以行向量对应列的值(相当于把行向量的行复制,成为与lhs维度相同的Tensor). 注意此时要求Tensor的列数与行向量的列数相等
    tensor与列向量相乘ÿ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值