zoukankan      html  css  js  c++  java
  • Gluon学习03-基础数据类型Ndarray

    Gluon学习03-基础数据类型Ndarray

    目录,方便快速定位:


    本机环境介绍:

    系统:Linuxmint
    Python版本:Python3


    1.API介绍

    MxNet版本:1.2.0
    API地址:https://mxnet.incubator.apache.org/api/python/ndarray/ndarray.html

    Ndarray在CPU/GPU上提供必要的张量操作,是一个多维的,固定大小的,同类型的矩阵.mxnet.ndarray与numpy.ndarray非常相似.

    The NDArray class:
    1.属性
    shape/size/ndim/context/dtype

    1. import mxnet as mx 
    2. from mxnet import nd 
    3. import numpy as np 
    4. #数据的形状 
    5. >>> x = mx.nd.array([1, 2, 3, 4]) 
    6. >>> x.shape 
    7. (4L,) 
    8. >>> y = mx.nd.zeros((2, 3, 4)) 
    9. >>> y.shape 
    10. (2L, 3L, 4L
    11.  
    12. #数据的多少 
    13. >>> import numpy as np 
    14. >>> x = mx.nd.zeros((3, 5, 2)) 
    15. >>> x.size 
    16. 30 
    17. >>> np.prod(x.shape) 
    18. 30 
    19.  
    20. #数据的阶/秩 
    21. >>> x = mx.nd.array([1, 2, 3, 4]) 
    22. >>> x.ndim 
    23. 1 
    24. >>> x = mx.nd.array([[1, 2], [3, 4]]) 
    25. >>> x.ndim 
    26. 2 
    27.  
    28. #数据所在的设备 
    29. >>> x = mx.nd.array([1, 2, 3, 4]) 
    30. >>> x.context 
    31. cpu(0
    32. >>> type(x.context) 
    33.  
    34. >>> y = mx.nd.zeros((2,3), mx.gpu(0)) 
    35. >>> y.context 
    36. gpu(0
    37.  
    38. #数据的类型 
    39. >>> x = mx.nd.zeros((2,3)) 
    40. >>> x.dtype 
    41.  
    42. >>> y = mx.nd.zeros((2,3), dtype='int32'
    43. >>> y.dtype 

    2.转换

    1. #转为标量,形状必须是(1,) 
    2. >>> x = mx.nd.ones((1,), dtype='int32'
    3. >>> x.asscalar() 
    4. 1 
    5. >>> type(x.asscalar()) 
    6.  
    7. #复制 
    8. >>> x = mx.nd.ones((2,3)) 
    9. >>> y = x.copy() 
    10. >>> y.asnumpy() 
    11. array([[ 1., 1., 1.], 
    12. [ 1., 1., 1.]], dtype=float32) 

    3.创建

    1. #通过自身API创建 
    2. >>> a=nd.arange((10)) 
    3. >>>
    4. [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.
    5. <NDArray 10 @cpu(0)> 
    6.  
    7. >>> b=nd.zeros((2,3)) 
    8. >>>
    9. [[0. 0. 0.
    10. [0. 0. 0.]] 
    11. <NDArray 2x3 @cpu(0)> 
    12.  
    13. >>> c=nd.ones((2,3,1)) 
    14. >>>
    15. [[[1.
    16. [1.
    17. [1.]] 
    18. [[1.
    19. [1.
    20. [1.]]] 
    21. <NDArray 2x3x1 @cpu(0)> 
    22.  
    23. #通过list创建 
    24. >>> d=[6,5,4,3,2,1
    25. >>> e=nd.array(d) 
    26. >>>
    27. [6. 5. 4. 3. 2. 1.
    28. <NDArray 6 @cpu(0)> 

    4.形状

    1. #转置 
    2. >>> x = mx.nd.arange(0,6).reshape((2,3)) 
    3. >>> x.asnumpy() 
    4. array([[ 0., 1., 2.], 
    5. [ 3., 4., 5.]], dtype=float32) 
    6. >>> x.T.asnumpy() 
    7. array([[ 0., 3.], 
    8. [ 1., 4.], 
    9. [ 2., 5.]], dtype=float32) 
    10.  
    11. #改变形状 
    12. >>> x = mx.nd.arange(0,6).reshape(2,3
    13. >>>
    14. [[0. 1. 2.
    15. [3. 4. 5.]] 
    16. <NDArray 2x3 @cpu(0)> 
    17.  
    18. >>> y = x.reshape(3,2
    19. >>>
    20. [[0. 1.
    21. [2. 3.
    22. [4. 5.]] 
    23. <NDArray 3x2 @cpu(0)> 
    24.  
    25. #列多少不管,就明确是n行,列= (x.size/n)上整 
    26. >>> y = x.reshape(2,-1
    27. >>>
    28. [[0. 1. 2.
    29. [3. 4. 5.]] 
    30. <NDArray 2x3 @cpu(0)> 
    31.  
    32. #只要一行 
    33. >>> y = x.reshape(-3
    34. >>>
    35. [0. 1. 2. 3. 4. 5.
    36. <NDArray 6 @cpu(0)> 
    37. >>>  
    38.  

    5.元素扩展

    1. #拼接,输入数组的唯独应该相同 
    2. x = [[1,1],[2,2]] 
    3. y = [[3,3],[4,4],[5,5]] 
    4. z = [[6,6], [7,7],[8,8]] 
    5. concat(x,y,z,dim=0) = [[ 1., 1.], 
    6. [ 2., 2.], 
    7. [ 3., 3.], 
    8. [ 4., 4.], 
    9. [ 5., 5.], 
    10. [ 6., 6.], 
    11. [ 7., 7.], 
    12. [ 8., 8.]] 

    参考:
    NDArray API

    每日一学,争取进步03

  • 相关阅读:
    Java实现 LeetCode 242 有效的字母异位词
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 212 单词搜索 II
    Java实现 LeetCode 344 反转字符串
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
    Java实现 洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
  • 原文地址:https://www.cnblogs.com/wushaogui/p/9134135.html
Copyright © 2011-2022 走看看