【Pytorch】Tensor基本操作

  • 一、Tensor概述
  • 二、Tensor张量的定义
    • tensor基本定义
    • 获取tensor大小
  • 三、生成Tensor
    • 定义全0的tensor
    • 定义随机tensor
    • 定义未初始化数据的张量
    • arange方法生成tensor
  • 四、Numpy 数据转换
    • tensor转numpy格式
    • numpy转tensor格式
  • 五、tenso运算操作
    • 加法
    • 减法
    • 乘法
    • 除法
  • 六、tensor维度变换
    • unsqueeze方法
    • squeeze方法
    • cat方法
  • 七、tensor索引

一、Tensor概述

PyTorch里面处理的最基本的操作对象就是Tensor(张量),它表示的是一个多维矩阵,在使用上和numpy是对应的

Tensor的基本数据类型有五种:

  • 32位浮点型:torch.FloatTensor。pyorch.Tensor()默认的就是这种类型。
  • 64位整型:torch.LongTensor。
  • 32位整型:torch.IntTensor。
  • 16位整型:torch.ShortTensor。
  • 64位浮点型:torch.DoubleTensor。

二、Tensor张量的定义

tensor基本定义

import torch
from torch import tensor
# 定义0维Tensor
a = tensor(10)
# 定义1维Tensor
b = torch.IntTensor([1,2,3])
# 定义2维Tensor
c = torch.Tensor([[1, 2], [3, 4], [5, 6]])
# 定义3维Tensor
d = tensor([[[1, 2], [3, 4], [5, 6]],[[1, 2], [3, 4], [5, 6]]])
print(a)
print(b)
print(c)
print(d)

输出结果:

tensor(10)
tensor([1, 2, 3], dtype=torch.int32)
tensor([[1., 2.],[3., 4.],[5., 6.]])
tensor([[[1, 2],[3, 4],[5, 6]],[[1, 2],[3, 4],[5, 6]]])

获取tensor大小

# 获取矩阵的大小
print(d.size())
print(d.shape)

输出结果

torch.Size([2, 3, 2])
torch.Size([2, 3, 2])
获取0维tensor的值
a.item()

三、生成Tensor

定义全0的tensor

# 定义一个5行3列的全为0的矩阵
a = torch.zeros(5,3,dtype=torch.double)
print(a)

输出

tensor([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]], dtype=torch.float64)

定义随机tensor

# 定义一个5行3列的随机值矩阵
x = torch.randn(5, 3)
print(x)

输出

tensor([[ 1.3276,  1.0624, -1.3002],[-0.8084,  1.0612, -0.9558],[ 1.2273, -0.0903, -0.9187],[-0.2024,  0.0736, -1.3670],[ 2.6466,  0.2927,  0.1307]])

定义未初始化数据的张量

# 定义未初始化数据的张量
x = torch.empty(5,3)
print(x)

输出

tensor([[1.0286e-38, 1.0653e-38, 1.0194e-38],[8.4490e-39, 1.0469e-38, 9.3674e-39],[9.9184e-39, 8.7245e-39, 9.2755e-39],[8.9082e-39, 9.9184e-39, 8.4490e-39],[9.6429e-39, 1.0653e-38, 1.0469e-38]])

# 定义未初始化数据的张量,torch.FloatTensor类型的张量
x = torch.Tensor(5, 3)
print(x)

输出

tensor([[9.9184e-39, 9.0919e-39, 1.0561e-38],[4.2246e-39, 1.0286e-38, 1.0653e-38],[1.0194e-38, 8.4490e-39, 1.0469e-38],[9.3674e-39, 9.9184e-39, 8.7245e-39],[9.2755e-39, 8.9082e-39, 9.9184e-39]])

arange方法生成tensor

# 使用torch.arange()生成张量
torch.arange(start=0, end = 11, step=2)

输出

tensor([ 0,  2,  4,  6,  8, 10])

# 在范围内生成固定数量的等间隔张量
torch.linspace(start = 1, end = 10, steps=5)

输出

tensor([ 1.0000,  3.2500,  5.5000,  7.7500, 10.0000])

四、Numpy 数据转换

tensor转numpy格式

import torch
import numpy as np# 定义一个3行2列的全为0的矩阵
b = torch.randn((3, 2))# tensor转化为numpy
numpy_b = b.numpy()
print(numpy_b)

输出

[[-0.27381065  0.43861285][ 1.0121734   0.4847141 ][ 2.6019974  -0.65572333]]

numpy转tensor格式

