美文网首页
torch中的tensor

torch中的tensor

作者: JerryLoveCoding | 来源:发表于2020-04-17 16:46 被阅读0次

tensor,即“张量”。实际上跟numpy数组、向量、矩阵的格式基本一样。但是是专门针对GPU来设计的,可以运行在GPU上来加快计算效率。

定义一个tensor

Torch中定义tensor,与numpy中定义矩阵差不多,例如定义一个5×3的tensor,每一项都是0的张量:

x = torch.zeros(5,3)

另外初始化tensor还有如下形式:

  • torch.empty(size) 返回形状为size的空tensor
  • torch.zeros_like(input) 返回跟input的tensor一个size的全零tensor
  • torch.ones(size) 全部是1的tensor
  • torch.ones_like(input) 返回跟input的tensor一个size的全1tensor
  • torch.arange(start=0, end, step=1) 用法与range一样
  • torch.linspace(start,end,steps,) 创建均分的1维张量,数值区间[start,end],样本量为steps
  • torch.logspace(start,end,steps)创建对数均分的1维张量,和linspace参数一样

还有根据numpy数组初始化:

  • torch.from_numpy(input) numpy中的数据与tensor中的数据使用的同一个内存,一个数据改变另一个也会改变

还有随机初始化:

  • torch.rand(size) [0,1)内的均匀分布随机数
  • torch.rand_like(input) 返回跟input的tensor一样size的0-1随机数
  • torch.randn(size) 返回标准正太分布N(0,1)的随机数
  • torch.normal(mean, std, out=None) 正态分布。mean和std都是tensor,决定返回的形状。mean默认为均值0,std默认标准差为1

tensor的属性有

.data:数据(但建议使用.detach()
.dtype:张量的数据类型 如:torch.FloatTensor
.shape:张量的形状
.device:张量所在的设备
.requires_grad:是否需要求导
.grad:data的梯度
.grad_fn:创建Tensor的function
.is_leaf:是否为叶子结点

tensor的操作

  • torch.cat(seq, dim=0, out=None) 把一堆tensor丢进去,按照dim指定的维度拼接、堆叠在一起.
    例:
a = torch.rand(2,3)
print(a)
b = torch.cat((a,a),dim=0)
print(b)
c = torch.cat((a,a),dim=1)
print(c)

Output:
tensor([[0.6646, 0.8589, 0.5251],
        [0.2515, 0.4547, 0.6944]])
tensor([[0.6646, 0.8589, 0.5251],
        [0.2515, 0.4547, 0.6944],
        [0.6646, 0.8589, 0.5251],
        [0.2515, 0.4547, 0.6944]])
tensor([[0.6646, 0.8589, 0.5251, 0.6646, 0.8589, 0.5251],
        [0.2515, 0.4547, 0.6944, 0.2515, 0.4547, 0.6944]])
  • torch.chunk(tensor, chunks, dim=0) 把tensor切成块,数量由chunks指定,方向由dim决定。
    例:
a = torch.rand(2, 3)
print(a)
b = torch.chunk(a, 2, 0)
print(b)
c = torch.chunk(a, 3, 1)
print(c)

Output:
tensor([[0.6142, 0.8066, 0.9073],
        [0.7619, 0.1243, 0.3439]])
(tensor([[0.6142, 0.8066, 0.9073]]), tensor([[0.7619, 0.1243, 0.3439]]))
(tensor([[0.6142],
        [0.7619]]), tensor([[0.8066],
        [0.1243]]), tensor([[0.9073],
        [0.3439]]))

torch与numpy数组互换:

  • tensor.numpy()torch.from_numpy(array)

torch的运算

  • 加法: a+b或者torch.add(a,b) 若想进行in-place操作(原地计算),只需在add后面加一个。另外,torch里面所有带""的操作,都是指的in-place的操作,torch加减乘除都能带_。
a = torch.rand(3)
b = torch.rand(3)
print(a)
print(b)
c = torch.add(a,b)
b.add_(a)
print(b,c)

Output
tensor([0.5786, 0.5824, 0.7091])
tensor([0.6576, 0.8690, 0.0564])
tensor([1.2362, 1.4514, 0.7655]) tensor([1.2362, 1.4514, 0.7655])
  • 乘法:torch.mul(input, other, out=None)input乘以other
  • 除法:torch.div(input, other, out=None)input除以other
  • 指数:torch.pow(input, exponent, out=None)
  • 开根号:torch.sqrt(input, out=None)
  • 四舍五入到整数:torch.round(input, out=None)
  • argmax函数:torch.argmax(input, dim=None, keepdim=False)返回指定维度最大值的序号,

相关文章

  • pytorch

    1.numpy and torch numpy中是array torch中是tensor torch.from_n...

  • 初识pytorch

    初始pytorch 定义tensor torch.FloatTensor() torch.Tensor() 上面两...

  • pytorch 常用数据类型转换

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

  • torch中的tensor

    tensor,即“张量”。实际上跟numpy数组、向量、矩阵的格式基本一样。但是是专门针对GPU来设计的,可以运行...

  • Pytorch自学笔记

    1.1 创建矩阵 torch.tensor(data) torch.Tensor(dim1,dim2) #也可以是...

  • PyTorch 快速上手

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

  • What is the difference between t

    In PyTorch torch.Tensor is the main tensor class. So all ...

  • pytorch tensor Tensor

    torch.tensor 和torch.Tensortorch.tensor :是一个方法,它根据传递的参数dat...

  • torch.nn.Patameter()详解

    torch.nn.Parameter是torch.Tensor的子类。其主要作用是将不可训练的tensor变成可训...

  • 2020-01-18 一些python技巧

    不定期更新 numpy to torch.tensor torch.from_numpy(xxx) xxx to ...

网友评论

      本文标题:torch中的tensor

      本文链接:https://www.haomeiwen.com/subject/hqdevhtx.html