a_func_a_day_in_python_numpy.ndarray与mxnet.ndarray.NDArray的转换

a_func_a_day_in_python_numpy.ndarray与mxnet.ndarray.NDArray的转换

标签: a_func_a_day_in_python


0. 简介

numpy.ndarray是python numpy工具包中的N维数组对象,NDArray是mxnet中存储的N维数据格式,类似于caffe中的blob结构,可以用于存储各类数据,包含feature map、weights、bias、图像内容等;

在实际应用中,我们会碰到numpy.ndarray与mxnet.ndarray.NDArray需要做类型转换的问题,下面简单说明。

1. numpy.ndarray -> mxnet.ndarray.NDArray

具体可以参照mxnet官网NDArray API的使用,我简单写了个测试小模块,可以一窥从ndarray到NDArray的转换过程:

import mxnet as mx
import numpy as np

# in python, Arrays should be constructed using array, zeros or empty,
# np.array is just a convenience function to create an ndarray, it is not a class itself.
# You can also create an array using np.ndarray, but it is not the recommended way.

# list -> mxnet.ndarray.NDArray
list_x = [[1, 2, 3], [4, 5, 6]]   # list
print "type(list_x): ", type(list_x)

MXNDArray_from_list = mx.nd.array(list_x)
print "type(MXNDArray_from_list): ", type(MXNDArray_from_list)

# np.ndarray(numpy.ndarray) -> mxnet.ndarray.NDArray 
ndarray_x = np.array([[1, 2, 3], [4, 5, 6]])  # np.ndarray
print "type(ndarray_x): ", type(ndarray_x)

MXNDArray_from_nparray = mx.nd.array(ndarray_x)
print "type(MXNDArray_from_nparray): ", type(MXNDArray_from_nparray)

# mxnet.ndarray.NDArray -> numpy.ndarray
MXNDArray_x = mx.nd.array([[1, 2, 3], [4, 5, 6]])
print "type(MXNDArray_x): ", type(MXNDArray_x)
print "MXNDArray_x.shape: ", MXNDArray_x.shape

MXNDArray_y = MXNDArray_x + mx.nd.ones(MXNDArray_x.shape) * 3
nparray_z = MXNDArray_y.asnumpy() 
print "type(MXNDArray_y): ", type(MXNDArray_y)
print "type(nparray_z): ", type(nparray_z)

输出结果:

type(list_x):  <type 'list'>
type(MXNDArray_from_list):  <class 'mxnet.ndarray.ndarray.NDArray'>
type(array_x):  <type 'numpy.ndarray'>
type(MXNDArray_from_nparray):  <class 'mxnet.ndarray.ndarray.NDArray'>
type(MXNDArray_x):  <class 'mxnet.ndarray.ndarray.NDArray'>
MXNDArray_x.shape:  (2L, 3L)
type(MXNDArray_y):  <class 'mxnet.ndarray.ndarray.NDArray'>
type(nparray_z):  <type 'numpy.ndarray'>

简单点说,就是直接调用mx.nd.array(ndarray_in_numpy)函数,就可以将numpy.ndarray转换为mxnet.ndarray.NDArray。

2. mxnet.ndarray.NDArray -> numpy.ndarray

同样可以参照mxnet官网NDArray API的使用,同样写了个简单的测试函数:

import mxnet as mx
import numpy as np

# other test
MXNDArray_A = mx.nd.ones((2, 3))
nparray_B = MXNDArray_A.asnumpy()
print "type(MXNDArray_A): ", type(MXNDArray_A)
print "type(nparray_B): ", type(nparray_B)

MXNDArray_C = mx.nd.ones((2, 3), dtype='int32')
MXNDArray_C.asnumpy()
print "type(NDArray_C): ", type(MXNDArray_C)

输出结果:

type(MXNDArray_A):  <class 'mxnet.ndarray.ndarray.NDArray'>
type(nparray_B):  <type 'numpy.ndarray'>
type(NDArray_C):  <class 'mxnet.ndarray.ndarray.NDArray'>

简单点说,就是对mxnet.ndarray.NDArray对象,直接调用函数asnumpy(),就可以将mxnet.ndarray.NDArray转换为numpy.ndarray。通过NDArray_z的调用可以发现,直接调用asnumpy()函数并不会对数据本身的格式进行转换

以上源码个人git链接:
https://github.com/humengdoudou/a_func_a_day_in_python/blob/master/test_ndarrayNDArray_20180330.py

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值