pytorch的tensor

 PyTorch的Tensor这么简单,你还用不明白吗? - 掘金 (juejin.cn)

Tensor翻译成汉语就是张量:

  • 单个数字叫常量或者标量

  • 一维数组叫向量

  • 二维数组叫矩阵

  • 更高维的叫张量

相对于Numpy,Pytorch的优势在于每个操作都可以在GPU上执行。默认情况下张量都是创建在CPU上的,我们需要显式地将张量移动到GPU上,但是要知道跨设备复制大张量有很大的时间和内存开销。

# 如果GPU可用的话将数据移到GPU上。
if torch.cuda.is_available():
    tensor = tensor.to("cuda")

print(f"Device tensor is stored on: {tensor.device}")

创建张量时候直接将其挪到GPU上有三种写法:

torch.rand(2,3).cuda()

torch.rand(2,3).to("cuda")

torch.rand(2,3, device="cuda")

对tensor输出单行单列的操作:

tensor = torch.rand(3,4)
print(tensor)
print(f"First row: {tensor[0]}") #第一行
print(f"First column: {tensor[:, 0]}") #第一列
print(f"Last column: {tensor[:, -1]}") #最后一列
print(tensor[:, 1]) #第二列

运行结果:
tensor([[0.7533, 0.8648, 0.4569, 0.9276],
        [0.1350, 0.7902, 0.4059, 0.2947],
        [0.0973, 0.2015, 0.9562, 0.8002]])
First row: tensor([0.7533, 0.8648, 0.4569, 0.9276])
First column: tensor([0.7533, 0.1350, 0.0973])
Last column: tensor([0.9276, 0.2947, 0.8002])
tensor([0.8648, 0.7902, 0.2015])

 tensor乘法:

*在pytorch中是对应元素相乘;

import torch
vec = torch.arange(4)
mtx = torch.arange(12).reshape(4,3)
print(vec*vec)
print(mtx*mtx) #这里print(mtx*vec)或者print(vec*mtx)会报错,两个tensor必须在行或者列上保持元素个数的一致

运行结果:
>>
tensor([0, 1, 4, 9])
tensor([[  0,   1,   4],
        [  9,  16,  25],
        [ 36,  49,  64],
        [ 81, 100, 121]])
import torch
vec = torch.arange(4).reshape(4,1) # 增加维度
mtx = torch.arange(12).reshape(4,3)
print(vec*mtx)
print(mtx*vec)

运行结果:
>>
tensor([[ 0,  0,  0],
        [ 3,  4,  5],
        [12, 14, 16],
        [27, 30, 33]])
tensor([[ 0,  0,  0],
        [ 3,  4,  5],
        [12, 14, 16],
        [27, 30, 33]])

 torch.mv(A,B)在pytorch是矩阵相乘,如果A是一个n×m张量,B是一个1×m维张量,将会输出一个n 元 1维张量。实际计算过程中是B×AT,即B×(A的转置);

vec = torch.arange(4)
mtx = torch.arange(12).reshape(3,4)
print(vec)
print(mtx)
print(torch.mv(mtx,vec))

运行结果:
tensor([0, 1, 2, 3])
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]])
tensor([14, 38, 62])

torch.mm(A,B) , 如果A是一个n×m张量,B 是一个 m×p张量,将会输出一个 n×p张量

 torch.dot(A,B),计算两个张量的内积,要求两个张量都为一维向量

import torch
vec = torch.arange(4)
print(torch.dot(vec, vec))

运行结果:
tensor(14)

黑科技@,也是严格按照第一个参数的列数要等于第二个参数的行数 

import torch
vec = torch.arange(4)
mtx = torch.arange(12)
m1 = mtx.reshape(4,3)
m2 = mtx.reshape(3,4)

print(vec @ vec) #vec @ vec==torch.dot(vec,vec)
print(vec @ m1) # 本句直接使用torch.mv()无法执行
print(m2 @ vec) # m2 @ vec==torch.mv(m2,vec)
print(m1 @ m2) # m1 @ m2==torch.mm(m1,m2)

运行结果:
>>
tensor(14)
tensor([42, 48, 54])
tensor([14, 38, 62])
tensor([[ 20,  23,  26,  29],
        [ 56,  68,  80,  92],
        [ 92, 113, 134, 155],
        [128, 158, 188, 218]])

torch.matmul(),matmul不自局限于一二维,可以进行高维张量的乘法 

vec = torch.arange(3)
mtx = torch.arange(12).reshape(3,4)
print(torch.matmul(vec,mtx))
print(torch.matmul(vec,vec))
print(torch.matmul(mtx.T,mtx))
print(torch.matmul(mtx.T,vec))

运行结果:
>>
tensor([20, 23, 26, 29])
tensor(5)
tensor([[ 80,  92, 104, 116],
        [ 92, 107, 122, 137],
        [104, 122, 140, 158],
        [116, 137, 158, 179]])
tensor([20, 23, 26, 29])

tensor.add_(),执行张量的加法运算

tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor, "\n")
tensor.add_(5)
print(tensor)

运行结果:
tensor([[1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.],
        [1., 0., 1., 1.]])

tensor([[6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.],
        [6., 5., 6., 6.]])

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值