Skip to content

Instantly share code, notes, and snippets.

@itsdfish
Last active May 24, 2023 19:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itsdfish/6ad91dbb3008056bf2656f22d16e30ed to your computer and use it in GitHub Desktop.
Save itsdfish/6ad91dbb3008056bf2656f22d16e30ed to your computer and use it in GitHub Desktop.
Discrete normalizing flow attemp
using Distributions
function sample_prior()
return rand(Beta(1, 1))
end
function generate_data()
θ = sample_prior()
y = rand(Binomial(10, θ))
return [θ,y]
end
# train using samples from joint distribution x,y ~ p(x,y) where x=[μ, σ] -> y = N(μ, σ)
# rows: μ, σ, y
num_params = 1; num_obs = 1; n_train = 10000
training_data = mapreduce(x -> generate_data(), hcat, 1:n_train);
############ train some data
X_train = reshape(training_data[1:num_params,:], (1,1,num_params,:));
Y_train = reshape(training_data[(num_params+1):end,:], (1,1,num_obs,:));
n_epochs = 2
batch_size = 200
n_batches = div(n_train, batch_size)
# make conditional normalizing flow
using InvertibleNetworks, LinearAlgebra, Flux
L = 3 # RealNVP multiscale layers
K = 4 # Coupling layers per scale
n_hidden = 32 # Hidden channels in coupling layers' neural network
G = NetworkConditionalGlow(num_params, num_obs, n_hidden, L, K;);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment