标签:pre rip Edito dfs 读取 pen bin 序列化 [1]
# -*- 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
标签:pre rip Edito dfs 读取 pen bin 序列化 [1]
原文地址:http://blog.51cto.com/13959448/2316998