【问题标题】:Julia : How to store tuples in different files?Julia:如何将元组存储在不同的文件中?
【发布时间】:2016-07-05 19:27:59
【问题描述】:

我想将每个元组 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,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_batches 文件的工作,但未能一一存储元组。我该如何解决?

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

【问题讨论】:

  • write(file, "X", X[p]) 不会成功吗? (即,可能的问题是理解)。
  • 不,它不起作用,因为它会读取第一个元组的第一个值。例如,如果 p = 4 。 X[1] 将是第一个元组的第一个值,X[2] 将是第一个元组的第二个值,X[3] 第一个元组的第三个值,X[4] 第一个元组的第四个值元组。但我正在寻找这个结构 X[1] 整个第一个元组,X[2] 整个第二个元组等等
  • y=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.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 => 元组 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] 等等。希望它可以帮助您更好地捕捉问题!

标签: file julia


【解决方案1】:

将所有数据设为 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

【讨论】:

  • 这里不一样我使用元组第一个参数表示元组内的元组数为4然后我们有第四个元组(4,[[2,3,5] ,[8,9,15],[17, 6, 45, 7],[78, 1 ,45, 2])
  • 那就用write(file, "X", X[2][i])
  • 或者如果你需要第一个元素,write(file, "X", (1, X[2][i]))
猜你喜欢
  • 2019-10-16
  • 2012-11-07
  • 2016-09-02
  • 2019-07-14
  • 1970-01-01
  • 2022-07-26
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多