Skip to content

Instantly share code, notes, and snippets.

@Slater-Victoroff
Last active January 4, 2016 00:39
Show Gist options
  • Save Slater-Victoroff/8543588 to your computer and use it in GitHub Desktop.
Save Slater-Victoroff/8543588 to your computer and use it in GitHub Desktop.
Simplest Possible Geometric Restricted Boltzmann Machine. Doesn't include training, just random generation and prediction for now.
import numpy as np
def transfer_function(x, y):
return np.power(np.prod(x, axis=1)[:, None] * np.prod(y, axis=0), 1./x.shape[1])
def gnn(c):
return normalize([np.random.random(c[i] * c[i + 1]).reshape((c[i], c[i + 1])) for i in range(len(c) - 1)])
def predict(weights, input_vector, reverse=False):
current_net = [input_vector] + weights
if reverse: current_net = [input_vector] + [layer.T for layer in weights[::-1]]
return reduce(transfer_function, current_net)
def normalize(weights):
constants = [np.power(np.prod(w, axis=1), -1./w.shape[1]).reshape(w.shape[0],1) for w in weights]
return [np.multiply(w, constant) for w, constant in zip(weights, constants)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment