Pytorch第三课:package-torch.Tensor包详解

本文详细介绍了Pytorch中的torch.Tensor,包括张量的类型、创建方式、索引与切块以及各种操作,如张量的转换、复制、属性获取、形状调整等。并特别强调了带下划线的操作会直接修改原张量。
摘要由CSDN通过智能技术生成

微博:https://weibo.com/wangxiaocaoai/profile?rightmod=1&wvr=6&mod=personinfo
微信公众号:搜索"AI躁动街"


本节要点:

1 张量的类型
2 张量的创建
3 张量的索引与切块
4 张量的操作
4.1 操作名有无下划线的区别
4.2 torch.Tensor所有操作列表
4.3 torch.Tensor操作举例感受
torch.Tensor是一种包含单一数据类型元素的多维矩阵。

1 张量的类型

Torch定义了七种CPU tensor类型和八种GPU tensor类型(如下表):

torch.Tensor是默认的tensor类型(torch.FlaotTensor)的简称
在这里插入图片描述

2 张量的创建

通过torch包的函数进行张量的创建在《Pytorch第一课:package-torch(1)之张量初识》一文中的第2节有详细介绍。

这里介绍的是torch.Tensor创建张量,有以下几类创建方式:
class torch.Tensor
class torch.Tensor(*sizes)
class torch.Tensor(size)
class torch.Tensor(sequence)
class torch.Tensor(ndarray)
class torch.Tensor(tensor)
class torch.Tensor(storage)

# 先导入torch包
import torch

2.1 无参数创建

如果没有提供参数,将会返回一个空的零维张量

class torch.Tensor

a = torch.Tensor()
print(a)
tensor([])

2.2 从规定其大小创建

class torch.Tensor(*sizes)

class torch.Tensor(size)

# 创建3*4大小的Int类型的张量
a = torch.IntTensor(3, 4)
print(a)

# 也可以指定填充的值为0
a = torch.IntTensor(3, 4).zero_()
print(a)
tensor([[ 0.0000e+00, -8.0531e+08,  1.3174e+09,  5.3687e+08],
        [-3.9761e+08,  3.2644e+04, -3.9720e+08,  3.2644e+04],
        [ 3.0850e+08,  1.0000e+00,  0.0000e+00,  1.9661e+05]], dtype=torch.int32)
tensor([[ 0,  0,  0,  0],
        [ 0,  0,  0,  0],
        [ 0,  0,  0,  0]], dtype=torch.int32)

2.3 从python的List序列创建

如果提供了python序列,将会从序列的副本创建一个tensor。

class torch.Tensor(sequence)

a = torch.Tensor([[1,2,3], [4,5,6]])
print(a)
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]])

2.4 从numpy创建

class torch.Tensor(ndarray)

import numpy as np
a_np = np.arange(1,10)
print(a_np)
a = torch.Tensor(a_np)
print(a)
[1 2 3 4 5 6 7 8 9]
tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

2.5 从已有张量中创建

如果提供了torch.Tensor或torch.Storage,将会返回一个有同样参数的tensor.

class torch.Tensor(tensor)

class torch.Tensor(storage)

a = torch.Tensor([1,2,3])
b = torch.Tensor(a)
print(b)
tensor([ 1.,  2.,  3.])

每一个张量tensor都有一个相应的torch.Storage用来保存其数据。类tensor提供了一个存储的多维的、横向视图,并且定义了在数值运算。

3 张量的索引与切块

张量的索引与切块在《Pytorch第一课:package-torch(1)之张量初识》一文中的第3节有详细介绍。此处介绍可以用python的索引和切片来获取和修改一个张量tensor中的内容。

# 创建一个张量
a  = torch.Tensor([[1,2,3], [4,5,6]])
print(a)

# 通过索引获取张量的内容
print(a[1][2])

# 修改张量的内容
a[1][2] = 10
print(a)
tensor([[ 1.,  2.,  3.],
        [ 4.,  5.,  6.]])