# numpy转化为tensor
numpy_e = np.array([[1, 2], [3, 4], [5, 6]])
torch_e = torch.from_numpy(numpy_e)print(numpy_e)
print(torch_e)

输出

[[1 2][3 4][5 6]]
tensor([[1, 2],[3, 4],[5, 6]], dtype=torch.int32)

五、tenso运算操作

加法

import torch
import numpy as np
x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
y= torch.Tensor([[1, 2], [3, 4], [5, 6]])
print(x + y)

输出

tensor([[ 2.,  4.],[ 6.,  8.],[10., 12.]])

print(torch.add(x, y))

输出

tensor([[ 2.,  4.],[ 6.,  8.],[10., 12.]])

减法

x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
y= torch.Tensor([[1, 2], [3, 4], [5, 6]])
print(x - y)

输出

tensor([[0., 0.],[0., 0.],[0., 0.]])

乘法

x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
y= torch.Tensor([[1, 2], [3, 4], [5, 6]])
print(x * y)

输出

tensor([[ 1.,  4.],[ 9., 16.],[25., 36.]])

除法

x = torch.Tensor([[1, 2], [3, 4], [5, 6]])
y= torch.Tensor([[1, 2], [3, 4], [5, 6]])
print(x / y)
print(torch.div(x, y))
tensor([[1., 1.],[1., 1.],[1., 1.]])
tensor([[1., 1.],[1., 1.],[1., 1.]])

六、tensor维度变换

# 转成3行4列的2维tensor
A = torch.arange(12.0).reshape(3,4)
A

输出

tensor([[ 0.,  1.,  2.,  3.],[ 4.,  5.,  6.,  7.],[ 8.,  9., 10., 11.]])

## 使用torch.reshape()
torch.reshape(input = A,shape = (2,-1))
tensor([[ 0.,  1.,  2.,  3.,  4.,  5.],[ 6.,  7.,  8.,  9., 10., 11.]])

unsqueeze方法

## torch.unsqueeze()返回在指定维度插入尺寸为1的新张量
A = torch.arange(12.0).reshape(2,6)
B = torch.unsqueeze(A,dim = 0)
B.shape

输出

torch.Size([1, 2, 6])

squeeze方法

## torch.squeeze()函数移除所有维度为1的维度
C = B.unsqueeze(dim = 3)
print("C.shape : ",C.shape)
D = torch.squeeze(C)
print("D.shape : ",D.shape)
## 移除指定维度为1的维度
E = torch.squeeze(C,dim = 0)
print("E.shape : ",E.shape)

输出

C.shape :  torch.Size([1, 2, 6, 1])
D.shape :  torch.Size([2, 6])
E.shape :  torch.Size([2, 6, 1])

cat方法

## 在给定维度中连接给定的张量序列
A = torch.arange(6.0).reshape(2,3)
B = torch.linspace(0,10,6).reshape(2,3)
print(A)
print(B)
## 在0纬度连接张量
C = torch.cat((A,B),dim=0)
C

输出

tensor([[0., 1., 2.],[3., 4., 5.]])
tensor([[ 0.,  2.,  4.],[ 6.,  8., 10.]])
tensor([[ 0.,  1.,  2.],[ 3.,  4.,  5.],[ 0.,  2.,  4.],[ 6.,  8., 10.]])

七、tensor索引

## 利用切片和索引获取张量中的元素
A = torch.arange(12).reshape(1,3,4)
A

输出

tensor([[[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]]])

A[0]

输出

tensor([[ 0,  1,  2,  3],[ 4,  5,  6,  7],[ 8,  9, 10, 11]])

## 获取第0维度下的矩阵前两行元素
A[0,0:2,:]

输出

tensor([[0, 1, 2, 3],[4, 5, 6, 7]])

## 获取第0维度下的矩阵,最后一行-4~-1列
A[0,-1,-4:-1]

输出

tensor([ 8,  9, 10])

八、tensor统计相关的计算

## 1维张量的最大值和最小值
A = torch.tensor([12.,34,25,11,67,32,29,30,99,55,23,44])
## 最大值及位置
print("最大值:",A.max())
print("最大值位置:",A.argmax())
## 最小值及位置
print("最小值:",A.min())
print("最小值位置:",A.argmin())

输出

最大值: tensor(99.)
最大值位置: tensor(8)
最小值: tensor(11.)
最小值位置: tensor(3)

