mxnet - reshape操作完全解析(理解0,-1,-2,-3,-4)

2024-04-24 11:08

本文主要是介绍mxnet - reshape操作完全解析(理解0,-1,-2,-3,-4),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一般来说,同一个操作,mxnet的ndarry和symbol都会有,分别对应动态图和静态图,比如reshape,可以调用 mx.nd.reshape,或者调用 mx.sym.reshape。下面对reshape这个操作进行解析,以mx.nd.reshape作为参考。

reshape的注释

reshape(data=None, shape=_Null, reverse=_Null, target_shape=_Null, keep_highest=_Null, out=None, name=None, **kwargs)Reshapes the input array... note:: ``Reshape`` is deprecated, use ``reshape``Given an array and a shape, this function returns a copy of the array in the new shape.The shape is a tuple of integers such as (2,3,4). The size of the new shape should be same as the size of the input array.Example::reshape([1,2,3,4], shape=(2,2)) = [[1,2], [3,4]]Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below:- ``0``  copy this dimension from the input to the output shape.Example::- input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2)- input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4)- ``-1`` infers the dimension of the output shape by using the remainder of the input dimensionskeeping the size of the new array same as that of the input array.At most one dimension of shape can be -1.Example::- input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4)- input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8)- input shape = (2,3,4), shape=(-1,), output shape = (24,)- ``-2`` copy all/remainder of the input dimensions to the output shape.Example::- input shape = (2,3,4), shape = (-2,), output shape = (2,3,4)- input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4)- input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1)- ``-3`` use the product of two consecutive dimensions of the input shape as the output dimension.Example::- input shape = (2,3,4), shape = (-3,4), output shape = (6,4)- input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20)- input shape = (2,3,4), shape = (0,-3), output shape = (2,12)- input shape = (2,3,4), shape = (-3,-2), output shape = (6,4)- ``-4`` split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1).Example::- input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4)- input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4)If the argument `reverse` is set to 1, then the special values are inferred from right to left.Example::- without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be (40,5)- with reverse=1, output shape will be (50,4).

reshape传入的一个参数shape元组,元组中的数字可以非0正数,或者是0,-1,-2,-3,-4 这些奇怪的输入,下面讲讲这些参数的意义。

0

0起一个占位符的作用,默认从左到右进行占位(除非传入reverse=1,则从右到左),维持原数组在该位置的维度。

  • input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2) # 中间维度维持不变
  • input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4) # 后两个维度维持不变

-1

-1是最后进行推导的,先保证其他数字被照顾好之后,在reshape前后数组的size不变的约束下,推导出该位置的维度。通常来说,最多只有一个-1,但是在有 -4 的情况下,可以有两个 -1。

  • input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4)
  • input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8)
  • input shape = (2,3,4), shape=(-1,), output shape = (24,)

-2

-2和-1不同,-2可以包括多个维度。当其他位置都有对应的维度之后,-2就来容纳剩下的多个维度。

  • input shape = (2,3,4), shape = (-2,), output shape = (2,3,4) # -2来容纳所有的维度
  • input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4) # 2占据了一个维度,-2容纳剩下的(3,4)
  • input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1) # (1,1)是新增的两个维度,-2将(2,3,4)给容纳

-3

-3是将对应的两个维度合成一个维度,合成之后的维度值为之前两个维度的乘积。

  • input shape = (2,3,4), shape = (-3,4), output shape = (6,4)
  • input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20)
  • input shape = (2,3,4), shape = (0,-3), output shape = (2,12)
  • input shape = (2,3,4), shape = (-3,-2), output shape = (6,4)

-4

-4和-3不同,-4是将一个维度拆分为两个,-4后面跟两个数字,代表拆分后的维度,其中可以有-1。

  • input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4) # 将2拆分为1X2,剩下的3,4传递给-2
  • input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4) # 将3拆分为1X3,剩下的4传递给-2

reverse

If the argument `reverse` is set to 1, then the special values are inferred from right to left.Example::- without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be (40,5)- with reverse=1, output shape will be (50,4).

一个例子:GN的实现

