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

相关文章

Python中配置文件的全面解析与使用

《Python中配置文件的全面解析与使用》在Python开发中,配置文件扮演着举足轻重的角色,它们允许开发者在不修改代码的情况下调整应用程序的行为,下面我们就来看看常见Python配置文件格式的使用吧... 目录一、INI配置文件二、YAML配置文件三、jsON配置文件四、TOML配置文件五、XML配置文件

Spring中@Lazy注解的使用技巧与实例解析

《Spring中@Lazy注解的使用技巧与实例解析》@Lazy注解在Spring框架中用于延迟Bean的初始化,优化应用启动性能,它不仅适用于@Bean和@Component,还可以用于注入点,通过将... 目录一、@Lazy注解的作用(一)延迟Bean的初始化(二)与@Autowired结合使用二、实例解

Python调用Orator ORM进行数据库操作

《Python调用OratorORM进行数据库操作》OratorORM是一个功能丰富且灵活的PythonORM库,旨在简化数据库操作,它支持多种数据库并提供了简洁且直观的API,下面我们就... 目录Orator ORM 主要特点安装使用示例总结Orator ORM 是一个功能丰富且灵活的 python O

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

C语言中自动与强制转换全解析

《C语言中自动与强制转换全解析》在编写C程序时,类型转换是确保数据正确性和一致性的关键环节,无论是隐式转换还是显式转换,都各有特点和应用场景,本文将详细探讨C语言中的类型转换机制,帮助您更好地理解并在... 目录类型转换的重要性自动类型转换(隐式转换)强制类型转换(显式转换)常见错误与注意事项总结与建议类型

C++实现封装的顺序表的操作与实践

《C++实现封装的顺序表的操作与实践》在程序设计中,顺序表是一种常见的线性数据结构,通常用于存储具有固定顺序的元素,与链表不同,顺序表中的元素是连续存储的,因此访问速度较快,但插入和删除操作的效率可能... 目录一、顺序表的基本概念二、顺序表类的设计1. 顺序表类的成员变量2. 构造函数和析构函数三、顺序表

使用C++实现单链表的操作与实践

《使用C++实现单链表的操作与实践》在程序设计中,链表是一种常见的数据结构,特别是在动态数据管理、频繁插入和删除元素的场景中,链表相比于数组,具有更高的灵活性和高效性,尤其是在需要频繁修改数据结构的应... 目录一、单链表的基本概念二、单链表类的设计1. 节点的定义2. 链表的类定义三、单链表的操作实现四、

Python利用自带模块实现屏幕像素高效操作

《Python利用自带模块实现屏幕像素高效操作》这篇文章主要为大家详细介绍了Python如何利用自带模块实现屏幕像素高效操作,文中的示例代码讲解详,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、获取屏幕放缩比例2、获取屏幕指定坐标处像素颜色3、一个简单的使用案例4、总结1、获取屏幕放缩比例from