def __init__(self, n_classes): tf.reset_default_graph() #Model hyperparameters # self.act_func = tf.nn.softplus #tf.tanh self.learning_rate = .0001 self.rs = 0 self.input_size = 784 self.output_size = n_classes # self.net = network_architecture #Placeholders - Inputs/Targets [B,X] # self.batch_size = tf.placeholder(tf.int32, None) # self.n_particles = tf.placeholder(tf.int32, None) self.one_over_N = tf.placeholder(tf.float32, None) self.x = tf.placeholder(tf.float32, [None, self.input_size]) self.y = tf.placeholder(tf.float32, [None, self.output_size]) # first_half_NN = NN([784, 100, 100, 2], [tf.nn.softplus,tf.nn.softplus, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [1,1, None]) # first_half_NN = NN([784, 100, 100, 2], [tf.tanh,tf.tanh, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [tf.tanh,tf.tanh, None]) first_half_NN = NN([784, 100, 100, 2], [tf.nn.softplus, tf.nn.softplus, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None]) second_half_BNN = NN([2, 100, 100, n_classes], [tf.nn.softplus, tf.nn.softplus, None]) # first_half_NN = NN([784, 100, 100, 2], [tf.nn.relu,tf.nn.relu, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [tf.nn.relu,tf.nn.relu, None]) # Ws, log_p_W_sum, log_q_W_sum = second_half_BNN.sample_weights() #Feedforward: [B,P,Y], [P], [P] self.y1 = first_half_NN.feedforward(self.x) # self.y2 = second_half_BNN.feedforward(self.y1, Ws) self.y2 = second_half_BNN.feedforward(self.y1) # y_hat, log_p_W, log_q_W = self.model(self.x) #Likelihood [B,P] # log_p_y_hat = self.log_likelihood(self.y, y_hat) softmax_error = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=self.y2)) #Objective # self.elbo = self.objective(log_p_y_hat, log_p_W, log_q_W, self.batch_fraction_of_dataset) # self.cost = softmax_error + self.one_over_N*(-log_p_W_sum*.00000001 + log_q_W_sum*.00000001 ) + .00001*first_half_NN.weight_decay() self.cost = softmax_error + .00001 * first_half_NN.weight_decay( ) + self.one_over_N * (.5 * second_half_BNN.weight_decay()) # Minimize negative ELBO self.optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate, epsilon=1e-02).minimize(self.cost) #For evaluation self.prediction = tf.nn.softmax(self.y2) correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.prediction, 1)) self.acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # W_means = second_half_BNN.sample_weight_means() # y2_mean = second_half_BNN.feedforward(self.y1, W_means) # self.prediction_using_mean = tf.nn.softmax(y2_mean) #To init variables self.init_vars = tf.global_variables_initializer() #For loadind/saving variables self.saver = tf.train.Saver() #For debugging # self.vars = tf.trainable_variables() # self.grads = tf.gradients(self.elbo, tf.trainable_variables()) #to make sure im not adding nodes to the graph tf.get_default_graph().finalize() #Start session self.sess = tf.Session()
def __init__(self, n_classes): tf.reset_default_graph() #Model hyperparameters # self.act_func = tf.nn.softplus #tf.tanh self.learning_rate = .0001 self.rs = 0 self.input_size = 784 self.output_size = n_classes # self.net = network_architecture #Placeholders - Inputs/Targets [B,X] # self.batch_size = tf.placeholder(tf.int32, None) # self.n_particles = tf.placeholder(tf.int32, None) self.one_over_N = tf.placeholder(tf.float32, None) self.x = tf.placeholder(tf.float32, [None, self.input_size]) self.y = tf.placeholder(tf.float32, [None, self.output_size]) # first_half_NN = NN([784, 100, 100, 2], [tf.nn.softplus,tf.nn.softplus, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [1,1, None]) # first_half_NN = NN([784, 100, 100, 2], [tf.tanh,tf.tanh, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [tf.tanh,tf.tanh, None]) first_half_NN = NN([784, 100, 100, 2], [tf.nn.softplus,tf.nn.softplus, None]) self.second_half_BNN = BNN([2,10, 10, n_classes], [tf.nn.softplus,tf.nn.softplus, None]) # first_half_NN = NN([784, 100, 100, 2], [tf.nn.relu,tf.nn.relu, None]) # second_half_BNN = BNN([2,100, 100, n_classes], [tf.nn.relu,tf.nn.relu, None]) Ws, self.log_p_W_sum, self.log_q_W_sum = self.second_half_BNN.sample_weights() #Feedforward: [B,P,Y], [P], [P] self.y1 = first_half_NN.feedforward(self.x) self.y2 = self.second_half_BNN.feedforward(self.y1, Ws) # y_hat, log_p_W, log_q_W = self.model(self.x) #Likelihood [B,P] # log_p_y_hat = self.log_likelihood(self.y, y_hat) self.softmax_error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=self.y2)) #Objective # self.elbo = self.objective(log_p_y_hat, log_p_W, log_q_W, self.batch_fraction_of_dataset) # self.cost = softmax_error + self.one_over_N*(-log_p_W_sum*.00000001 + log_q_W_sum*.00000001 ) + .00001*first_half_NN.weight_decay() self.cost = self.softmax_error - self.log_p_W_sum + self.log_q_W_sum + .00001*first_half_NN.weight_decay() # self.cost2 = self.softmax_error - self.log_p_W_sum self.cost2 = self.softmax_error + self.log_q_W_sum # Minimize negative ELBO self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=1e-02).minimize(self.cost) #For evaluation self.prediction = tf.nn.softmax(self.y2) # W_means = second_half_BNN.sample_weight_means() # y2_mean = second_half_BNN.feedforward(self.y1, W_means) # self.prediction_using_mean = tf.nn.softmax(y2_mean) #To init variables self.init_vars = tf.global_variables_initializer() #For loadind/saving variables self.saver = tf.train.Saver() #For debugging # self.vars = tf.trainable_variables() # self.grads = tf.gradients(self.elbo, tf.trainable_variables()) #to make sure im not adding nodes to the graph tf.get_default_graph().finalize() #Start session self.sess = tf.Session()
def __init__(self, n_classes): tf.reset_default_graph() #Model hyperparameters # self.act_func = tf.nn.softplus #tf.tanh self.learning_rate = .0001 self.rs = 0 self.input_size = 784 self.output_size = n_classes #Placeholders - Inputs/Targets [B,X] self.one_over_N = tf.placeholder(tf.float32, None) self.x = tf.placeholder(tf.float32, [None, self.input_size]) self.y = tf.placeholder(tf.float32, [None, self.output_size]) # B = tf.shape(self.x)[0] # out = tf.reshape(self.x, [B, 32,32,3]) # conv1_weights = tf.Variable(tf.truncated_normal([5, 5, 3, 20], stddev=0.1)) # conv1_biases = tf.Variable(tf.truncated_normal([20], stddev=0.1)) # out = tf.nn.conv2d(out,conv1_weights,strides=[1, 2, 2, 1],padding='VALID') # out = tf.nn.relu(tf.nn.bias_add(out, conv1_biases)) # out = tf.nn.max_pool(out,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID') # out = tf.nn.lrn(out) # out = tf.reshape(out, [B, -1]) # BNN_model = BNN([self.input_size, 100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None]) # BNN_model = BNN([self.input_size, 100,2, 100, n_classes], [tf.nn.softplus,tf.nn.softplus,tf.nn.softplus, None]) BNN_model = NN([self.input_size, 100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None]) # Ws, log_p_W_sum, log_q_W_sum = BNN_model.sample_weights() # #Feedforward: [B,P,Y], [P], [P] # # self.y2 = BNN_model.feedforward(self.x, Ws) # self.y2 = BNN_model.feedforward(out, Ws) # Ws, log_p_W_sum = BNN_model.sample_weight_means() # self.y2 = BNN_model.feedforward(self.x, Ws) self.y2 = BNN_model.feedforward(self.x) #Likelihood [B,P] self.softmax_error = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=self.y2)) self.reg = .00000001*BNN_model.weight_decay() # self.one_over_N*(-log_p_W_sum*.0000001 ) #+ log_q_W_sum) #Objective self.cost = self.softmax_error + self.reg # Minimize negative ELBO self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=1e-02).minimize(self.cost) #For evaluation self.prediction = tf.nn.softmax(self.y2) correct_prediction = tf.equal(tf.argmax(self.y,1), tf.argmax(self.prediction,1)) self.acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # W_means, log_p_W_sum = BNN_model.sample_weight_means() # # y2_mean = BNN_model.feedforward(self.x, W_means) # y2_mean = BNN_model.feedforward(self.x, W_means) # self.prediction_using_mean = tf.nn.softmax(y2_mean) # correct_prediction = tf.equal(tf.argmax(self.y,1), tf.argmax(self.prediction_using_mean,1)) # self.acc2 = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # self.softmax_error2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=y2_mean)) #To init variables self.init_vars = tf.global_variables_initializer() #For loadind/saving variables self.saver = tf.train.Saver() #For debugging # self.vars = tf.trainable_variables() # self.grads = tf.gradients(self.elbo, tf.trainable_variables()) #to make sure im not adding nodes to the graph tf.get_default_graph().finalize() #Start session self.sess = tf.Session()
def __init__(self, n_classes): tf.reset_default_graph() #Model hyperparameters # self.act_func = tf.nn.softplus #tf.tanh self.learning_rate = .0001 self.rs = 0 self.input_size = 784 self.output_size = n_classes #Placeholders - Inputs/Targets [B,X] self.one_over_N = tf.placeholder(tf.float32, None) self.x = tf.placeholder(tf.float32, [None, self.input_size]) self.y = tf.placeholder(tf.float32, [None, self.output_size]) # B = tf.shape(self.x)[0] # out = tf.reshape(self.x, [B, 32,32,3]) # conv1_weights = tf.Variable(tf.truncated_normal([5, 5, 3, 20], stddev=0.1)) # conv1_biases = tf.Variable(tf.truncated_normal([20], stddev=0.1)) # out = tf.nn.conv2d(out,conv1_weights,strides=[1, 2, 2, 1],padding='VALID') # out = tf.nn.relu(tf.nn.bias_add(out, conv1_biases)) # out = tf.nn.max_pool(out,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID') # out = tf.nn.lrn(out) # out = tf.reshape(out, [B, -1]) # BNN_model = BNN([self.input_size, 100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None]) # BNN_model = BNN([self.input_size, 100,2, 100, n_classes], [tf.nn.softplus,tf.nn.softplus,tf.nn.softplus, None]) BNN_model = NN([self.input_size, 100, 100, n_classes], [tf.nn.softplus, tf.nn.softplus, None]) # Ws, log_p_W_sum, log_q_W_sum = BNN_model.sample_weights() # #Feedforward: [B,P,Y], [P], [P] # # self.y2 = BNN_model.feedforward(self.x, Ws) # self.y2 = BNN_model.feedforward(out, Ws) # Ws, log_p_W_sum = BNN_model.sample_weight_means() # self.y2 = BNN_model.feedforward(self.x, Ws) self.y2 = BNN_model.feedforward(self.x) #Likelihood [B,P] self.softmax_error = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=self.y2)) self.reg = .00000001 * BNN_model.weight_decay( ) # self.one_over_N*(-log_p_W_sum*.0000001 ) #+ log_q_W_sum) #Objective self.cost = self.softmax_error + self.reg # Minimize negative ELBO self.optimizer = tf.train.AdamOptimizer( learning_rate=self.learning_rate, epsilon=1e-02).minimize(self.cost) #For evaluation self.prediction = tf.nn.softmax(self.y2) correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.prediction, 1)) self.acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # W_means, log_p_W_sum = BNN_model.sample_weight_means() # # y2_mean = BNN_model.feedforward(self.x, W_means) # y2_mean = BNN_model.feedforward(self.x, W_means) # self.prediction_using_mean = tf.nn.softmax(y2_mean) # correct_prediction = tf.equal(tf.argmax(self.y,1), tf.argmax(self.prediction_using_mean,1)) # self.acc2 = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # self.softmax_error2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=y2_mean)) #To init variables self.init_vars = tf.global_variables_initializer() #For loadind/saving variables self.saver = tf.train.Saver() #For debugging # self.vars = tf.trainable_variables() # self.grads = tf.gradients(self.elbo, tf.trainable_variables()) #to make sure im not adding nodes to the graph tf.get_default_graph().finalize() #Start session self.sess = tf.Session()
class DKF_new(object): def __init__(self, network_architecture): tf.reset_default_graph() self.rs = 0 self.act_fct=tf.nn.softplus #tf.tanh self.learning_rate = 0.001 self.reg_param = .00001 self.z_size = network_architecture["n_z"] self.input_size = network_architecture["n_input"] self.action_size = network_architecture["n_actions"] self.n_particles = network_architecture["n_particles"] self.n_timesteps = network_architecture["n_timesteps"] # Graph Input: [B,T,X], [B,T,A] self.x = tf.placeholder(tf.float32, [None, None, self.input_size]) self.actions = tf.placeholder(tf.float32, [None, None, self.action_size]) #Recognition/Inference net q(z|z-1,u,x) self.rec_net = NN([self.input_size+self.z_size+self.action_size, 100, 100, self.z_size*2], [tf.nn.softplus,tf.nn.softplus, None]) #Emission net p(x|z) self.emiss_net = NN([self.z_size, 100, 100, self.input_size], [tf.nn.softplus,tf.nn.softplus, None]) #Transition net p(z|z-1,u) self.trans_net = NN([self.z_size+self.action_size, 100, 100, self.z_size*2], [tf.nn.softplus,tf.nn.softplus, None]) weight_decay = self.rec_net.weight_decay() + self.emiss_net.weight_decay() + self.trans_net.weight_decay() #Objective self.elbo = self.Model(self.x, self.actions) self.cost = -self.elbo + (self.reg_param * self.l2_regularization()) #Optimize self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate, epsilon=1e-02).minimize(self.cost) #Evaluation self.prev_z_and_current_a_ = tf.placeholder(tf.float32, [self.batch_size, self.z_size+self.action_size]) self.next_state = self.transition_net(self.prev_z_and_current_a_) self.current_z_ = tf.placeholder(tf.float32, [self.batch_size, self.z_size]) self.current_emission = tf.sigmoid(self.observation_net(self.current_z_)) self.prior_mean_ = tf.placeholder(tf.float32, [self.batch_size, self.z_size]) self.prior_logvar_ = tf.placeholder(tf.float32, [self.batch_size, self.z_size]) eps = tf.random_normal((self.batch_size, self.z_size), 0, 1, dtype=tf.float32) self.sample = tf.add(self.prior_mean_, tf.multiply(tf.sqrt(tf.exp(self.prior_logvar_)), eps)) #to make sure im not adding nodes to the graph tf.get_default_graph().finalize() #Start session self.sess = tf.Session() def Model(self, x, actions): ''' x: [B,T,X] actions: [B,T,A] output: elbo scalar ''' #problem with loop is creates many insteaces of everythin in it.. for t in range(self.n_timesteps): def fn_over_timesteps(particle_and_logprobs, xa_t): ''' particle_and_logprobs: [B,PZ+3] xa_t: [B,X+A] Steps: encode, sample, decode, calc logprobs return [B,Z+3] ''' #unpack previous particle_and_logprobs, prev_z:[B,PZ], ignore prev logprobs prev_particles = tf.slice(particle_and_logprobs, [0,0], [self.batch_size, self.n_particles*self.z_size]) #unpack xa_t current_x = tf.slice(xa_t, [0,0], [self.batch_size, self.input_size]) #[B,X] current_a = tf.slice(xa_t, [0,self.input_size], [self.batch_size, self.action_size]) #[B,A] log_pzs = [] log_qzs = [] log_pxs = [] new_particles = [] for i in range(self.n_particles): #select particle prev_z = tf.slice(prev_particles, [0,i*self.z_size], [self.batch_size, self.z_size]) #combine prez and current a (used for prior) prev_z_and_current_a = tf.concat([prev_z, current_a], axis=1) #[B,ZA] #ENCODE #Concatenate current x, current action, prev_z: [B,XA+Z] concatenate_all = tf.concat([xa_t, prev_z], axis=1) #Predict q(z|z-1,u,x): [B,Z] [B,Z] z_mean, z_log_var = self.recognition_net(concatenate_all) #SAMPLE from q(z|z-1,u,x) [B,Z] eps = tf.random_normal((self.batch_size, self.z_size), 0, 1, dtype=tf.float32) this_particle = tf.add(z_mean, tf.multiply(tf.sqrt(tf.exp(z_log_var)), eps)) #DECODE p(x|z): [B,X] x_mean = self.observation_net(this_particle) #CALC LOGPROBS #Prior p(z|z-1,u) [B,Z] prior_mean, prior_log_var = self.transition_net(prev_z_and_current_a) #[B,Z] log_p_z = self.log_normal(this_particle, prior_mean, prior_log_var) #[B] #Recognition q(z|z-1,x,u) log_q_z = self.log_normal(this_particle, z_mean, z_log_var) #Likelihood p(x|z) Bernoulli reconstr_loss = \ tf.reduce_sum(tf.maximum(x_mean, 0) - x_mean * current_x + tf.log(1 + tf.exp(-tf.abs(x_mean))), 1) #sum over dimensions log_p_x = -reconstr_loss log_pzs.append(log_p_z) log_qzs.append(log_q_z) log_pxs.append(log_p_x) new_particles.append(this_particle) #Average the log probs log_p_z = tf.reduce_mean(tf.stack(log_pzs), axis=0) #over particles so its [B] log_q_z = tf.reduce_mean(tf.stack(log_qzs), axis=0) log_p_x = tf.reduce_mean(tf.stack(log_pxs), axis=0) #Organize output logprobs = tf.stack([log_p_x, log_p_z, log_q_z], axis=1) # [B,3] #Reshape particles new_particles = tf.stack(new_particles, axis=1) #[B,P,Z] new_particles = tf.reshape(new_particles, [self.batch_size, self.n_particles*self.z_size]) # output = tf.concat(1, [new_particles, logprobs])# [B,Z+3] output = tf.concat([new_particles, logprobs], axis=1)# [B,Z+3] return output # Put obs and actions to together [B,T,X+A] x_and_a = tf.concat([x, actions], axis=2) # Transpose so that timesteps is first [T,B,X+A] x_and_a = tf.transpose(x_and_a, [1,0,2]) #Make initializations for scan z_init = tf.zeros([self.batch_size, self.n_particles*self.z_size]) logprobs_init = tf.zeros([self.batch_size, 3]) # Put z and logprobs together [B,PZ+3] # initializer = tf.concat(1, [z_init, logprobs_init]) initializer = tf.concat([z_init, logprobs_init], axis=1) # Scan over timesteps, returning particles and logprobs [T,B,Z+3] self.particles_and_logprobs = tf.scan(fn_over_timesteps, x_and_a, initializer=initializer) #unpack the logprobs list([pz+3,T,B]) particles_and_logprobs_unstacked = tf.unstack(self.particles_and_logprobs, axis=2) #[T,B] log_p_x_over_time = particles_and_logprobs_unstacked[self.z_size*self.n_particles] log_p_z_over_time = particles_and_logprobs_unstacked[self.z_size*self.n_particles+1] log_q_z_over_time = particles_and_logprobs_unstacked[self.z_size*self.n_particles+2] # sum over timesteps [B] log_q_z_batch = tf.reduce_sum(log_q_z_over_time, axis=0) log_p_z_batch = tf.reduce_sum(log_p_z_over_time, axis=0) log_p_x_batch = tf.reduce_sum(log_p_x_over_time, axis=0) # average over batch [1] self.log_q_z_final = tf.reduce_mean(log_q_z_batch) self.log_p_z_final = tf.reduce_mean(log_p_z_batch) self.log_p_x_final = tf.reduce_mean(log_p_x_batch) elbo = self.log_p_x_final + self.log_p_z_final - self.log_q_z_final return elbo