File 朱莉娅:如何在不同的文件中存储元组?
我想把每个元组X[p]存储在不同的文件中 mini_batch1.jld中的X[1]mini_batch2中的X[2]。。。。。 但是我下面的代码在创建的文件中存储(复制)了X[p]的所有元组。让我们看一个例子:File 朱莉娅:如何在不同的文件中存储元组?,file,julia,File,Julia,我想把每个元组X[p]存储在不同的文件中 mini_batch1.jld中的X[1]mini_batch2中的X[2]。。。。。 但是我下面的代码在创建的文件中存储(复制)了X[p]的所有元组。让我们看一个例子: m= 100 k= 3 # number of tuples or partition y=rand_gen(m,k) (3,[[-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,
m= 100
k= 3 # number of tuples or partition
y=rand_gen(m,k)
(3,[[-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0],[1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0],[1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0]])
我想加入:
mini_batch1第一个元组
[-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0]
mini_batch2第二个元组
[1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0]
等等。但是,我的代码执行创建mini_批处理文件的任务,但无法逐个存储元组。我怎样才能解决这个问题
workspace()
using JLD, HDF5
function gen_random(m,k)
# m the length of the vector , for instance m=100000 and k
# the number of partitions let's set k=16
s = rand(m)
# Pkg.add("JLD"), Pkg.add("HDF5") these two packages are needed
# in order to store our vectors in files under the extension jld
# allow to convert each random number to -1 or 1
X=float_to_binary(s)
parts= kfoldperm(length(X),k)
# l want to store each tuple X[p] in a different file
# X[1] in mini_batch1.jld X[2] in mini_batch2.....
# but my code below store all the tuple X[p] in the files created.
for p in 1:length(parts)
file =jldopen(@sprintf("my path to file/mini_batch%d.jld", p),"w")
write(file, "X", [X[p] for p in parts])
close(file)
end
return [X[p] for p in parts]
function float_to_binary(s,level=0.4)
for i=1:length(s)
s[i] = s[i] > level ? 1.0 : -1.0
end
file = jldopen("/my path/mydata.jld", "w")
write(file, "s", s) # alternatively, say "@write file A"
close(file)
return s
end
function kfoldperm(l,k)
n,r = divrem(l,k)
b = collect(1:n:l+1)
for i in 1:length(b)
b[i] += i > r ? r : i-1
end
p = randperm(l)
return [p[r] for r in [b[i]:b[i+1]-1 for i=1:k]]
end
将所有数据设为1和-1会使示例更难阅读,因此下面是一个具有更多可分辨数字的示例:
julia> using JLD
julia> X = Vector{Int}[[1,2], [3,4]]
2-element Array{Array{Int64,1},1}:
[1,2]
[3,4]
julia> for i = 1:2
jldopen("file$i.jld", "w") do file
write(file, "X", X[i])
end
end
julia> X1 = load("file1.jld", "X")
2-element Array{Int64,1}:
1
2
julia> X2 = load("file2.jld", "X")
2-element Array{Int64,1}:
3
4
write(文件,“X”,X[p])
会不会起作用?(即,可能的问题是理解)。不,它不起作用,因为它将读取第一个元组的第一个值。例如,如果p=4。X[1]将是第一个元组的第一个值,X[2]将是第一个元组的第二个值,X[3]将是第一个元组的第三个值,X[4]将是第一个元组的第四个值。但是我在寻找这个结构,例如,整个第一个元组,整个第二个元组,所以ony=gen_random(m,k)#m=100,k=4(4,[[1.0,1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,1,1],[1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,1.0,1.0,1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,1.0,1.0],[1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0],[1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0,-1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0]])这是我想在每个文件中获取的元组结构:文件1=>tuple 1[1.0,1.0,1.0,1.0,1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0]等等。希望它能帮助你更好地解决这个问题!!这里不一样,我用的是元组,第一个参数表示元组中的元组数,是4,然后我们有第四个元组(4,[[2,3,5],[8,9,15],[17,6,45,7],[78,1,45,2]),然后使用write(文件,“X”,X[2][i])
或者,如果您需要第一个元素,编写(文件,“X”,“1,X[2][i])
。