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将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

Golang HashMap实现原理解析

《GolangHashMap实现原理解析》HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持高效的插入、查找和删除操作,:本文主要介绍GolangH... 目录HashMap是一种基于哈希表实现的键值对存储结构,它通过哈希函数将键映射到数组的索引位置,支持

Python ZIP文件操作技巧详解

《PythonZIP文件操作技巧详解》在数据处理和系统开发中,ZIP文件操作是开发者必须掌握的核心技能,Python标准库提供的zipfile模块以简洁的API和跨平台特性,成为处理ZIP文件的首选... 目录一、ZIP文件操作基础三板斧1.1 创建压缩包1.2 解压操作1.3 文件遍历与信息获取二、进阶技

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组