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

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

关于WebSocket协议状态码解析

《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3

CSS Padding 和 Margin 区别全解析

《CSSPadding和Margin区别全解析》CSS中的padding和margin是两个非常基础且重要的属性,它们用于控制元素周围的空白区域,本文将详细介绍padding和... 目录css Padding 和 Margin 全解析1. Padding: 内边距2. Margin: 外边距3. Padd

Oracle数据库常见字段类型大全以及超详细解析

《Oracle数据库常见字段类型大全以及超详细解析》在Oracle数据库中查询特定表的字段个数通常需要使用SQL语句来完成,:本文主要介绍Oracle数据库常见字段类型大全以及超详细解析,文中通过... 目录前言一、字符类型(Character)1、CHAR:定长字符数据类型2、VARCHAR2:变长字符数

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

SpringCloud动态配置注解@RefreshScope与@Component的深度解析

《SpringCloud动态配置注解@RefreshScope与@Component的深度解析》在现代微服务架构中,动态配置管理是一个关键需求,本文将为大家介绍SpringCloud中相关的注解@Re... 目录引言1. @RefreshScope 的作用与原理1.1 什么是 @RefreshScope1.

Java并发编程必备之Synchronized关键字深入解析

《Java并发编程必备之Synchronized关键字深入解析》本文我们深入探索了Java中的Synchronized关键字,包括其互斥性和可重入性的特性,文章详细介绍了Synchronized的三种... 目录一、前言二、Synchronized关键字2.1 Synchronized的特性1. 互斥2.

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

C# WinForms存储过程操作数据库的实例讲解

《C#WinForms存储过程操作数据库的实例讲解》:本文主要介绍C#WinForms存储过程操作数据库的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、存储过程基础二、C# 调用流程1. 数据库连接配置2. 执行存储过程(增删改)3. 查询数据三、事务处