mxnet进阶 - mxnet.io.DataDesc 源码分析

本文详细介绍了MXNet中DataDesc类的功能与用法。DataDesc是用于存储数据名称、形状、类型和布局信息的数据描述子,它帮助描述数据集的结构特征,如批处理轴、数据类型等,对于数据并行处理至关重要。

全称应该叫Data Descriptor 数据描述子

一个namedtuple的子类

本质也是一个tuple,只不过对不同位置的元素命名了而已,可以通过命名访问到元素,和ordinaried字典类似

mxnet.DataDesc 使用了两个信息描述数据

  • name 是名字 字符串 str
  • shape 是数据形状 元组 里面存int类型的数 
class DataDesc(namedtuple('DataDesc', ['name', 'shape'])):
    """DataDesc is used to store name, shape, type and layout
    information of the data or the label.

    The `layout` describes how the axes in `shape` should be interpreted,
    for example for image data setting `layout=NCHW` indicates
    that the first axis is number of examples in the batch(N),
    C is number of channels, H is the height and W is the width of the image.

    For sequential data, by default `layout` is set to ``NTC``, where
    N is number of examples in the batch, T the temporal axis representing time
    and C is the number of channels.

    Parameters
    ----------
    cls : DataDesc
         The class.
    name : str
         Data name.
    shape : tuple of int
         Data shape.
    dtype : np.dtype, optional
         Data type.
    layout : str, optional
         Data layout.
    """
    def __new__(cls, name, shape, dtype=mx_real_t, layout='NCHW'): # pylint: disable=super-on-old-class
        ret = super(cls, DataDesc).__new__(cls, name, shape)
        ret.dtype = dtype
        ret.layout = layout
        return ret

    def __repr__(self):
        return "DataDesc[%s,%s,%s,%s]" % (self.name, self.shape, self.dtype,
                                          self.layout)

    @staticmethod
    def get_batch_axis(layout):
        """Get the dimension that corresponds to the batch size.

        When data parallelism is used, the data will be automatically split and
        concatenated along the batch-size dimension. Axis can be -1, which means
        the whole array will be copied for each data-parallelism device.

        Parameters
        ----------
        layout : str
            layout string. For example, "NCHW".

        Returns
        -------
        int
            An axis indicating the batch_size dimension.
        """
        if layout is None:
            return 0
        return layout.find('N')

    @staticmethod
    def get_list(shapes, types):
        """Get DataDesc list from attribute lists.

        Parameters
        ----------
        shapes : a tuple of (name, shape)
        types : a tuple of  (name, type)
        """
        if types is not None:
            type_dict = dict(types)
            return [DataDesc(x[0], x[1], type_dict[x[0]]) for x in shapes]
        else:
            return [DataDesc(x[0], x[1]) for x in shapes]

 

(d2l) C:\Windows\System32>pip install mxnet-cu101==1.7.0 -f https://dist.mxnet.io/python Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Looking in links: https://dist.mxnet.io/python DEPRECATION: Wheel filename 'mxnet-pi-1.5.0-py2.py3-none-any.whl' is not correctly normalised. Future versions of pip will raise the following error: Invalid wheel filename (invalid version): 'mxnet-pi-1.5.0-py2.py3-none-any' pip 25.3 will enforce this behaviour change. A possible replacement is to rename the wheel to use a correctly normalised name (this may require updating the version in the project metadata). Discussion can be found at https://github.com/pypa/pip/issues/12938 DEPRECATION: Wheel filename 'mxnet-raspi-1.5.0-py2.py3-none-any.whl' is not correctly normalised. Future versions of pip will raise the following error: Invalid wheel filename (invalid version): 'mxnet-raspi-1.5.0-py2.py3-none-any' pip 25.3 will enforce this behaviour change. A possible replacement is to rename the wheel to use a correctly normalised name (this may require updating the version in the project metadata). Discussion can be found at https://github.com/pypa/pip/issues/12938 Collecting mxnet-cu101==1.7.0 Using cached https://repo.mxnet.io/dist/python/cu101/mxnet_cu101-1.7.0-py2.py3-none-win_amd64.whl (718.3 MB) Requirement already satisfied: numpy<1.17.0,>=1.8.2 in d:\anaconda\envs\d2l\lib\site-packages (from mxnet-cu101==1.7.0) (1.16.6) Requirement already satisfied: requests<2.19.0,>=2.18.4 in d:\anaconda\envs\d2l\lib\site-packages (from mxnet-cu101==1.7.0) (2.18.4) INFO: pip is looking at multiple versions of mxnet-cu101 to determine which version is compatible with other requirements. This could take a while. ERROR: Could not find a version that satisfies the requirement graphviz<0.9.0,>=0.8.1 (from mxnet-cu101) (from versions: none) ERROR: No matching distribution found for graphviz<0.9.0,>=0.8.1
最新发布
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值