torch

torch 包含了多维张量的数据结构以及基于其上的多种数学操作。另外,它也提供了多种工具,其中一些可以更有效地对张量和任意类型进行序列化。

import torch

torch.set_printoptions

设置打印选项
torch.set_printoptions(precision=None, threshold=None, edgeitems=None, linewidth=None, profile=None)

  • precision – 浮点数输出的精度位数 (默认为8 )
  • threshold – 阈值,触发汇总显示而不是完全显示(repr)的数组元素的总数 (默认为1000)
  • edgeitems – 汇总显示中,每维(轴)两端显示的项数(默认值为3)
  • linewidth – 用于插入行间隔的每行字符数(默认为80)。Thresholded matricies will ignore this parameter.
  • profile – pretty打印的完全默认值。 可以覆盖上述所有选项 (默认为short, full)
  • sci_mode - 科学计数法
a = torch.rand(100,100)
a

只会输出一部分数据

torch.set_printoptions(precision=10)
a

输出全部数据

torch.eye

返回一个2维张量,对角线位置全1,其它位置全0

torch.eye(4)
tensor([[1., 0., 0., 0.],[0., 1., 0., 0.],[0., 0., 1., 0.],[0., 0., 0., 1.]])

numpy转化为tensor

import numpy as np
a = np.array([1,2,3,4,5,6])
a
array([1, 2, 3, 4, 5, 6])
#方法一
tensor_a = torch.tensor(a)
tensor_a
tensor([1, 2, 3, 4, 5, 6], dtype=torch.int32)
tensor_a[0] = -1
a
array([1, 2, 3, 4, 5, 6])
b = np.array([1,2,3,4,5,6])
b
array([1, 2, 3, 4, 5, 6])

torch.from_numpy将numpy.ndarray 转换为pytorch的 Tensor。 返回的张量tensor和numpy的ndarray共享同一内存空间。**修改一个会导致另外一个也被修改。**返回的张量不能改变大小。

#方法二
tensor_b = torch.from_numpy(b)
tensor_b
tensor([1, 2, 3, 4, 5, 6], dtype=torch.int32)
tensor_b[0] = -1
b
array([-1,  2,  3,  4,  5,  6])

torch.linspace

返回一个1维张量,包含在区间start 和 end 上均匀间隔的steps个点。 输出1维张量的长度为steps。

  • start (float) – 序列的起始点
  • end (float) – 序列的最终值
  • steps (int) – 在start 和 end间生成的样本数
  • out (Tensor, optional) – 结果张量
torch.linspace(1,5,steps = 10)
tensor([1.0000000000, 1.4444444180, 1.8888888359, 2.3333334923, 2.7777776718,3.2222223282, 3.6666665077, 4.1111111641, 4.5555553436, 5.0000000000])

torch.logspace

返回一个1维张量,包含在区间 1 0 s t a r t 10^{start} 10start和 1 0 e n d 10^{end} 10end上以对数刻度均匀间隔的steps个点。 输出1维张量的长度为steps。

  • start (float) – 序列的起始点
  • end (float) – 序列的最终值
  • steps (int) – 在start 和 end间生成的样本数
  • out (Tensor, optional) – 结果张量
torch.logspace(1,8,steps = 3)
tensor([   10.0000000000, 31622.7773437500, 100000000.0000000000])

torch.ones

返回一个全为1 的张量,形状由可变参数sizes定义。

torch.ones(3,5)
tensor([[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.],[1., 1., 1., 1., 1.]])

torch.rand

返回一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义。

torch.rand(4)
tensor([0.2573032379, 0.2179149985, 0.4656094313, 0.0828071833])
torch.rand(5, 3)
tensor([[0.7632396221, 0.0302500725, 0.6429942846],[0.2644285560, 0.1896467209, 0.4061338902],[0.6657168269, 0.1425771117, 0.5113636851],[0.9372168183, 0.7655946612, 0.1887679100],[0.3604329824, 0.9002583623, 0.0541984439]])

torch.randn

返回一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数

torch.randn(2,3)
tensor([[-0.3220688105, -1.0411276817, -0.0246272516],[ 0.2796218097, -0.4497807026, -0.0062698554]])

torch.randperm

给定参数n,返回一个从0 到n -1 的随机整数排列。

torch.randperm(8)
tensor([6, 3, 5, 7, 4, 2, 0, 1])

torch.arange

返回一个1维张量,长度为 floor((end−start)/step)。包含从start到end,以step为步长的一组序列值(默认步长为1)。

  • start (float) – 序列的起始点
  • end (float) – 序列的终止点
  • step (float) – 相邻点的间隔大小
 torch.arange(1,10)
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])
 torch.arange(1,10,3)
tensor([1, 4, 7])

torch.zeros

返回一个全为标量 0 的张量,形状由可变参数sizes 定义。

