Pytorch基础入门1

目录

  • [1 :简介](#1 :简介)
  • 2:Tensor的创建
    • [2.1:torch.tensor() && torch.tensor([])](#2.1:torch.tensor() && torch.tensor([]))
    • [2.2:torch.randn && torch.randperm](#2.2:torch.randn && torch.randperm)
    • 2.3:torch.range(begin,end,step)
    • [2.4 通过指定numpy的方式创建Tensor](#2.4 通过指定numpy的方式创建Tensor)
    • [2.5 创建0矩阵,1矩阵和单位矩阵](#2.5 创建0矩阵,1矩阵和单位矩阵)

1 :简介

Pytorch是torch的python版本,是由Facebook开源的神经网络框架,专门针对 GPU 加速的深度神经网络(DNN)编程。Torch 是一个经典的对多维矩阵数据进行操作的张量(tensor )库,在机器学习和其他数学密集型应用有广泛应用。与Tensorflow的静态计算图不同,pytorch的计算图是动态的,可以根据计算需要实时改变计算图。

2:Tensor的创建

Tensor张量是Pytorch里最基本的数据结构。直观上来讲,它是一个多维矩阵,支持GPU加速,其基本数据类型如下:

数据类型 CPU tensor GPU tensor
8位无符号整型 torch.ByteTensor torch.cuda.ByteTensor
8位有符号整型 torch.CharTensor torch.cuda.CharTensor
16位有符号整型 torch.ShortTensor torch.cuda.ShortTensor
32位有符号整型 torch.IntTensor torch.cuda.IntTensor
64位有符号整型 torch.LongTensor torch.cuda.LonfTensor
32位浮点型 torch.FloatTensor torch.cuda.FloatTensor
64位浮点型 torch.DoubleTensor torch.cuda.DoubleTensor
布尔类型 torch.BoolTensor torch.cuda.BoolTensor

2.1:torch.tensor() && torch.tensor([])

二者的主要区别在于创建的对象的size和value不同

python 复制代码
import torch
 
tensor1 = torch.Tensor(2, 3)
tensor2 = torch.Tensor([2, 3])
### 打印数据 
print(tensor1)
print(tensor2)

结果如下:

powershell 复制代码
tensor([[-1.1072e+20,  1.5989e-42,  0.0000e+00], 
        [ 0.0000e+00,  0.0000e+00,  0.0000e+00]])
tensor([2., 3.])

2.2:torch.randn && torch.randperm

生成的数据类型为浮点型,与numpy.randn生成随机数的方法类似,生成的浮点数的取值满足均值为0,方差为1的正态布,torch.randpern(n)为创建一个n个整数,随机排列的Tensor

python 复制代码
import torch 

tensor1 = torch.randn(2,3)
tensor2 = torch.randperm(10)

### 打印数据
print(tensor1)
print(tensor2)

结果如下:

powershell 复制代码
tensor([[-0.0469,  0.4112, -0.5787], 
        [ 1.0721, -0.0457, -0.4078]])
tensor([4, 3, 8, 9, 2, 7, 5, 6, 0, 1])

2.3:torch.range(begin,end,step)

生成一个一维的Tensor,三个参数分别的起始位置,终止位置和步长

python 复制代码
import torch 

tensor01 = torch.range(1,10,2)

### 打印数据
print (tensor01)

结果如下:

powershell 复制代码
tensor([1., 3., 5., 7., 9.])

2.4 通过指定numpy的方式创建Tensor

很多时候我们需要创建指定的Tensor,而numpy就是一个很好的方式

python 复制代码
import torch  # type: ignore
import numpy as np  

tensor01 = torch.tensor(np.arange(15).reshape(3,5))
### 打印数据
print(tensor01)

结果如下

powershell 复制代码
tensor([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14]], dtype=torch.int32)

2.5 创建0矩阵,1矩阵和单位矩阵

python 复制代码
import torch 

tensor1 =torch.ones(3,3)
tensor2 =torch.zeros(3,3)
tensor3 =torch.eye(3,3)

print(tensor1)
print(tensor2)
print(tensor3)

结果如下:

powershell 复制代码
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]])
tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])
相关推荐
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴3 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再3 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
Forrit3 小时前
ptyorch安装
pytorch
喵手5 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威6 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ7 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto
枷锁—sha7 小时前
【SRC】SQL注入WAF 绕过应对策略(二)
网络·数据库·python·sql·安全·网络安全