def main(): print("reading data...") data = ngsim_manipulation.Data() x = tf.placeholder(tf.float32, [None, rnn_rbm.n_visible], name="x") #The placeholder variable that holds our data W = tf.Variable(tf.random_normal([rnn_rbm.n_visible, rnn_rbm.n_hidden], 0.01), name="W") #The weight matrix of the RBM Wuh = tf.Variable(tf.random_normal( [rnn_rbm.n_hidden_recurrent, rnn_rbm.n_hidden], 0.0001), name="Wuh") #The RNN -> RBM hidden weight matrix bh = tf.Variable(tf.zeros([1, rnn_rbm.n_hidden], tf.float32), name="bh") #The RNN -> RBM hidden bias vector Wuv = tf.Variable(tf.random_normal( [rnn_rbm.n_hidden_recurrent, rnn_rbm.n_visible], 0.0001), name="Wuv") #The RNN -> RBM visible weight matrix bv = tf.Variable(tf.zeros([1, rnn_rbm.n_visible], tf.float32), name="bv") #The RNN -> RBM visible bias vector Wvu = tf.Variable(tf.random_normal( [rnn_rbm.n_visible, rnn_rbm.n_hidden_recurrent], 0.0001), name="Wvu") #The data -> RNN weight matrix Wuu = tf.Variable(tf.random_normal( [rnn_rbm.n_hidden_recurrent, rnn_rbm.n_hidden_recurrent], 0.0001), name="Wuu") #The RNN hidden unit weight matrix bu = tf.Variable(tf.zeros([1, rnn_rbm.n_hidden_recurrent], tf.float32), name="bu") #The RNN hidden unit bias vector u0 = tf.Variable(tf.zeros([1, rnn_rbm.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([1, rnn_rbm.n_hidden], tf.float32), name="BH_t") BV_t = tf.Variable(tf.ones([1, rnn_rbm.n_visible], 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 = RBM.get_cd_update(x, W, bv, bh, k=1, lr=lr) # CD-k print("training RBM for weight initialization...") #Run the session with tf.Session() as sess: #Initialize the variables of the model init = tf.global_variables_initializer() sess.run(init) #Run over each trajectory num_epoch times for epoch in tqdm(range(num_epochs)): #for idx in range(100): for idx in range(data.num_trajectories): sess.run( updt, feed_dict={x: data.next_traj()} ) # even if traj is comprised of data in different time, we treat it like a batch of data in the same time, because we can only initialize one RBM. #Save the initialized model here save_path = saver.save(sess, "parameter_checkpoints/initialized.ckpt")
def main(num_epochs): print("reading data...") data = ngsim_manipulation.Data() #First, we build the model and get pointers to the model parameters x, cost, reconstruction, W, Wuh, Wuv, Wvu, Wuu, bh, bv, bu, lr, u0 = rnn_rbm.rnnrbm( ) #The trainable parameters, as well as the initial state of the RNN params = [W, Wuh, Wuv, Wvu, Wuu, bh, bv, bu, u0] opt_func = tf.train.AdamOptimizer(learning_rate=lr) grad_and_params = opt_func.compute_gradients(cost, params) grad_and_params = [(tf.clip_by_value(grad, -10., 10.), var) for grad, var in grad_and_params] updt = opt_func.apply_gradients(grad_and_params) #The learning rate of the optimizer is a parameter that we set on a schedule during training #opt_func = tf.train.GradientDescentOptimizer(learning_rate=lr) #grad_and_params = opt_func.compute_gradients(cost, params) #grad_and_params = [(tf.clip_by_value(grad, -10., 10.), var) for grad, var in grad_and_params] #We use gradient clipping to prevent gradients from blowing up during training #updt = opt_func.apply_gradients(grad_and_params) saver = tf.train.Saver( params, max_to_keep=1 ) #We use this saver object to restore the weights of the model and save the weights every few epochs with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore( sess, saved_initial_weights_path ) #Here we load the initial weights of the model that we created with weight_initializations.py ##这边,可以考虑batch,也可以一次就只处理一个traj 随机梯度下降 print("training in progress...") for epoch in range(num_epochs): costs = [] start = time.time() #for j in tqdm(range(100)): for j in tqdm(range(data.num_trajectories)): _, C = sess.run([updt, cost], feed_dict={ x: data.next_traj(), lr: learningRate }) costs.append(C) #Print the progress at epoch print("epoch: {} cost: {} time: {}".format(epoch, np.mean(costs), time.time() - start)) print("\n") #Here we save the weights of the model every few epochs saver.save(sess, "parameter_checkpoints/epoch_{}.ckpt".format(epoch))
def draw_all_trajectories(): if not os.path.isdir("./picture_folder"): os.makedirs("./picture_folder") data = ngsim_manipulation.Data() trajectories = data.dataset plt.figure(figsize=(30, 15)) for traj in trajectories: traj = data.decontraction(traj) plt.plot(traj[:, 0], traj[:, 1]) plt.xlabel("x") plt.ylabel("y") plt.title("All trajectories") plt.legend() plt.savefig("./picture_folder/All_trajectories") mng = plt.get_current_fig_manager() mng.window.showMaximized() plt.show()
def main(saved_weights_path): print("reading data...") data = ngsim_manipulation.Data() x, cost, reconstruction, W, Wuh, Wuv, Wvu, Wuu, bh, bv, bu, lr, u0 = rnn_rbm.rnnrbm( ) # First we build and get the parameters of the network params = [W, Wuh, Wuv, Wvu, Wuu, bh, bv, bu, u0] saver = tf.train.Saver( params) #We use this saver object to restore the weights of the model #get many trajectories #trajectories_primer = data.get_trajectories(start=0, end=5) idx = [ 0, 3, 4 ] # three representative traj: get_traj(0) get_traj(3) get_traj(4) trajectories_primer = data.get_trajs(idx) trajectories_primer = data.add_noise_gaussian(trajectories_primer) #trajectories_primer = data.add_noise_zero(idx) #get one traj #idx=0 #trajectories_primer = [data.get_traj(idx)] #trajectories_primer = data.add_noise_gaussian(trajectories_primer) #trajectories_primer = data.add_noise_zero([idx]) #xytraj=data.xyset[1] #xytraj = xytraj*[data.max[0],1] #x_sample=RBM.gibbs_sample(x, W, bv, bh, 1) reconstructed_trajectories = [] decontr_trajectories_primer = [] print("reconstruction...") with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) saver.restore( sess, saved_weights_path) #load the saved weights of the network ''' # without time dependence, reconstruction based on a single RBM(only one RBM for all time steps) # actually, this performs not too badly, even just one RBM for different time(because we choose rbm_steps=100m it include time dependence already) for j in tqdm( range(len(trajectories_primer)) ): decontract_traj = data.decontraction( sess.run(x_sample, feed_dict={x: trajectories_primer[j]}) ) reconstructed_trajectories.append( decontract_traj ) decontr_trajectories_primer.append( data.decontraction(trajectories_primer[j]) ) ''' # reconstruction based on RNN-RBM, time dependance. Expectation RBM for j in tqdm(range(len(trajectories_primer))): for i in range(num_sample): decontract_traj = data.decontraction( sess.run(reconstruction(), feed_dict={x: trajectories_primer[j]})) reconstructed_trajectories.append( decontract_traj ) #Prime the network with primer and reconstruct this trajectory decontr_trajectories_primer.append( data.decontraction(trajectories_primer[j])) trajectories = decontr_trajectories_primer + reconstructed_trajectories #+ [xytraj] draw.draw_trajectories(trajectories, num_trajs=len(trajectories_primer)) '''