cossio / RestrictedBoltzmannMachines.jl

Train and sample Restricted Boltzmann machines in Julia
MIT License
14 stars 3 forks source link

Spin sampling #5

Closed cossio closed 2 years ago

cossio commented 2 years ago

Here are two ways of sampling spins.

using LogExpFunctions, Random

function _sample_1(θ)
    u = rand(eltype(θ), size(θ))
    return ifelse.(u .* (1 .+ exp.(-2θ)) .≤ 1, Int8(1), Int8(-1))
end

function _sample_2(θ)
    u = randexp(eltype(θ), size(θ))
    return ifelse.(2θ .≥ u .- log1mexp.(u), Int8(1), Int8(-1))
end

and their benchmarks

image

So implement the first.