Exemple #1
0
    def __init__(self,
                 batch_size,
                 z_size,
                 n_z_particles,
                 ga,
                 n_transformations=3,
                 encoder2='None'):

        self.batch_size = batch_size
        self.z_size = z_size
        self.n_z_particles = n_z_particles
        self.ga = ga
        self.encoder2 = encoder2

        self.rs = 0

        self.n_transitions = n_transformations

        self.variables = []

        for t in range(self.n_transitions):

            net1 = NN([z_size, 100], [tf.tanh], batch_size)
            net2 = NN([100, z_size], [None], batch_size)
            net3 = NN([100, z_size], [tf.nn.sigmoid], batch_size)

            self.variables.append([net1, net2, net3])
Exemple #2
0
    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()
Exemple #3
0
    def __init__(self, n_clusters):

        tf.reset_default_graph()
        self.rs = 0
        self.input_size = 1

        #Model hyperparameters
        self.learning_rate = .001
        self.n_clusters = n_clusters

        #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.tau = tf.placeholder(tf.float32, None)

        #q(z|x)
        f_z_given_x = NN([self.input_size, self.n_clusters], [None])
        self.logits = f_z_given_x.feedforward(self.x)  #[B,K]
        q_z = RelaxedOneHotCategorical(self.tau, logits=self.logits)
        self.z = q_z.sample()  #[B,K]
        self.log_qz = q_z.log_prob(self.z)  #[B]

        #q(T)
        q_tree_logits = tf.Variable(
            tf.random_normal([self.n_clusters, self.n_clusters], stddev=0.35))
        # q_tree = RelaxedOneHotCategorical(self.tau, logits=q_tree_logits)
        # self.tree = q_tree.sample()
        # self.log_qT = tf.reduce_sum(q_tree.log_prob(self.tree))
        self.tree = tf.nn.softmax(q_tree_logits)
        self.log_qT = tf.zeros([1])

        # q(pi)
        q_pi_pre_softmax = tf.Variable(
            tf.random_normal([self.n_clusters], stddev=0.35))  #[K]
        cluster_mixture_weight = tf.nn.softmax(q_pi_pre_softmax)  #[K]
        log_qp = tf.zeros([1])  #[1]
        self.cluster_mixture_weight = tf.reshape(cluster_mixture_weight,
                                                 [1, self.n_clusters])

        # q(means)
        q_mu = tf.Variable(
            tf.random_normal([self.n_clusters, self.input_size],
                             stddev=0.35)) + 25  #[K,X]
        self.cluster_means = q_mu  #[K,X]
        self.log_qm = tf.zeros([self.n_clusters])  #[K]

        # q(variance)
        q_logvar = tf.Variable(
            tf.random_normal([self.n_clusters, self.input_size],
                             stddev=0.35))  #[K,X]
        self.cluster_logvars = q_logvar  #[K,X]
        log_qv = tf.zeros([self.n_clusters])  #[K]

        #Compute prob of joint

        #p(x|z,mu,sigma)  get prob of x under each gaussian, then sum weighted by z
        log_px = log_normal_new(self.x, self.cluster_means,
                                self.cluster_logvars)  #[B,K]
        log_px = tf.log(
            tf.maximum(tf.reduce_sum(tf.exp(log_px) * self.z, axis=1),
                       tf.exp(-30.)))  #[B]

        #log_p(z|pi)
        # self.log_pz = 0.  #[B]
        self.log_pz = tf.log(
            tf.maximum(
                tf.reduce_sum(self.cluster_mixture_weight *
                              tf.stop_gradient(self.z),
                              axis=1), tf.exp(-30.)))  #[B]

        #log p(mu|T)

        self.log_pm = tf.zeros([self.n_clusters])  #[K]

        # log_pm = log_normal(self.cluster_logvars, tf.ones([self.input_size])*20., tf.ones([self.input_size])*10.)

        # c_m = tf.transpose(self.cluster_means) #[X,K]
        # c_m = tf.matmul(c_m, self.tree) #[X,K]
        # c_m = c_m - tf.transpose(self.cluster_means) #[X,K]
        # prob_mean = 1./(1.+tf.exp(-2.*c_m))
        # self.log_pm = tf.reduce_sum(tf.log(tf.maximum(prob_mean, tf.exp(-30.))), axis=0) #[K]

        #log p(sigma)
        log_pv = tf.zeros([self.n_clusters])  #[K]
        # log_pv = log_normal(self.cluster_logvars, tf.ones([self.input_size])*3., tf.ones([self.input_size])*-.5)

        #log p(pi)
        log_pp = tf.zeros([1])  #[1]
        # log_pp =  10* tf.log(1. / tf.reduce_max(cluster_mixture_weight))  #[1]

        #log p(T)
        log_pT = tf.zeros([1])

        # elbo = log_px + log_pz + log_pm + log_pv + log_pp - log_qz - log_qm - log_qv - log_qp

        self.elbo = (
            tf.reduce_mean(log_px + self.log_pz - self.log_qz,
                           axis=0)  #over batch 
            + self.one_over_N *
            (tf.reduce_sum(self.log_pm - self.log_qm + log_pv - log_qv, axis=0)
             + log_pp - log_qp + log_pT - self.log_qT))

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate,
            epsilon=1e-02).minimize(-self.elbo)

        #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())
        self.grads = tf.gradients(self.elbo, self.cluster_means)

        #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_clusters):

        tf.reset_default_graph()
        self.rs = 0
        self.input_size = 1

        #Model hyperparameters
        self.learning_rate = .001
        self.n_clusters = n_clusters
        # prior_mean = 
        # prior_logvar = 
        temperature = .8

        #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.tau = tf.placeholder(tf.float32, None)


        # q(z|x)
        # f_z_given_x = NN([self.input_size, 100, self.n_clusters], [tf.nn.softplus,tf.nn.softplus, None])
        f_z_given_x = NN([self.input_size, self.n_clusters], [None])

        self.logits = f_z_given_x.feedforward(self.x)  #[B,K]
        # self.logits = tf.reshape(self.logits, [-1, 1, self.n_clusters])  #[B,1,K]
        # q_z = RelaxedOneHotCategorical(temperature, logits=self.logits) 

        # q(pi) q(sigma)  q(mu), theyre delta functions for now
        q_pi_pre_softmax = tf.Variable(tf.random_normal([self.n_clusters], stddev=0.35))  #[K]
        q_mu = tf.Variable(tf.random_normal([self.n_clusters, self.input_size], stddev=0.35))+25  #[K,X]
        q_logvar = tf.Variable(tf.random_normal([self.n_clusters, self.input_size], stddev=0.35))  #[K,X]


        #Sample q
        # self.z = q_z.sample()  #[B,K] 
        #vs
        self.z = tf.nn.softmax(self.logits / self.tau)


        # print self.z
        # log_qz = q_z.log_prob(self.z) #[B]
        # print log_qz
        # self.z = q_z.sample()  #[B,1,K] 


        

        # print self.z
        # self.log_qz = q_z.log_prob(self.z) #[B]
        self.log_qz = 0.

        # self.log_qz = tf.log(tf.reduce_sum(tf.square(self.z), axis=1))  #[B]

        # print log_qz

        # self.z = tf.reshape(self.z, [-1, self.n_clusters])
        # self.log_qz = tf.reshape(log_qz, [-1])

        # print self.log_qz

        cluster_mixture_weight = tf.nn.softmax(q_pi_pre_softmax)  #[K]
        log_qp =  tf.zeros([1])  #[1]
        self.cluster_mixture_weight = tf.reshape(cluster_mixture_weight, [1,self.n_clusters])

        self.cluster_means = q_mu   #[K,X]
        log_qm = tf.zeros([self.n_clusters])  #[K]

        self.cluster_logvars = q_logvar   #[K,X]
        log_qv = tf.zeros([self.n_clusters])  #[K]


        #Compute prob of joint

        #p(x|z,mu,sigma)  get prob of x under each gaussian, then sum weighted by z
        #x: [B,X]
        #mu, sigma: [K,X]
        #log_prob, output: [B,K]
        log_px = log_normal_new(self.x, self.cluster_means, self.cluster_logvars) #[B,K]
        #times z: [B,K]  then sum
        log_px = tf.log(tf.maximum(tf.reduce_sum(tf.exp(log_px) * self.z, axis=1), tf.exp(-30.)))  #[B]
        # log_px = tf.log(tf.maximum(tf.reduce_sum(tf.exp(log_px) * self.z * self.cluster_mixture_weight , axis=1), tf.exp(-30.)))  #[B]


        #log_p(z|pi)
        # self.log_pz = 0.  #[B]
        self.log_pz = tf.log(tf.maximum(tf.reduce_sum(self.cluster_mixture_weight * tf.stop_gradient(self.z), axis=1), tf.exp(-30.)))  #[B]


        #log p(mu)
        log_pm = tf.zeros([self.n_clusters])  #[K]
        # log_pm = log_normal(self.cluster_logvars, tf.ones([self.input_size])*20., tf.ones([self.input_size])*10.)


        #log p(sigma)
        log_pv = tf.zeros([self.n_clusters])  #[K]
        # log_pv = log_normal(self.cluster_logvars, tf.ones([self.input_size])*3., tf.ones([self.input_size])*-.5)


        #log p(pi)
        log_pp = tf.zeros([1])  #[1]
        # log_pp =  10* tf.log(1. / tf.reduce_max(cluster_mixture_weight))  #[1]


        # elbo = log_px + log_pz + log_pm + log_pv + log_pp - log_qz - log_qm - log_qv - log_qp

        self.elbo = (tf.reduce_mean(log_px + self.log_pz - self.log_qz, axis=0) #over batch 
                + self.one_over_N*( tf.reduce_sum(log_pm + log_pv - log_qm - log_qv, axis=0) + log_pp - log_qp))

        # Minimize negative ELBO
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate,epsilon=1e-02).minimize(-self.elbo)


        #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())
        self.grads = tf.gradients(self.elbo, self.cluster_means)


        #to make sure im not adding nodes to the graph
        tf.get_default_graph().finalize()

        #Start session
        self.sess = tf.Session()