【Pytorch】Tensor基本操作相关推荐

  1. pytorch tensor 初始化_PyTorch简明笔记[1]-Tensor的初始化和基本操作

    听麻麻说,偷偷收藏而不感谢是不礼貌的,至少应该点个赞~我觉得麻麻说的对! 不断地被人安利PyTorch,终于忍不住诱惑决定入坑了. 当初学习TensorFlow的时候,没有系统性地学习.之前TF的英文 ...

  2. pytorch Tensor及其基本操作

    转自: https://zhuanlan.zhihu.com/p/36233589 由于之前的草稿都没了,现在只有重写-. 我好痛苦 本章只是对pytorch的常规操作进行一个总结,大家看过有脑子里有 ...

  3. python numpy.arry, pytorch.Tensor及原生list相互转换

    文章目录 python numpy.arry, pytorch.Tensor及原生list相互转换 1 原生list转numpy list 2 numpy.array 转原生list 3 numpy. ...

  4. pytorch tensor求向量的模长

    想要求pytorch tensor中某个2048维度的向量的模长,可以先相乘,然后再用sum求和. 假设 v是一个2048维的向量,则可以利用一下两个语句求出模长的平方. sq = v * vsum_ ...

  5. PyTorch Tensor 的形状

    PyTorch Tensor 的形状 标量 (Scalar) 向量 (Vector) 矩阵 (Matrix) 图片展示 标量 (Scalar) 标量 (Scalar) 由一个值组成. (0维) # S ...

  6. Pytorch - 使用opencv-python解码视频文件并将视频帧转换为Pytorch tensor作为网络模型输入数据

    1 视频文件作为网络模型的输入数据 越来越多的神经网络模型开始以视频作为训练数据,比如基于视频数据的行为识别等等,这就需要我们将视频转换为可适用的张量,本文将以pytorch为例,展示一下视频数据转换 ...

  7. pytorch tensor查找0_在PyTorch中Tensor的查找和筛选例子

    本文源码基于版本1.0,交互界面基于0.4.1 import torch 按照指定轴上的坐标进行过滤 index_select() 沿着某tensor的一个轴dim筛选若干个坐标 >>&g ...

  8. pytorch Tensor操作(二)

    高级索引 PyTorch在0.2版本中完善了索引操作,目前已经支持绝大多数numpy的高级索引.高级索引可以看成是普通索引操作的扩展,但是高级索引操作的结果一般不和原始的Tensor贡献内出. x = ...

  9. opencv mat 修改_C++ opencv矩阵和pytorch tensor的互相转换

    矩阵和tensor相互转换 cvmat到tensor tips:这里主要要注意的就是在opencv和pytorch中存储顺序的差异 cv::cvtColor(frame, frame, CV_BGR2 ...

最新文章

  1. sdut 2805(最小生成树)
  2. 民营企业SAP项目客户的几种心态
  3. 教你十分钟快速搭建springBoot项目实战
  4. IXMLDOMDocument中的load方法返回值有BUG
  5. centos6.5下redis安装步骤总结
  6. java一个式子开根号语句_Oracle查询语句,你知道几个?(上)
  7. Centos下oracle11g R2的启动与关闭监听、数据库
  8. JavaScript包管理器综述
  9. 【图像处理】基于matlab GUI美颜系统【含Matlab源码 809期】
  10. php压缩解压zip文件夹,php利用ZipArchive类实现文件压缩与解压
  11. 串口硬盘如何应用于并口硬盘计算机,串口硬盘和并口硬盘的区别 并口硬盘怎么改串口硬盘【详解】...
  12. 大学生职业生涯规划计划与路径_大学生职业生涯的规划路径
  13. Tensorflow 释放内存
  14. excel 的lookup和sumifs函数使用
  15. 1.1 迷茫的大学——《逆袭大学》连载
  16. mysql json 数组转行
  17. Python3 编写处理Excel表格数据筛选脚本用到的一些方法
  18. html5 cms结构,cms产品架构图.html
  19. PTA——日K蜡烛图
  20. 计算机的未来发展趋势

热门文章

  1. 如何设计一个高并发高可用的秒杀或抢券系统
  2. Pose Animator(姿势动画师)
  3. 人工智能和python的关系
  4. 新浪博客、百度博客搬家(PHP代码)
  5. LoadLibrary失败的原因
  6. Candence Virtuoso进行基本的电路设计
  7. fortran中write用法
  8. 资深HR来告诉大家制作个人简历的时候内容要怎么写?
  9. ubuntu 发行版安装向日葵远程控制软件
  10. SLAM实操入门(七):使用Velodyne16线激光雷达与A-Loam进行三维SLAM