torch.zeros(2, 13)
tensor([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])
a = torch.tensor([1,2,3,4,5,6,7,8,9])
a
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9])

torch.split

  • 当split_size_or_sections为int时,tenor结构和split_size_or_sections,正好匹配,那么ouput就是大小相同的块结构。如果按照split_size_or_sections结构,tensor不够了,那么就把剩下的那部分做一个块处理。
  • 当split_size_or_sections 为list时,那么tensor结构会一共切分成len(list)这么多的小块,每个小块中的大小按照list中的大小决定,其中list中的数字总和应等于该维度的大小,否则会报错(注意这里与split_size_or_sections为int时的情况不同)。
torch.split(a,2)
(tensor([1, 2]), tensor([3, 4]), tensor([5, 6]), tensor([7, 8]), tensor([9]))
torch.split(a,[2,3,4])
(tensor([1, 2]), tensor([3, 4, 5]), tensor([6, 7, 8, 9]))

torch.cat

torch.cat()可以看做 torch.split() 和 torch.chunk()的反操作

  • inputs (sequence of Tensors) – 可以是任意相同Tensor 类型的python 序列
  • dimension (int, optional) – 沿着此维连接张量序列。
a = torch.tensor([[1,2,3],[3,3,1]])
a
tensor([[1, 2, 3],[3, 3, 1]])
b = torch.tensor([[11,22,3],[7,3,2]])
b
tensor([[11, 22,  3],[ 7,  3,  2]])
torch.cat((a,b),0)
tensor([[ 1,  2,  3],[ 3,  3,  1],[11, 22,  3],[ 7,  3,  2]])
torch.cat((a,b),1)
tensor([[ 1,  2,  3, 11, 22,  3],[ 3,  3,  1,  7,  3,  2]])

torch.transpose

返回输入矩阵input的转置

  • input (Tensor) – 输入张量
  • dim0 (int) – 转置的第一维
  • dim1 (int) – 转置的第二维
a = torch.tensor([[1,2],[3,4]])
a
tensor([[1, 2],[3, 4]])
torch.transpose(a,0,1)
tensor([[1, 3],[2, 4]])
torch.transpose(a,1,0)
tensor([[1, 3],[2, 4]])
a.T
tensor([[1, 3],[2, 4]])

随机种子

设置CPU生成随机数的种子,方便下次复现实验结果。
为 CPU 设置 种子 用于生成随机数,以使得结果是确定的。
当你设置一个随机种子时,接下来的随机算法生成数根据当前的随机种子按照一定规律生成。
随机种子作用域是在设置时到下一次设置时。要想重复实验结果,设置同样随机种子即可。

# 设定生成随机数的种子,并返回一个 torch._C.Generator 对象。
torch.manual_seed(seed)
# 返回生成随机数的原始种子值
torch.initial_seed()
#  从伯努利分布中抽取二元随机数(0 或者 1)。
torch.bernoulli

torch.abs

计算输入张量的每个元素绝对值

a = torch.tensor([[-1,2],[-3,-4]])
torch.abs(a)
tensor([[1, 2],[3, 4]])

torch.add()

对输入张量input逐元素加上标量值value,并返回结果到一个新的张量out,即 out=tensor+value。

a = torch.tensor([[-1,2],[-3,-4]])
torch.add(a,10)
tensor([[ 9, 12],[ 7,  6]])

torch.div()

将input逐元素除以标量值value,并返回结果到输出张量out。 即 out=tensor/value

torch.exp

返回一个新张量,包含输入input张量每个元素的指数。

torch.floor

返回一个新张量,包含输入input张量每个元素的floor,即不小于元素的最大整数。

torch.mul

用标量值value乘以输入input的每个元素,并返回一个新的结果张量。 out=tensor∗value

torch.pow

对输入input的按元素求exponent次幂值,并返回结果张量。 幂值exponent 可以为单一 float 数或者与input相同元素数的张量。

torch.round

返回一个新张量,将输入input张量每个元素舍入到最近的整数。

torch.sigmoid

返回一个新张量,包含输入input张量每个元素的sigmoid值。

torch.sqrt

返回一个新张量,包含输入input张量每个元素的平方根。

torch.mean

返回输入张量所有元素的均值。

torch.median

返回输入张量给定维度每行的中位数,同时返回一个包含中位数的索引的LongTensor。

torch.equal

如果两个张量有相同的形状和元素值,则返回True ,否则 False。

torch.max

返回输入张量所有元素的最大值。

torch.min

返回输入张量所有元素的最小值。

torch.Tensor

torch.Tensor是一种包含单一数据类型元素的多维矩阵。

Torch定义了七种CPU tensor类型和八种GPU tensor类型:

