All problems
Contrastive Divergence Step
Problem
One Gibbs step of contrastive divergence for a restricted Boltzmann machine. Compute hidden probabilities sigmoid(W v + b_h), sample binary hidden units after random.seed(seed) using one random.random() per unit, then return the visible reconstruction probabilities sigmoid(W^T h + b_v).
The signature is contrastive_divergence_step(visible, weights, bias_v, bias_h, seed). W has shape (num_hidden, num_visible).
Hint
CD approximates the model distribution gradient with one round trip through the hidden layer.
Test cases
case 1
in: [[1,0],[[1,0],[0,1]],[0,0],[0,0],0]
expected: [0.5,0.5]
case 2
in: [[1,1],[[1,1]],[0,0],[0],5]
expected: [0.7310585786,0.7310585786]
case 3
in: [[0,0],[[1,0],[0,1]],[0,0],[0,0],1]
expected: [0.7310585786,0.5]
case 4
in: [[2,-1],[[1,0],[0,1]],[0.1,-0.1],[0.2,0.2],42]
expected: [0.7502601056,0.7109495026]
Starter codePython
import math
import random
def contrastive_divergence_step(visible, weights, bias_v, bias_h, seed):
# Your code here
pass4 test cases · run your code against them in the editor