tensor(6.)
tensor([[  1.,   2.,   3.],
        [  4.,   5.,  10.]])

4 张量的操作

《Pytorch第二课:package-torch(2)之数学操作》一文中有详细介绍了torch包中对于张量的操作。

本章要介绍的是torch.Tensor包中的张量的操作。两者的操作大部分是相同的功能,可以互相参考,torch包没有的功能,在4.3节会详细举例介绍。

注意:会改变tensor的函数操作会用一个下划线后缀来标示。比如,torch.FloatTensor.abs_()会在原地计算绝对值,并返回改变后的tensor,而tensor.FloatTensor.abs()将会在一个新的tensor中计算结果。

4.1 有无下划线的比较

给一个简单的例子来体会一下有无下划线的区别:

# 创建一个tensor
a = torch.tensor([-1,-2,3])
print(a)

# 直接在原tensor上做改变
a.abs_()
print(a)
tensor([-1, -2,  3])
tensor([ 1,  2,  3])
# 创建一个tensor
a = torch.tensor([-1,-2,3])
print(a)

# 并不会改变a
a.abs()  
print(a)

b = a.abs()
print(b)
tensor([-1, -2,  3])
tensor([-1, -2,  3])
tensor([ 1,  2,  3])

4.2 所有torch.Tensor的操作

注意,不要将以下torch.Tensor的操作操作和torch包中的同名操作混淆,torch.Tensor的操作是可以直接作用在原张量上的。

比如.byte(),.char()直接作用在tensor上进行原tensor的修改:

a = torch.Tensor([1,2,3]).byte()
print(a.type())

a = torch.Tensor([1,2,3]).char()
print(a.type())

但是同名操作的功能是相同的,可以参见torch包。以下是所有torch.Tensor的操作:
abs() → Tensor
abs_() → Tensor
acos() → Tensor
acos_() → Tensor
add(value)
add_(value)
addbmm(beta=1, mat, alpha=1, batch1, batch2) → Tensor
addbmm_(beta=1, mat, alpha=1, batch1, batch2) → Tensor
addcdiv(value=1, tensor1, tensor2) → Tensor
addcdiv_(value=1, tensor1, tensor2) → Tensor
addcmul(value=1, tensor1, tensor2) → Tensor
addcmul_(value=1, tensor1, tensor2) → Tensor
addmm(beta=1, mat, alpha=1, mat1, mat2) → Tensor
addmm_(beta=1, mat, alpha=1, mat1, mat2) → Tensor
addmv(beta=1, tensor, alpha=1, mat, vec) → Tensor
addmv_(beta=1, tensor, alpha=1, mat, vec) → Tensor
addr(beta=1, alpha=1, vec1, vec2) → Tensor
addr_(beta=1, alpha=1, vec1, vec2) → Tensor
apply_(callable) → Tensor
asin() → Tensor
asin_() → Tensor
atan() → Tensor
atan2() → Tensor
atan2_() → Tensor
atan_() → Tensor
baddbmm(beta=1, alpha=1, batch1, batch2) → Tensor
baddbmm_(beta=1, alpha=1, batch1, batch2) → Tensor
bernoulli() → Tensor
bernoulli_() → Tensor
bmm(batch2) → Tensor
byte() → Tensor
bmm(median=0, sigma=1, *, generator=None) → Tensor
ceil() → Tensor
ceil_() → Tensor
char()
chunk(n_chunks, dim=0) → Tensor
clamp(min, max) → Tensor
clamp_(min, max) → Tensor
clone() → Tensor
contiguous() → Tensor
copy_(src, async=False) → Tensor
cos() → Tensor
cos_() → Tensor
cosh() → Tensor
cosh_() → Tensor
cpu() → Tensor
cross(other, dim=-1) → Tensor
cuda(device=None, async=False)
cumprod(dim) → Tensor
cumsum(dim) → Tensor
data_ptr() → int
diag(diagonal=0) → Tensor
dim() → int
dist(other, p=2) → Tensor
div(value)
div_(value)
dot(tensor2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值