Pytorch学习笔记day2——Tensor运算

那么今天,我们来学习一下张量运算。笔者最早接触AI还是在tensorflow的时代。张量tensor本身,可以理解成向量和矩阵的一个推广。

一维张量就是向量:A[i]
二维张量就是矩阵:A[i][j]
三维张量就是…就是三维张量…嗯总不能每个维度都起个名字吧:A[i][j][k]

张量本身有很多算子,但是一般AI里面出现的张量计算都会比较简单,加减乘除差不多了。这里贴一个torch的API网址
https://pytorch.org/docs/stable/torch.html

今天的主要目标,就是要把张量的生成,取数,基本运算都学会。最后,我们要试一试我们的CUDA是不是能用了,稍微试试看GPU到底比CPU快多少。

啊,开始之前,要感谢大自然的馈赠。今天测试用的代码,大部分是百度的AI工具生成的。
在这里插入图片描述

张量的初始化

直接贴代码

import torch
import numpy as np

x = torch.empty(2, 3)       #生成空张量,元素值和运行环境相关
x = torch.rand(2, 3)         #生成随机张量
x = torch.zeros(2, 3, dtype=torch.float)   #生成全0张量
x = torch.ones(2, 3)     #生成全1张量
x = torch.eye(3,2)     #生成对角为1张量
x = torch.full((3,3), 0.25)  #生成全为指定值的张量
x = torch.arange(start=0, end=5, step=2)  #生成一定范围内的向量

#生成和A尺寸相同的张量, A必须是张量否则报错
A = torch.rand(2, 3)
x = torch.ones_like(A)
x = torch.zeros_like(A)
x = torch.rand_like(A)

print(A.shape)
print(A.size())
print(A.size(0))
print(A.size(1))
print(A.numel())

运行结果如下

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

实际上spyder里面打开一个tensor,是能看到里面所有的接口的。不过这些接口都是啥意思,就要慢慢熟悉和翻API手册了
在这里插入图片描述

张量的取值

矩阵运算,私以为matlab是最好用的,毕竟人家都叫mat lab了对吧。但是tensor的类似运算,也需要熟悉
推荐看看这个

直接贴代码

import torch
import numpy as np

A = [[11,12,13,14],[21,22,23,24],[31,32,33,34]]
print(A)

x = torch.tensor(A)
print(x)
print("="*40)

# 取单个值
print(x[0][0])
print(x[0][3])
print(x[0,0])
print(x[0,3])
print("="*40)

# 取某行某列
print(x[0])
print(x[:,0])
print("="*40)

# 某行,列拼起来
print(x[[0,2]])
print(x[:,[1,3]])
print("="*40)

# 用矩阵取数
ind2 = [[0, 1, 2],[1, 2, 3]]   # 3*4[2*3] = 3  取下标
ind3 = [[[0, 1, 2],[0, 1, 2]]]   # 3*4[1*2*3] = 2*3*4  取行
ind4 = [[[0,0],[0,0]],[[0,0],[0,0]]]  #3*4[1*2*2*2] = 2*2 取下标
ind5 = [[[[0,0],[0,0]],[[0,0],[0,0]]]]  #3*4[1*1*2*2*2] = 2*2*2*4  取行

print(x[ind2].shape)
print(x[ind3].shape)
print(x[ind4].shape)
print(x[ind5].shape)

print(x[ind2])
print(x[ind3])
print(x[ind4])
print(x[ind5])

结果如下,看起来在取行的下标也是张量形式时,规则会比较复杂,不过暂时不做过多探索。需要用到再考虑

[[11, 12, 13, 14], [21, 22, 23, 24], [31, 32, 33, 34]]
tensor([[11, 12, 13, 14],
        [21, 22, 23, 24],
        [31, 32, 33, 34]])
========================================
tensor(11)
tensor(14)
tensor(11)
tensor(14)
========================================
tensor([11, 12, 13, 14])
tensor([11, 21, 31])
========================================
tensor([[11, 12, 13, 14],
        [31, 32, 33, 34]])
tensor([[12, 14],
        [22, 24],
        [32, 34]])
========================================
torch.Size([3])
torch.Size([2, 3, 4])
torch.Size([2, 2])
torch.Size([2, 2, 2, 4])
tensor([12, 23, 34])
tensor([[[11, 12, 13, 14],
         [21, 22, 23, 24],
         [31, 32, 33, 34]],

        [[11, 12, 13, 14],
         [21, 22, 23, 24],
         [31, 32, 33, 34]]])
tensor([[11, 11],
        [11, 11]])
tensor([[[[11, 12, 13, 14],
          [11, 12, 13, 14]],

         [[11, 12, 13, 14],
          [11, 12, 13, 14]]],


        [[[11, 12, 13, 14],
          [11, 12, 13, 14]],

         [[11, 12, 13, 14],
          [11, 12, 13, 14]]]])

张量的基本运算

直接贴代码

import torch
import numpy as np


import torch
 
# 创建两个tensor
a = torch.tensor([1, 2, 3, 4, 5, 6])
b = torch.tensor([7, 8, 9, 10, 11, 12])
 
# 四则运算
c_add = a + b
c_sub = a - b
c_mul = a * b
c_div = a / b
 
print(c_add)
print(c_sub)
print(c_mul)
print(c_div)
print("\n")

# 矩阵计算
c_mul = torch.mul(a,b)
c_matmul = torch.matmul(a,b.T)

print(c_mul)
print(c_matmul)
print("\n")

# 向量重新整理尺寸,元素按照右侧下标连续
d = a.view(2,3)
e = b.reshape(3,2)

print(d)
print(e)
print("\n")

f_mul = torch.mul(d,e.T)
f_matmul = torch.matmul(d, e)

print(f_mul)
print(f_matmul)

输出结果如下

tensor([ 8, 10, 12, 14, 16, 18])
tensor([-6, -6, -6, -6, -6, -6])
tensor([ 7, 16, 27, 40, 55, 72])
tensor([0.1429, 0.2500, 0.3333, 0.4000, 0.4545, 0.5000])


tensor([ 7, 16, 27, 40, 55, 72])
tensor(217)


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


tensor([[ 7, 18, 33],
        [32, 50, 72]])
tensor([[ 58,  64],
        [139, 154]])
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值