1. 程式人生 > >mxnet-讀寫檔案

mxnet-讀寫檔案

 # -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import mxnet as mx
import numpy as np

import pickle as pkl

#1.pickle
a = mx.nd.ones((2, 3))
# 序列化儲存
data = pkl.dumps(a)
pkl.dump(data, open('tmp.pickle', 'wb'))
# 序列化讀取 
data = pkl.load(open('tmp.pickle', 'rb'))
b = pkl.loads(data)
print b.asnumpy()
#2.直接讀寫
a = mx.nd.ones((2,3))
b = mx.nd.ones((5,6))
mx.nd.save("temp.ndarray", [a,b])
c = mx.nd.load("temp.ndarray")
print c

d = {'a':a, 'b':b}
mx.nd.save("temp.ndarray", d)
c = mx.nd.load("temp.ndarray")
print c

[[1. 1. 1.]
 [1. 1. 1.]]
[
[[1. 1. 1.]
 [1. 1. 1.]]
<NDArray 2x3 @cpu(0)>, 
[[1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]]
<NDArray 5x6 @cpu(0)>]
{'a': 
[[1. 1. 1.]
 [1. 1. 1.]]
<NDArray 2x3 @cpu(0)>, 'b': 
[[1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1.]]
<NDArray 5x6 @cpu(0)>}

a = mx.nd.ones((2, 3))
mx.nd.save("temp.ndarray", [a,])
a <- mx.nd.load("temp.ndarray")
as.array(a[[1]])
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
mx.nd.save('s3://mybucket/mydata.ndarray', [a,])  # if compiled with USE_S3=1
mx.nd.save('hdfs///users/myname/mydata.bin', [a,])  # if compiled with USE_HDFS=1