class GroupNorm(mx.gluon.HybridBlock):r"""Group Normalizationrefer to paper <Group Normalization>"""def __init__(self,in_channels,groups=32,gamma_initializer='ones',beta_initializer='zeros',**kwargs):super(GroupNorm, self).__init__(**kwargs)self.groups = min(in_channels, groups)assert in_channels % self.groups == 0, "Channel number should be divisible by groups."attrs = SpecialAttrScope.current.attrsself.mirroring_level = attrs.get('mirroring_level', 0)self.eps = attrs.get('gn_eps', 2e-5)self.use_fp16 = Falsewith self.name_scope():self.gamma = self.params.get('gamma',grad_req='write',shape=(1, in_channels, 1, 1),init=gamma_initializer,allow_deferred_init=True,differentiable=True)self.beta = self.params.get('beta',grad_req='write',shape=(1, in_channels, 1, 1),init=beta_initializer,allow_deferred_init=True,differentiable=True)def cast(self, dtype):self.use_fp16 = Falseif np.dtype(dtype).name == 'float16':self.use_fp16 = Truedtype = 'float32'super(GroupNorm, self).cast(dtype)def hybrid_forward(self, F, x, gamma, beta):_kwargs = {}if F is mx.symbol and self.mirroring_level >= 3:_kwargs['force_mirroring'] = 'True'if self.use_fp16:x = F.cast(data=x, dtype='float32')# (N, C, H, W) --> (N, G, C//G, H, Wx = F.reshape(x, shape=(-1, -4, self.groups, -1, -2))# y = (x - mean) / sqrt(var + eps)mean = F.mean(x, axis=(2, 3, 4), keepdims=True, **_kwargs)y = F.broadcast_sub(x, mean, **_kwargs)var = F.mean(y**2, axis=(2, 3, 4), keepdims=True, **_kwargs)y = F.broadcast_div(y, F.sqrt(var + self.eps))# (N, G, C//G, H, W --> (N, C, H, W)y = F.reshape(y, shape=(-1, -3, -2))y = F.broadcast_mul(y, gamma, **_kwargs)y = F.broadcast_add(y, beta, **_kwargs)if self.use_fp16:y = F.cast(data=y, dtype='float16')return y

这篇关于mxnet - reshape操作完全解析(理解0,-1,-2,-3,-4)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/931583

相关文章

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析(结合应用场景)

《nginx-t、nginx-sstop和nginx-sreload命令的详细解析(结合应用场景)》本文解析Nginx的-t、-sstop、-sreload命令,分别用于配置语法检... 以下是关于 nginx -t、nginx -s stop 和 nginx -s reload 命令的详细解析,结合实际应

MyBatis中$与#的区别解析

《MyBatis中$与#的区别解析》文章浏览阅读314次,点赞4次,收藏6次。MyBatis使用#{}作为参数占位符时,会创建预处理语句(PreparedStatement),并将参数值作为预处理语句... 目录一、介绍二、sql注入风险实例一、介绍#(井号):MyBATis使用#{}作为参数占位符时,会

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Java操作Word文档的全面指南

《Java操作Word文档的全面指南》在Java开发中,操作Word文档是常见的业务需求,广泛应用于合同生成、报表输出、通知发布、法律文书生成、病历模板填写等场景,本文将全面介绍Java操作Word文... 目录简介段落页头与页脚页码表格图片批注文本框目录图表简介Word编程最重要的类是org.apach

PostgreSQL的扩展dict_int应用案例解析

《PostgreSQL的扩展dict_int应用案例解析》dict_int扩展为PostgreSQL提供了专业的整数文本处理能力,特别适合需要精确处理数字内容的搜索场景,本文给大家介绍PostgreS... 目录PostgreSQL的扩展dict_int一、扩展概述二、核心功能三、安装与启用四、字典配置方法

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

深度解析Java DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

从原理到实战深入理解Java 断言assert

《从原理到实战深入理解Java断言assert》本文深入解析Java断言机制,涵盖语法、工作原理、启用方式及与异常的区别,推荐用于开发阶段的条件检查与状态验证,并强调生产环境应使用参数验证工具类替代... 目录深入理解 Java 断言(assert):从原理到实战引言:为什么需要断言?一、断言基础1.1 语