Data tyoe CPU tensor GPU tensor
32-bit floating point torch.FloatTensor torch.cuda.FloatTensor
64-bit floating point torch.DoubleTensor torch.cuda.DoubleTensor
16-bit floating point N/A torch.cuda.HalfTensor
8-bit integer (unsigned) torch.ByteTensor torch.cuda.ByteTensor
8-bit integer (signed) torch.CharTensor torch.cuda.CharTensor
16-bit integer (signed) torch.ShortTensor torch.cuda.ShortTensor
32-bit integer (signed) torch.IntTensor torch.cuda.IntTensor
64-bit integer (signed) torch.LongTensor torch.cuda.LongTensor

torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称。

torch模块常用方法总结相关推荐

  1. pythonos模块使用方法_Python OS模块常用方法总结

    Python OS模块常用方法总结 Python OS模块方法: 操作 说明 os.getcwd() 得到当前工作目录,即当前Python脚本工作的目录路径 os.listdir() 返回指定目录下的 ...

  2. Python random模块常用方法的使用

    Python random模块常用方法的使用 Python 的random模块包含许多随机数生成器. random是Python标准库之一,直接导入即可使用.本文介绍random中常用方法的用法. 一 ...

  3. re匹配截至到第一个中文_Python正则表达式:re模块常用方法详解

    正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.re 模块使 Python 语言拥有全部的正则表达式功能,re模块常用方法: re.match re.search r ...

  4. Python安装torch模块报错处理

    Python安装torch模块报错处理 前言 安装 报错 解决 查找文档 解决方案 安装 torch 等待安装完成 安装 torchvision 提示安装完成 总结 前言 因python项目上线,需要 ...

  5. 【 线性回归 Linear-Regression torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人(4/10)】

    torch模块实现与源码详解 深度学习 Pytorch笔记 B站刘二大人 深度学习 Pytorch笔记 B站刘二大人(4/10) 介绍 至此开始,深度学习模型构建的预备知识已经完全准备完毕. 从本章开 ...

  6. python中docx模块的使用_python使用docx模块读写docx文件的方法与docx模块常用方法详解...

    一,docx模块 Python可以利用python-docx模块处理word文档,处理方式是面向对象的.也就是说python-docx模块会把word文档,文档中的段落.文本.字体等都看做对象,对对象 ...

  7. python如何安装torch_Python安装torch模块报错处理

    Python安装torch模块报错处理 前言 安装 报错 解决 查找文档 解决方案 安装 torch 等待安装完成 安装 torchvision 提示安装完成 总结 前言 因python项目上线,需要 ...

  8. Python torch 模块,randperm() 实例源码

    参考Python torch 模块,randperm() 实例源码 - 云+社区 - 腾讯云 torch.randperm(n, *, out=None, dtype=torch.int64, lay ...

  9. Datetime模块常用方法(年、季度、月、周、日)

    目录 Datetime模块常用方法 1.年的加减 2.月的加减 3.日的加减 4.小时的加减 5.分钟的加减 6.时间与字符串的转换 7.两个日期计算相差多少天 8.当前小时:00:00--00:00 ...

最新文章

  1. 图像处理和图像识别中常用的OpenCV函数
  2. Vue js 的生命周期(看了就懂)
  3. [转] 标准化和归一化
  4. I/O复用之 epoll
  5. html字符串变量,字符串变量中的Python HTML
  6. JDK动态代理和CGLib动态代理简单演示
  7. [深入浅出WP8.1(Runtime)]浮出控件(Flyout)
  8. 练字格子纸模板pdf_这么好用的模板,我要好好保存下来!
  9. 腾讯宣布五年5000亿投入计划,发力新基建(2020年5月)
  10. power接口 sata_sata硬盘不接power接口读的出来吗
  11. 【linux】Can't connect to local MySQL server through socket和Plugin 'auth_socket' is not loaded报错...
  12. 拥抱Flutter,从0到1构建大前端应用
  13. 【译】Vue Patterns
  14. 快速排序 时间复杂度计算
  15. java pdf转png格式_如何在Java中将PDF转换为PNG或JPG
  16. 蓝牙、Wifi与ZigBee无线传输技术中,谁比较占有优势
  17. CSP2019滚粗记
  18. linux就是这个范儿之特种文件系统(1)
  19. 墨菲定律的内涵可以告诉我们的道理
  20. 重磅!微丰荣获2021企业微信CRM创新服务产品奖,沃丰科技加快企业数字化进程

热门文章

  1. jaspersoft studio创建jasperreport模板 summary显示page header(页眉)
  2. streamx平台部署
  3. 详解win10开机启动慢是什么原因怎么解决
  4. npm出现UNMET DEPENDENCY的一种情况
  5. 阿里出海遭遇挑战,Lazada三年换三帅仍无法制霸东南亚
  6. 基于眼底照片的病理性近视眼及识别
  7. 不管几岁,少女心万岁~女神节快乐!
  8. IOS swift开发——获取设备定位信息
  9. FFMPEG保存视频流数据至本地(rtsp转mp4)
  10. Pandas学习笔记(6)Renaming and Combining