如何将我的数据划分为随机区分小批量[JULIA]

时间:2016-06-23 10:34:30

标签: julia

我有一个100000个例子的数据向量。值为-1和1。 我想从这个数据 16个不同的迷你批次中随机获取,每个都是6250.

这是我的代码,用于生成存储在文件中的100000个示例的向量。

Dan回答了如何将我的数据划分到不同部分的问题。

现在,我想在p文件中存储[部件中的p [p] [p]。我的意思是:如果我有3个部分,我想创建和存储p的值。我该怎么做?

workspace()
using JLD, HDF5
#import HTreeRBM

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)

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("/home/anelmad/Desktop/stage-inria/code/HTreeRBM.jl/artificial_data/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 个答案:

答案 0 :(得分:2)

通过运行:

来定义kfoldperm
function kfoldperm(N,k)
    n,r = divrem(N,k)
    b = collect(1:n:N+1)
    for i in 1:length(b)
        b[i] += i > r ? r : i-1  
    end
    p = randperm(N)
    return [p[r] for r in [b[i]:b[i+1]-1 for i=1:k]]
end

现在,

v = rand(10)
parts = kfoldperm(10,3)
[v[p] for p in parts]

将为您提供v到3个部分的分区。