当前位置: 首页 > news >正文

深度学习常用的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 torchg1 = 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 matha = 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))

在这里插入图片描述

http://www.lryc.cn/news/10194.html

相关文章:

  • 2023年美国大学生数学建模A题:受干旱影响的植物群落建模详解+模型代码(一)
  • PPS文件如何转换成PPT?附两种方法
  • ParallelsDesktop安装【亲测可行】
  • 在 Python 中只接受数字作为用户输入
  • 【集合】JAVA基础篇(二)
  • 机房意外掉电导致Elasticsearch的部分index无数据的修复过程
  • Spring入门案例三:注解进行引用类型的自动装配
  • kubernet + kubevirt + ceph 汇总文档
  • 软件测试项目实战(附全套实战项目教程+视频+源码)
  • Python seek()和tell()函数详解
  • 数据库系统:1. 绪论
  • Android App开发基础
  • 力扣-分数排名
  • 图文详解Ansible中的变量及加密
  • silicon labs平台通过串口升级固件方案
  • MySQL 派生表产生关联索引auto_key0导致SQL非常的慢
  • 计算机网络期末复习汇总(附某高校期末真题试卷)
  • 2月,还是不要跳槽
  • 科技爱好者周刊之爱好者记录
  • C++入门:函数重载
  • 每天10个前端小知识 【Day 16】
  • 23美赛D题:确定联合国可持续发展目标的优先级(ICM)思路Python代码
  • 高校房产管理系统有哪些管理功能范围?
  • ACM MM 相关内容的整理+汇总
  • 前段时间公司招人,面了一个要20K的,一问自动化只会点皮毛···
  • 链表:反转链表、快慢指针、删除链表【零神基础精讲】
  • SQlServer 定时执行sql语句作业的制定
  • Windows安装VMware虚拟机+配置Ubuntu的详细步骤以及解决配置过程中报错的问题(完整版)
  • 103.第十九章 MySQL数据库 -- MySQL的备份和恢复、MySQL主从复制(十三)
  • SSH免密登录以及IP别名配置(保姆级教程)