MXNET源码中TShape值的获取和打印
生活随笔
收集整理的這篇文章主要介紹了
MXNET源码中TShape值的获取和打印
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
承接上一篇(https://blog.csdn.net/zhqh100/article/details/91438657),嘗試打印TShape的數(shù)值,
同樣還是文件incubator-mxnet/src/c_api/c_api.cc中,函數(shù)MXNDArrayReshape64修改如下:
MXNET_DLL int MXNDArrayReshape64(NDArrayHandle handle,int ndim,dim_t *dims,bool reverse,NDArrayHandle *out) {NDArray *ptr = new NDArray();API_BEGIN();NDArray *arr = static_cast<NDArray*>(handle);mxnet::Tuple<dim_t> shape(dims, dims+ndim);CHECK_GT(arr->shape().Size(), 0) << "Source ndarray's shape is undefined. Input shape: "<< arr->shape();mxnet::TShape new_shape = mxnet::op::InferReshapeShape(shape, arr->shape(), reverse);std::cout << "ndim =" << new_shape.ndim() << std::endl;for (int i = 0; i < new_shape.ndim(); i ++){std::cout << "new_shape[" << i << "] = " << new_shape[i] << std::endl;}*ptr = arr->ReshapeWithRecord(new_shape);*out = ptr;API_END_HANDLE_ERROR(delete ptr);
}
Python測試代碼為:
from mxnet import autograd, nd
import mxnet
print(mxnet.__version__)x = nd.arange(42)
x = x.reshape((6, 7))
print(x[2:4].asnumpy())
打印結(jié)果為:
# python3 mxnet_test.py
1.5.0
ndim =2
new_shape[0] = 6
new_shape[1] = 7
slice_begin:2
slice_end:4
p[0] = 14
p[1] = 15
p[2] = 16
p[3] = 17
[[14. 15. 16. 17. 18. 19. 20.][21. 22. 23. 24. 25. 26. 27.]]
現(xiàn)在我們可以從C++中獲取到NDArray的真實(shí)信息了
總結(jié)
以上是生活随笔為你收集整理的MXNET源码中TShape值的获取和打印的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MXNET源码中NDArray数据的获取
- 下一篇: MXNet中x.grad源码追溯