<s> from __future__ import print_function <EOL> import numpy as np <EOL> class RBM : <EOL> def __init__ ( self , num_visible , num_hidden , learning_rate = <NUM_LIT:0.1> ) : <EOL> self . num_hidden = num_hidden <EOL> self . num_visible = num_visible <EOL> self . learning_rate = learning_rate <EOL> self . weights = <NUM_LIT:0.1> * np . random . randn ( self . num_visible , self . num_hidden ) <EOL> self . weights = np . insert ( self . weights , <NUM_LIT:0> , <NUM_LIT:0> , axis = <NUM_LIT:0> ) <EOL> self . weights = np . insert ( self . weights , <NUM_LIT:0> , <NUM_LIT:0> , axis = <NUM_LIT:1> ) <EOL> def train ( self , data , max_epochs = <NUM_LIT:1000> ) : <EOL> """<STR_LIT>""" <EOL> num_examples = data . shape [ <NUM_LIT:0> ] <EOL> data = np . insert ( data , <NUM_LIT:0> , <NUM_LIT:1> , axis = <NUM_LIT:1> ) <EOL> for epoch in range ( max_epochs ) : <EOL> pos_hidden_activations = np . dot ( data , self . weights ) <EOL> pos_hidden_probs = self . _logistic ( pos_hidden_activations ) <EOL> pos_hidden_states = pos_hidden_probs > np . random . rand ( num_examples , self . num_hidden + <NUM_LIT:1> ) <EOL> pos_associations = np . dot ( data . T , pos_hidden_probs ) <EOL> neg_visible_activations = np . dot ( pos_hidden_states , self . weights . T ) <EOL> neg_visible_probs = self . _logistic ( neg_visible_activations ) <EOL> neg_visible_probs [ : , <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> neg_hidden_activations = np . dot ( neg_visible_probs , self . weights ) <EOL> neg_hidden_probs = self . _logistic ( neg_hidden_activations ) <EOL> neg_associations = np . dot ( neg_visible_probs . T , neg_hidden_probs ) <EOL> self . weights += self . learning_rate * ( ( pos_associations - neg_associations ) / num_examples ) <EOL> error = np . sum ( ( data - neg_visible_probs ) ** <NUM_LIT:2> ) <EOL> print ( "<STR_LIT>" % ( epoch , error ) ) <EOL> def run_visible ( self , data ) : <EOL> """<STR_LIT>""" <EOL> num_examples = data . shape [ <NUM_LIT:0> ] <EOL> hidden_states = np . ones ( ( num_examples , self . num_hidden + <NUM_LIT:1> ) ) <EOL> data = np . insert ( data , <NUM_LIT:0> , <NUM_LIT:1> , axis = <NUM_LIT:1> ) <EOL> hidden_activations = np . dot ( data , self . weights ) <EOL> hidden_probs = self . _logistic ( hidden_activations ) <EOL> hidden_states [ : , : ] = hidden_probs > np . random . rand ( num_examples , self . num_hidden + <NUM_LIT:1> ) <EOL> hidden_states = hidden_states [ : , <NUM_LIT:1> : ] <EOL> return hidden_states <EOL> def run_hidden ( self , data ) : <EOL> """<STR_LIT>""" <EOL> num_examples = data . shape [ <NUM_LIT:0> ] <EOL> visible_states = np . ones ( ( num_examples , self . num_visible + <NUM_LIT:1> ) ) <EOL> data = np . insert ( data , <NUM_LIT:0> , <NUM_LIT:1> , axis = <NUM_LIT:1> ) <EOL> visible_activations = np . dot ( data , self . weights . T ) <EOL> visible_probs = self . _logistic ( visible_activations ) <EOL> visible_states [ : , : ] = visible_probs > np . random . rand ( num_examples , self . num_visible + <NUM_LIT:1> ) <EOL> visible_states = visible_states [ : , <NUM_LIT:1> : ] <EOL> return visible_states <EOL> def daydream ( self , num_samples ) : <EOL> """<STR_LIT>""" <EOL> samples = np . ones ( ( num_samples , self . num_visible + <NUM_LIT:1> ) ) <EOL> samples [ <NUM_LIT:0> , <NUM_LIT:1> : ] = np . random . rand ( self . num_visible ) <EOL> for i in range ( <NUM_LIT:1> , num_samples ) : <EOL> visible = samples [ i - <NUM_LIT:1> , : ] <EOL> hidden_activations = np . dot ( visible , self . weights ) <EOL> hidden_probs = self . _logistic ( hidden_activations ) <EOL> hidden_states = hidden_probs > np . random . rand ( self . num_hidden + <NUM_LIT:1> ) <EOL> hidden_states [ <NUM_LIT:0> ] = <NUM_LIT:1> <EOL> visible_activations = np . dot ( hidden_states , self . weights . T ) <EOL> visible_probs = self . _logistic ( visible_activations ) <EOL> visible_states = visible_probs > np . random . rand ( self . num_visible + <NUM_LIT:1> ) <EOL> samples [ i , : ] = visible_states <EOL> return samples [ : , <NUM_LIT:1> : ] <EOL> def _logistic ( self , x ) : <EOL> return <NUM_LIT:1.0> / ( <NUM_LIT:1> + np . exp ( - x ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> r = RBM ( num_visible = <NUM_LIT:6> , num_hidden = <NUM_LIT:2> ) <EOL> training_data = np . array ( [ [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ] , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> r . train ( training_data , max_epochs = <NUM_LIT> ) <EOL> print ( r . weights ) <EOL> user = np . array ( [ [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> ] ] ) <EOL> print ( r . run_visible ( user ) ) </s>
