深度学习常用的python函数(一)

由于我只简单的学过python和pytorch,其中有很多函数的操作都还是一知半解的,其中有些函数经常见到,所以就打算记录下来。

1.zip

zip(*a):针对单个可迭代对象压缩成n个元组,元组数量n等于min(a中元素的最小长度)

a = [(1, 2), (3, 4), (5, 6)]
c, d = zip(*a)
print(c, d)
# (1, 3, 5) (2, 4, 6)

2.view, arange和range

import torch
# torch.arange(1, 3) =[1,3)   前闭后开
# torch.range(1, 3) =[1,3]    前闭后闭
e = torch.arange(1, 3)
e1 = torch.range(1, 3)
e, e1  # (tensor([1, 2]), tensor([1., 2., 3.]))

view的作用就是用来组织Tensor的形状,

import torch
e = torch.arange(1, 19).view(2, 3, 3)
e1 = torch.range(1, 18).view(2, 3, 3)
e,e1,e==e1

输出结果如下
在这里插入图片描述

3.torch.transpose 用来交换维度(重新组织维度)

torch.Size([1, 3, 2])–>transpose(0,1)后 torch.Size变为([3,1,2])
有两种写法 ① b.transpose(0, 1) ② torch.transpose(b, 0, 1),我觉得都可以使用

import torch
#https://pytorch.org/docs/stable/generated/torch.transpose.html#torch.transpose
b = torch.Tensor([[[0, 1], [2, 3], [4, 5]]])
print(b.shape)
print('----------')
b=b.transpose(0, 1)
print(b.shape)
print('----------')
c = torch.transpose(b, 0, 1)
print(c.shape)
# print(b)
# print(c)

在这里插入图片描述

4.torch.cat

torch.cat()是为了把多个tensor进行拼接而存在的(这里仅仅用了相同的元素)

# 3.torch.cat
# dim(整数,可选)– 张量被控制的维度
# m 默认为0  代表在竖向的方向上添加
# [c,
#  c,
#  c]
# m为1代表在横向的方向上添加[c,c,c]
c = torch.Tensor([[0, 1], [2, 3], [4, 5]])
# print(c)
print(c.shape)
print('--------------')
c1 = torch.cat((c, c, c), dim=0)  
# print(c1)
print(c1.shape)
print('--------------')
c2 = torch.cat((c, c), dim=1)  #  [c,c]
# print(c2)
print(c2.shape)
# print(c2.shape[1])

5.数组操作

这部分的操作比较复杂,有的三维数组的操作,就像这样的[:::],大家可以自己复制尝试一下,查看输出

# 4.数组操作
#  enc_hidden[-2,:,:], enc_hidden[-1,:,:]
d = torch.Tensor([[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
                  [[10, 11, 12], [13, 14, 15], [16, 17, 18]]])
print(d.size())  # torch.Size([2, 3, 3])
print(d)
# tensor([[[ 1.,  2.,  3.],
#          [ 4.,  5.,  6.],
#          [ 7.,  8.,  9.]],
#
#         [[10., 11., 12.],
#          [13., 14., 15.],
#          [16., 17., 18.]]])
print('------1---------')
print(d[-1, :, :])

print('------2--------')
print(d[:, -1, :])

print('------3--------')
print(d[:, :, -1])

print('------4--------')
print(d[:, :, -2])

print('------5--------')
print(d[-1])
print(d[:, :, -1])
print(d[-1, :, :])
print(d[:, -1, :])

6.squeeze()和unsqueeze()

squeeze在的中文意思压缩,unsqueeze取消压缩,unsqueeze是添加维度的意思

特别的,当unsqueeze()里面参数是0 的时候,该矩阵由(3,4)变成是(3,4,1)

e = torch.arange(1, 19).view(2, 3, 3)
print(e)
print(e.shape)
print('-----unsqueeze:就是在某个位置增加一个维度-------')
ee = e.unsqueeze(3)
print(ee)
print(ee.shape)

在这里插入图片描述在这里插入图片描述

# 5.squeeze()和unsqueeze()
#   squeeze在的中文意思压缩,就是降低维度,squeeze()函数只能压缩维度为1的矩阵
#  当unsqueeze()里面参数是0 的时候,该矩阵由(3,4)变成是(3,4,1)
# 对形如(2, 3, 3)的矩阵squeeze不起作用,但不会报错
e = torch.arange(1, 19).view(2, 3, 3)
print(e)
print(e.shape)
print('-----squeeze:就是在某个位置降低一个维度-------')
eee = e.squeeze(0)
print(eee)
print(eee.shape)

在这里插入图片描述

7.torch.bmm 计算两个tensor的矩阵乘法

torch.bmm(input, mat2, *, out=None)
input and mat2 must be 3-D tensors each containing the same number of matrices.

If input is a (b×n×m) tensor, mat2 is a (b×m×p) tensor, out will be a (b×n×p) tensor.

两个tensor的第一维是相等的,然后第一个数组的第三维和第二个数组的第二维度要求一样,对于剩下的则不做要求,输出维度

import torch

g1 = torch.arange(1, 7).view(1, 2, 3)
print(g1.shape)
print(g1)
g2 = torch.arange(1, 10).view(1, 3, 3)
print(g2)
print('-------')

torch.bmm(g1, g2)

在这里插入图片描述

8.torch.exp(x) 计算e(2.7)的x次方

分别计算e^0(e的0次方), e的1次方,e的log(2)次方

import math

a = torch.tensor([0, 1, math.log(2)])
print(torch.exp(a))

在这里插入图片描述

9.torch.max

求最大值

a = torch.randn(4, 4)
print(a)
print(torch.max(a, dim=1))
print('----------')
print(torch.max(a, dim=1, keepdim=True))

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值