1、张量支持的数据类型

  

Pytorch学习笔记-(三)张量_NumPy

(1)获取/设置Pytorch默认的张量类型
import torch
# 2、张量Tensor

# 2.1.1、获取Pytorch的默认类型
def DefaultType_func():
    dtype=torch.tensor([1,2,3.4]).dtype
    print("张量Tensor的默认类型为:",dtype)
    print("")

# 2.1.2、设置Pytorch的默认类型
def SetDefaultType_func():
    torch.set_default_dtype(torch.float32)
    print("张量Tensor的默认类型被设置为:torch.float32")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

Pytorch学习笔记-(三)张量_NumPy_02

2、张量生成

# 2.2、生成张量
def CreateTensor_Test():
    print("方式一(需要赋值):")
    A=torch.tensor([1, 2])
    print("A大小为:",A.size())
    print("A的维度为:",A.shape)
    print("A中的元素个数为:",A.numel())
    A2 = torch.tensor([1, 2],dtype=torch.float32,requires_grad=True)  #使用requires_grad来启用“可计算每个元素的梯度” 注:只有浮点型可以计算梯度。

    print("方式二(赋值或指定大小):")
    B=torch.Tensor([3, 4])  #获取大小、维度的方法同上
    B2 = torch.Tensor(2,3)  # 生成一张2*3大小的张量

    print("方式三(指定大小或赋值):")
    # 其他方法
    torch.zeros(3,3)                         # 3*3的全0张量
    torch.ones(3, 3)                         # 3*3的全1张量
    torch.eye(3, 3)                          # 3*3的单位张量
    torch.full((3, 3),fill_value=0.25)  # 3*3的张量,并全都填充0.25
    torch.empty(3, 3)                        # 3*3的空张量

    print("方式四(其他方式):")
    torch.arange(start=0,end=10,step=2)  # 使用torch.arange()生成指定步长的张量;start开始位置,end结束位置,step步长
    torch.linspace(start=1, end=10, steps=5)  # 使用torch.linspace()生成等间距的张量;start开始位置,end结束位置,steps元素个数
    torch.logspace(start=0.1, end=1, steps=5)  # 使用torch.logspace()生成以对数为间距的张量;start开始位置,end结束位置,steps元素个数

    # 随机生成张量的元素的值
    torch.manual_seed(123)   # ①设置随机数种子
    C1=torch.rand(3,4)       # ②随机生成一个3*4的张量
    C2 = torch.randperm(10)  # ②随机生成一个的张量,元素值在0-10间随机生成
    print("随机值张量C1:",C1)
    print("随机值(0~10)张量C2:",C2)

    # **_like()系列方法-复制张量维度
    B3 = torch.ones_like(B2)   # 生成一张与B2维度相同的全1的张量
    B4 = torch.zeros_like(B2)  # 生成一张与B2维度相同的全0的张量
    B5 = torch.rand_like(B2)   # 生成一张与B2维度相同的随机张量

    # new_**()系列方法-复制张量类型
    B3 = B2.new_full((3,3),fill_value=1)   # 生成一张与B2类型相似但尺寸不同的张量,并所有元素赋值为1
    B4 = B2.new_empty((3,3))                    # 生成一张与B2类型相似但尺寸不同的的空张量
    B5 = B2.new_zeros((3,3))                    # 生成一张与B2类型相似但尺寸不同的的全0的张量
    B5 = B2.new_ones((3,3))                     # 生成一张与B2类型相似但尺寸不同的的全1的张量

    print("")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.

3、张量操作

(1)类型转化-张量类型间转化
# 2.3.1、类型转化示例
def TypeConvert_Test():
    # 类型转化 - 张量类型间转化
    a=torch.tensor([1.1,2.2])
    print("a.dtype:",a.dtype)
    print("a.long()方法:", a.long().dtype)
    print("a.float()方法:", a.float().dtype)
    print("a.int()方法:", a.int().dtype)
    torch.set_default_dtype(torch.float32)
    print("")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

Pytorch学习笔记-(三)张量_NumPy_03

(2)类型转化-张量与NumPy类型
# 2.3.1、类型转化示例
def TypeConvert_Test():

    # 类型转化 - 张量与NumPy类型
    # NumPy->张量
    numPy1=np.ones((3,3))
    print("方式一:")
    tensor1=torch.from_numpy(numPy1)
    print("tensor1:",tensor1)

    print("方式二:")
    tensor2=torch.as_tensor(numPy1)
    print("tensor2:",tensor2)

    # 张量->NumPy
    numPy2=tensor2.numpy()
    print("numPy2:",numPy2)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

Pytorch学习笔记-(三)张量_类型转化_04

(3)改变张量的形状
# 2.3.2、改变张量的形状
def ChangeShape_Test():

    #2.3.2.1、 改变形状
    # 通过reshape()设置形状,range用于生成指定步长的张量;12->3*4
    A=torch.arange(12.0).reshape(3,4)  # 写法一
    print("A:",A)
    torch.reshape(input=A,shape=(2,-1))  # 写法二
    print("A:",A)

    #torch.resize_方法改变形状
    A.resize_(2,5)
    print("A:",A)

    # 将A的形状设置为与B相同的形状 resize_as_
    B=torch.arange(10.0,22.0).reshape(2,6)
    A.resize_as_(B)
    print("B:",B)
    print("A:", A)

    #2.3.2.2、 拓展维度
    AAA=torch.arange(3)
    BBB=AAA.expand(3,-1)  # 指定拓展为的形状
    print("BBB:",BBB)
    CCC=torch.arange(6).reshape(2,3)
    DDD=AAA.expand_as(CCC)  # 将A的形状拓展为C的形状 expand_as
    print("DDD:",DDD)

    F=A.repeat(1,2,2)  # 使用.repeat()方法按照指定的形状进行重复填充
    print("F:",F)
    print("F维度为:",F.shape)

    #2.3.2.3、 升维torch.unsqueeze()与降维torch.squeeze()
    AA=torch.arange(12.0).reshape(2,6)
    BB=torch.unsqueeze(AA,dim=0)  # 升维-写法一
    print("BB维度为:",BB.shape)
    CC=BB.unsqueeze(dim=3)        # 升维-写法二
    print("CC维度为:",CC.shape)
    DD=torch.squeeze(CC,dim=0)  # 降维-写法一
    print("DD维度为:",DD.shape)
    F=DD.squeeze(dim=2)        # 降维-写法二
    print("F:", F)
    print("F维度为:",F.shape)

    print("")
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
(4)获取张量中的元素

 

(5)拼接与拆分

 

4、张量计算

 

作者:꧁执笔小白꧂