julia - 如何将我的数据随机分成不同的小批量 [JULIA]

标签 julia

我有一个包含 100000 个示例的数据向量。值为 -1 和 1。 我想从这些数据中随机获取 16 个不同的小批量,每个批量为 6250 个。

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

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

现在,我想将 [X[p] for p in parts] 存储在 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

最佳答案

通过运行定义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 个部分。

关于julia - 如何将我的数据随机分成不同的小批量 [JULIA],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37989159/

相关文章:

r - 如何在 R 中运行 jl 文件 (julia)

julia - 更好的方法来计算唯一项目的出现次数?

python-3.x - Julia 是否需要矢量化来加速计算?

dataframe - 通过csv读取文件和julia中的管道读取有什么区别?

julia - 自动创建多个方法

module - Julia:如何让多个工作人员访问模块中 'include(...)' 的函数?

function - Julia 中没有自然默认值的命名参数

performance - Julia 的特征分解比 Mathematica 慢 5 倍?

dataframe - 在同一列中分配过滤值的结果不正确

io - 在 Julia 中设置多个 I/O 缓冲区的最佳方法是什么?