def recurrence(k, u_tm1, x_t): bv_t = visible_bias_recurrence(u_tm1) bh_t = hidden_bias_recurrence(u_tm1) x_out = CRBM.gibbs_sample(x_t, W, bv_t, bh_t, k=5) u_t = rnn_recurrence(u_tm1, x_out) tf.assign(utm1, u_t) cost = CRBM.free_energy_cost(x_t, x_out, W, bv_t, bh_t) return u_t, x_out, cost
def recurrence(k, u_tm1, x_t, W, bh, bv, Wuh, Wuv, Wvu, Wuu, bu): bv_t = visible_bias_recurrence(u_tm1, Wuv, bv) bh_t = hidden_bias_recurrence(u_tm1, Wuh, bh) x_out = CRBM.gibbs_sample(x_t, W, bv_t, bh_t, k=1) u_t = rnn_recurrence(u_tm1, x_out, Wvu, Wuu, bu) #cost = tf.losses.mean_squared_error(x_t,x_out) cost = CRBM.free_energy_cost(x_t, x_out, W, bv_t, bh_t) return u_t, x_out, cost
def get_CRBM(how_long=4000): print "Creating data...." mean1 = np.array([-0.2, 0.3]) mean2 = np.array([0.5, -0.5]) cov1 = np.array([[0.02, 0.005], [0.001, 0.01]]) cov2 = np.array([[0.02, 0.0], [0.0, 0.02]]) dataset = np.random.multivariate_normal(mean1, cov1, 200).tolist() dataset += np.random.multivariate_normal(mean2, cov2, 200).tolist() r = CRBM(2, 4) # Train at the following learning rates: print "Training..." for i in range(how_long): print " " + str(i) + "...", r.train_epoch(dataset, (0.9, 0.9), 20) E = np.sum([r.energy(data) for data in dataset]) print "Energy = " + str(E) + "...", print "Done" return r, dataset
def get_CRBM(how_long=4000): print "Creating data...." mean1 = np.array([-0.2, 0.3]) mean2 = np.array([0.5, -0.5]) cov1 = np.array([[0.02, 0.005], [0.001, 0.01]]) cov2 = np.array([[0.02, 0.0], [0.0, 0.02]]) dataset = np.random.multivariate_normal(mean1, cov1, 200).tolist() dataset += np.random.multivariate_normal(mean2, cov2, 200).tolist() r = CRBM(2, 4) # Train at the following learning rates: print "Training..." for i in range(how_long): print ' ' + str(i) + '...', r.train_epoch(dataset, (0.9, 0.9), 20) E = np.sum([r.energy(data) for data in dataset]) print 'Energy = ' + str(E) + '...', print 'Done' return r, dataset
return self.pi, self.A, self.O, self.A_ijk, self.O_jl, self.O_jf if __name__ == "__main__": num_str = '25' itr = 5 feedback_tperiod = 5 session_len = 250 it_str = str(itr) f_str = str(feedback_tperiod) # import inital transition and emissions probability matrices prob_transition = CRBM.CRBM('transition') prob_emission = CRBM.CRBM('emission') prob_emission_f = CRBM.CRBM('emission_f') a_matrix = prob_transition.total_transition_probs() o_matrix = prob_emission._o_jk() o_f_matrix = prob_emission_f._o_jf() # Training Data Import TrainingD = trainingData.TrainingData() [training_input_seq, training_output_seq, feedback_seq_all] = TrainingD.io_sequence_generator() training_total_len = len(training_input_seq) training_output_f_seq = np.zeros((int(len(training_input_seq)//feedback_tperiod)+1,)) output_f_time_stamp = np.zeros_like(training_output_f_seq) # feedback time stamps
from scipy import * import pickle sys.path.insert(0, '../Categorical_Boltzmann_Machines') np.set_printoptions(linewidth=700) np.set_printoptions(precision=3, edgeitems=25) # prob_transition = CRBM.CRBM('transition') # A_average = prob_transition.total_transition_probs() # for i in arange(0, 1000, 5): # barchart(A_average[i]) # view(azimuth=180, elevation=180) # show() # prob_emission = CRBM.CRBM('emission') # O_average = prob_emission._o_jk() # # prob_emission_f = CRBM.CRBM('emission_f') # O_f_average = prob_emission_f._o_jf() # TestD = testdata.TrainingData() # [test_input_seq, test_output_seq, test_output_f_seq] = TestD.io_sequence_generator() TrainingD = trainingData.TrainingData() [test_input_seq, test_output_seq, test_output_f_seq] = TrainingD.io_sequence_generator() with open('data_info.pickle', 'rb') as d_i: data_info = pickle.load(d_i)
def main(): #Load the Songs tf.reset_default_graph() songs = input_manipulation.get_songs('Game_Music_Midi') X = tf.placeholder(tf.float32, [None, RcrbmHyp.num_timesteps, RcrbmHyp.span], name="X") x = tf.placeholder(tf.float32, [RcrbmHyp.num_timesteps, RcrbmHyp.span], name="x") batch_size = tf.placeholder(tf.int64, [1], name="batch_size") #parameters of CRBM W = tf.Variable(tf.truncated_normal([ RcrbmHyp.size_conv_filters, RcrbmHyp.span, 1, RcrbmHyp.num_conv_filters ], 0.001), name="W") #The weight matrix of the RBM bh = tf.Variable(tf.zeros( [RcrbmHyp.hidden_width, RcrbmHyp.num_conv_filters], tf.float32), name="bh") #The RNN -> RBM hidden bias vector bv = tf.Variable(tf.zeros([RcrbmHyp.num_timesteps, RcrbmHyp.span], tf.float32), name="bv") #The RNN -> RBM visible bias vector #parameters related to RNN Wuh = tf.Variable(tf.random_normal([ RcrbmHyp.n_hidden_recurrent, int(RcrbmHyp.hidden_width * RcrbmHyp.num_conv_filters) ], 0.0001), name="Wuh") #The RNN -> RBM hidden weight matrix Wuv = tf.Variable(tf.random_normal([ RcrbmHyp.n_hidden_recurrent, int(RcrbmHyp.num_timesteps * RcrbmHyp.span) ], 0.0001), name="Wuv") #The RNN -> RBM visible weight matrix Wvu = tf.Variable(tf.random_normal([ int(RcrbmHyp.num_timesteps * RcrbmHyp.span), RcrbmHyp.n_hidden_recurrent ], 0.0001), name="Wvu") #The data -> RNN weight matrix Wuu = tf.Variable(tf.random_normal( [RcrbmHyp.n_hidden_recurrent, RcrbmHyp.n_hidden_recurrent], 0.0001), name="Wuu") #The RNN hidden unit weight matrix bu = tf.Variable(tf.zeros([1, RcrbmHyp.n_hidden_recurrent], tf.float32), name="bu") #The RNN hidden unit bias vector u0 = tf.Variable(tf.zeros([1, RcrbmHyp.n_hidden_recurrent], tf.float32), name="u0") #The initial state of the RNN #The RBM bias vectors. These matrices will get populated during rnn-rbm training and generation BH_t = tf.Variable(tf.ones( [RcrbmHyp.hidden_width, RcrbmHyp.num_conv_filters], tf.float32), name="BH_t") BV_t = tf.Variable(tf.ones([RcrbmHyp.num_timesteps, RcrbmHyp.span], tf.float32), name="BV_t") #Build the RBM optimization saver = tf.train.Saver() #Note that we initialize the RNN->RBM bias vectors with the bias vectors of the trained RBM. These vectors will form the templates for the bv_t and #bh_t of each RBM that we create when we run the RNN-RBM updt_cnn, cnnloss = CRBM.cnn_update(x, W, bv, bh, 10.) updt = CRBM.get_cd_update_batch(X, W, bv, bh, 1, RcrbmHyp.crbm_lr) #Run the session with tf.Session() as sess: #Initialize the variables of the model init = tf.global_variables_initializer() sess.run(init) for epoch in tqdm(range(RcrbmHyp.crbm_num_epochs)): cost = [] for song in songs: for i in range(song.shape[0]): _, c = sess.run([updt_cnn, cnnloss], feed_dict={x: song[i, :, :]}) cost.append(c) print(np.mean(cost)) #Run over each song num_epoch times for epoch in tqdm(range(RcrbmHyp.crbm_num_epochs)): for song in songs: sess.run(updt, feed_dict={X: song}) #Save the initialized model here save_path = saver.save(sess, "parameter_checkpoints/initialized_crbm.ckpt")
#dataset = [[-1.0 + np.random.normal(0,0.15) + 0.02*n, 0.5 + np.random.normal(0,0.2)] for n in range(100)] XY = np.array(dataset) # And show it f = plt.figure(1) plt.scatter(XY[:,0], XY[:,1], 20, 'b', 'o') plt.title('Distribution') plt.draw() f.show() # Make a CRBM print "Creating RBM" rbm = CRBM(2,20) rbm.lo = -2 rbm.hi = 2 # Train it k=20 for i in range(10000): #for i in range(0): err = rbm.train_epoch(dataset, 0.1, k) if i%10 == 0: print "Epoch", i, ": Error =", err # Now reconstruct some stuff XY_rec = np.random.uniform(-2,2,(100,2))