Exemple #5
0
                    shell=True)

    train_ = 1  # if 0, it loads data

    if train_:

        logits = torch.log(torch.tensor(np.array([.5]))).float()
        logits.requires_grad_(True)

        print()
        print('RELAX')
        print('Value:', val)
        print()

        # net = NN()
        surrogate = NN(input_size=1, output_size=1, n_residual_blocks=2)
        optim = torch.optim.Adam([logits], lr=.004)
        optim_NN = torch.optim.Adam(surrogate.parameters(), lr=.00005)

        steps = []
        losses10 = []
        zs = []
        for step in range(total_steps + 1):

            # batch=[]

            optim_NN.zero_grad()

            losses = 0
            for ii in range(10):
                #Sample p(z)
Exemple #6
0
    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, prior_variance):
        
        tf.reset_default_graph()

        #Model hyperparameters
        # self.act_func = tf.nn.softplus #tf.tanh
        self.learning_rate = .001
        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.tanh,tf.nn.tanh, None])
        # first_half_NN = NN([784, 100, 100, 2], [tf.nn.softplus,tf.nn.softplus, None])
        # second_half_BNN = BNN([self.input_size, 100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None], prior_variance)
        # second_half_BNN = NN([self.input_size, 100, 100, n_classes], [tf.nn.softplus,tf.nn.softplus, None])
        # second_half_BNN = NN([self.input_size, 200, 200, 100, 200, 200, n_classes], [tf.nn.softplus,tf.nn.softplus,None,tf.nn.softplus,tf.nn.softplus, None])
        second_half_BNN = NN([self.input_size, 200, 200, n_classes], [tf.nn.softplus,tf.nn.softplus,None])





        # Ws, log_p_W_sum, log_q_W_sum = second_half_BNN.sample_weights()


        #Feedforward [B,2]
        # self.z = first_half_NN.feedforward(self.x)
        # log_pz = tf.reduce_mean(log_norm(self.z, tf.zeros([2]), tf.log(tf.ones([2]))))



        # self.pred = second_half_BNN.feedforward(self.x, Ws)
        self.pred = second_half_BNN.feedforward(self.x)

        # 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.pred))

        # sigmoid_cross_entropy =  tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=self.pred))

        #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 + log_q_W_sum) #+ .00001*first_half_NN.weight_decay()
        self.cost = softmax_error #+ self.one_over_N*(log_q_W_sum) 
        # self.cost = sigmoid_cross_entropy #+ self.one_over_N*(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.pred) 

        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))

        #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()
