pytorch入门笔记(二)

笔记为以下函数的实例

**torch.manual_seed **-------生成随机数种子,并且使得每次得到的随机数是固定的
张量的拼接,切分,索引,变换,压缩:
torch.cat
torch.stack
torch.chunk
enumerate
torch.split
torch.index_select
torch.masked_select
torch.reshape
torch.transpose
torch.squeeze

张量的数学运算,以add为代表
torch.add

import torch

# 生成随机数种子,并且使得每次得到的随机数是固定的
torch.manual_seed(1)

# ========================================================= example 1
# torch.cat是将两个张量(tensor)拼接在一起,cat是concatnate的意思,即拼接,联系在一起。
# 使用torch.cat((A,B),dim)时,除拼接维数dim数值可不同外其余维数数值需相同,方能对齐。
# torch.cat


flag = False
if flag:
    t = torch.ones((3, 5))
    t_0 = torch.full((4, 5), 8)
    t_cat = torch.cat((t, t_0), dim=0)
    print("t:{} shape:{}\nt_0:{} shape:{}\nt_cat:{} shape:{} "
          .format(t, t.shape, t_0,t_0.shape,t_cat,t_cat.shape))

    t1 = torch.full((5, 2), 8)
    t2 = torch.full((5, 5), 4)
    tcat = torch.cat((t1, t2), dim=1)
    print("t1:{} shape:{} \n t2:{} shape:{} \n tcat:{} shape:{}".format(t1, t1.shape, t2, t2.shape, tcat, tcat.shape))

# ============================================================ example 2
# torch.stack 增加新的维度进行堆叠
flag = False

if flag:
    # t1 维度为1*3*3,第一维为整个矩阵,第二维为每一行,第三维为每一列
    t1 = torch.tensor(([1, 2, 3], [4, 5, 6], [7, 8, 9]))
    # t2 维度为1*3*3,第一维为整个矩阵,第二维为每一行,第三维为每一列
    t2 = torch.tensor(([3, 2, 1], [6, 5, 4], [9, 8, 7]))
    # 进行维度叠加,
    # dim = 0 / dim = -3 表示将两个张量的第一维进行叠加
    # dim = 1 / dim = -2 表示将两个张量的第二维进行叠加,即矩阵的行
    # dim = 2 / dim = -1 表示将两个张量的第三维进行叠加,即矩阵的列
    # 如下,torch.stack取出t1的第一行与t2的第一行组成一个新的矩阵,作为t_stack的第一维度,
    #                  取出t1的第二行与t2的第二行组成一个新的矩阵,作为t_stack的第二维度
    #                  取出t1的第三行与t2的第三行组成一个新的矩阵,作为t_stack的第三维度
    t_stack = torch.stack([t1, t2], dim=1)
    print("\nt1:{} shape:{}".format(t1, t1.shape))
    print("\nt2:{} shape:{}".format(t2, t2.shape))
    print("\nt_stack:{} shape:{}".format(t_stack, t_stack.shape))

# ============================================================== example 3
# torch.chunk
# 将tensor按dim(行或列)平均分割成chunk_num个tensor块,返回的是一个元组。
# 如果指定轴的元素个数被chunks除不尽,那么最后一块的元素个数变少
flag = False

if flag:
    t1 = torch.tensor(([1, 2, 3], [4, 5, 6], [7, 8, 9]))
    t2 = torch.tensor(([3, 2, 1], [6, 5, 4], [9, 8, 7]))
    tcat = torch.cat((t1, t2), dim=1)
    # 将tcat按dim = 1即列分割成6个tensor块,同理,dim = 0 为行
    t_chunk = torch.chunk(tcat, 6, 1)
    print("\ntcat:{} shape:{}".format(tcat, tcat.shape))
    print(t_chunk)
    # enumerate() 函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,
    # 同时列出数据和数据下标,一般用在 for 循环当中。
    for idx, t in enumerate(t_chunk):
        print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
# ================================================================example 4
# torch.split
# torch.split(tensor, split_size_or_sections, dim=0) #将tensor 拆分成相应的组块
# split的split_size_or_sections 表示每一个组块中的数据大小,chunks表示组块的数量
flag = False
if flag:
    t1 = torch.tensor(([1, 2, 3], [4, 5, 6], [7, 8, 9]))
    t2 = torch.tensor(([3, 2, 1], [6, 5, 4], [9, 8, 7]))
    tcat = torch.cat((t1, t2), dim=1)
    # 将tcat按行分割,并且分割后的每个组块大小为一
    t_split = torch.split(tcat, 2, dim=0)
    for idx, t in enumerate(t_split):
        print("分割后第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
# ================================================================ example 5
# torch.index_select 在dim上,按index索引数据
flag = False
if flag:
    t = torch.tensor(([1, 2, 3], [4, 5, 6], [7, 8, 9]))
    # 索引t的第二列
    idx = torch.tensor([1], dtype=torch.long)
    t_select = torch.index_select(t, 1, idx)
    print("t:\n{}\nt_select:\n{}".format(t, t_select))

# ================================================================ example 6
# torch.masked_select
# 按mask中的True进行索引,即将满足条件的点选出来。
# ge >=
# gt >
# le <=
# lt <
# mask跟input元素数目相同,维度不需要相同

flag = False
if flag:
    t = torch.tensor(([1, 2, 3], [4, 5, 6], [7, 8, 9]))
    mask = t.ge(5)
    t_masked_select = torch.masked_select(t, mask)
    print("t:\n{}\nmask:\n{}\nt_masked_select:\n{}".format(t, mask, t_masked_select))

# ===============================================================example 7
# torch.reshape  变换形状 当张量在内存中连续,新张量与input共享内存
# torch.transpose 交换张量两个维度,当dim=0,dim=1时为转置
# torch.squeeze 压缩长度为一的维度
# torch.add     张量数学运算

flag = False

if flag:
    t = torch.randperm(12)
    t_reshape = torch.reshape(t, (2, 3, 2))
    print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
    # 验证共享内存
    t[0] = 1024
    print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
    print("t.data 内存地址:{}".format(id(t.data)))
    print("t_reshape.data 内存地址:{}".format(id(t_reshape.data)))

flag = False

if flag:
        # torch.transpose
        t = torch.rand((2, 3, 4))
        t_transpose = torch.transpose(t, dim0=1, dim1=2)  # c*h*w     h*w*c
        print("t shape:{}\nt_transpose shape: {}".format(t.shape, t_transpose.shape))

flag = False

if flag:
    t = torch.rand((1, 2, 3, 1))
    # 去掉指定的定的维数为一的维度。
    t_sq = torch.squeeze(t)
    t_0 = torch.squeeze(t, dim=0)
    t_1 = torch.squeeze(t, dim=1)
    print(t.shape)
    print(t_sq.shape)
    print(t_0.shape)
    print(t_1.shape)

flag = True

if flag:
    t_0 = torch.randn((3, 3))
    t_1 = torch.ones_like(t_0)
    t_add = torch.add(t_0, 10, t_1)

    print("t_0:\n{}\nt_1:\n{}\nt_add_10:\n{}".format(t_0, t_1, t_add))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值