Exemple #9
0
    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()
Exemple #10
0
        print 'Current:', saved_parameter_file
        with open(experiment_log, "a") as myfile:
            myfile.write('\nCurrent:' + saved_parameter_file +'\n')




        #Train 
        print 'Training'




        #Initialize model
        if m == 'nn':
            model = NN(net)
        elif m == 'bnn':
            model = BNN(net)
        elif m == 'mnf':
            model = MNF(net)


        start = time.time()
        model.train(train_x=train_x, train_y=train_y, 
                    epochs=epochs, batch_size=n_batch, n_particles=S_training, display_step=[1000,5000],
                    path_to_load_variables='',
                    path_to_save_variables=parameter_path+saved_parameter_file)

        time_to_train = time.time() - start
        print 'Time to train', time_to_train
        with open(experiment_log, "a") as f:
Exemple #11
0
    experiment_log = experiment_log_path + 'experiment_' + date_ + '_' + time_2 + '.txt'
    print 'Saving experiment log to ' + experiment_log

    for m in list_of_models:

        saved_parameter_file = m + '_epochs' + str(epochs) + '.ckpt'
        print 'Current:', saved_parameter_file
        with open(experiment_log, "a") as myfile:
            myfile.write('\nCurrent:' + saved_parameter_file + '\n')

        #Train
        print 'Training'

        #Initialize model
        if m == 'nn':
            model = NN(net)
        elif m == 'bnn':
            model = BNN(net)
        elif m == 'mnf':
            model = MNF(net)

        start = time.time()
        model.train(train_x=train_x,
                    train_y=train_y,
                    epochs=epochs,
                    batch_size=n_batch,
                    n_particles=S_training,
                    display_step=[1000, 5000],
                    path_to_load_variables='',
                    path_to_save_variables=parameter_path +
                    saved_parameter_file)
Exemple #12
0
    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()
Exemple #13
0
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