Example #1
0
  def _compute_log_moment(self, sigma, q, moment_order):
    """Compute high moment of privacy loss.

    Args:
      sigma: the noise sigma, in the multiples of the sensitivity.
      q: the sampling ratio.
      moment_order: the order of moment.
    Returns:
      log E[exp(moment_order * X)]
    """
    assert moment_order <= self._max_moment_order, ("The order of %d is out "
                                                    "of the upper bound %d."
                                                    % (moment_order,
                                                       self._max_moment_order))
    binomial_table = tf.slice(self._binomial_table, [moment_order, 0],
                              [1, moment_order + 1])
    # qs = [1 q q^2 ... q^L] = exp([0 1 2 ... L] * log(q))
    qs = tf.exp(tf.constant([i * 1.0 for i in range(moment_order + 1)],
                            dtype=tf.float64) * tf.cast(
                                tf.log(q), dtype=tf.float64))
    moments0 = self._differential_moments(sigma, 0.0, moment_order)
    term0 = tf.reduce_sum(binomial_table * qs * moments0)
    moments1 = self._differential_moments(sigma, 1.0, moment_order)
    term1 = tf.reduce_sum(binomial_table * qs * moments1)
    return tf.squeeze(tf.log(tf.cast(q * term0 + (1.0 - q) * term1,
                                     tf.float64)))
Example #2
0
def cross_entropy(output, target):
    """Returns the cost function of Cross-entropy of two distributions, implement
    softmax internally.

    Parameters
    ----------
    output : Tensorflow variable
        A distribution with shape: [None, n_feature].
    target : Tensorflow variable
        A distribution with shape: [None, n_feature].

    Examples
    --------
    >>> ce = tf.cost.cross_entropy(y_logits, y_target_logits)

    Notes
    -----
    About cross-entropy: `wiki <https://en.wikipedia.org/wiki/Cross_entropy>`_.\n
    The code is borrowed from: `here <https://en.wikipedia.org/wiki/Cross_entropy>`_.
    """
    with tf.name_scope("cross_entropy_loss"):
        net_output_tf = output
        target_tf = target
        cross_entropy = tf.add(tf.mul(tf.log(net_output_tf, name=None),target_tf),
                             tf.mul(tf.log(1 - net_output_tf), (1 - target_tf)))
        return -1 * tf.reduce_mean(tf.reduce_sum(cross_entropy, 1), name='cross_entropy_mean')
def inverse_transform_box(bbox, height, width):
    """ Transform the bounding box format 
        Args:
            bbox: [N X 4] input N bbox
                  format = [left top right bottom]                  
            height: height of original image
            width: width of original image

        Return:
            bbox: [N X 4] output rounded N bbox
                  fromat = [cx, cy, log(w/W), log(h/H)]
    """
    x1, y1, x2, y2 = tf.split(1, 4, bbox)

    w = x2 - x1
    h = y2 - y1
    x = x1 + w / 2
    y = y1 + h / 2

    x /= width / 2
    y /= height / 2
    x -= 1
    y -= 1
    w = tf.log(w / width)
    h = tf.log(h / height)

    bbox_out = tf.concat(1, [x, y, h, w])

    return bbox_out
Example #4
0
 def _create_loss_optimizer(self):
     # The loss is composed of two terms:
     # 1.) The reconstruction loss (the negative log probability
     #     of the input under the reconstructed Bernoulli distribution
     #     induced by the decoder in the data space).
     #     This can be interpreted as the number of "nats" required
     #     for reconstructing the input when the activation in latent
     #     is given.
     # Adding 1e-10 to avoid evaluatio of log(0.0)
     reconstr_loss = \
         -tf.reduce_sum(self.x * tf.log(1e-10 + self.x_reconstr_mean)
                        + (1 - self.x) * tf.log(1e-10 + 1 - self.x_reconstr_mean),
                        1)
     # 2.) The latent loss, which is defined as the Kullback Leibler divergence
     #     between the distribution in latent space induced by the encoder on
     #     the data and some prior. This acts as a kind of regularizer.
     #     This can be interpreted as the number of "nats" required
     #     for transmitting the the latent space distribution given
     #     the prior.
     latent_loss = -0.5 * tf.reduce_sum(1 + self.z_log_sigma_sq
                                        - tf.square(self.z_mean)
                                        - tf.exp(self.z_log_sigma_sq), 1)
     self.cost = tf.reduce_mean(reconstr_loss + latent_loss)  # average over batch
     # Use ADAM optimizer
     self.optimizer = \
         tf.train.AdamOptimizer(learning_rate=self.learning_rate).minimize(self.cost)
Example #5
0
        tf.multiply(tf.tile(query_rnn_output, [NEG + 1, 1]), doc_y), 1, True)
    norm_prod = tf.multiply(query_norm, doc_norm)

    # cos_sim_raw = query * doc / (||query|| * ||doc||)
    cos_sim_raw = tf.truediv(prod, norm_prod)
    # gamma = 20
    cos_sim = tf.transpose(
        tf.reshape(tf.transpose(cos_sim_raw), [NEG + 1, query_BS])) * 20

with tf.name_scope('Loss'):
    # Train Loss
    # 转化为softmax概率矩阵。
    prob = tf.nn.softmax(cos_sim)
    # 只取第一列,即正样本列概率。
    hit_prob = tf.slice(prob, [0, 0], [-1, 1])
    loss = -tf.reduce_sum(tf.log(hit_prob))
    tf.summary.scalar('loss', loss)

with tf.name_scope('Training'):
    # Optimizer
    train_step = tf.train.AdamOptimizer(conf.learning_rate).minimize(loss)

# with tf.name_scope('Accuracy'):
#     correct_prediction = tf.equal(tf.argmax(prob, 1), 0)
#     accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#     tf.summary.scalar('accuracy', accuracy)

merged = tf.summary.merge_all()

with tf.name_scope('Test'):
    average_loss = tf.placeholder(tf.float32)
Example #6
0
def train_neural_networks():
    decoded, predict_output = neural_networks()

    us_cost_function = tf.reduce_mean(tf.pow(X - decoded, 2))
    s_cost_function = -tf.reduce_sum(Y * tf.log(predict_output))
    us_optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=0.01).minimize(us_cost_function)
    s_optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=0.01).minimize(s_cost_function)

    correct_prediction = tf.equal(tf.argmax(predict_output, 1),
                                  tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    training_epochs = 20
    batch_size = 10
    total_batches = training_data.shape[0]
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        # ------------ Training Autoencoders - Unsupervised Learning ----------- #
        # autoencoder是一种非监督学习算法,他利用反向传播算法,让目标值等于输入值
        for epoch in range(training_epochs):
            epoch_costs = np.empty(0)
            for b in range(total_batches):
                offset = (b * batch_size) % (train_x.shape[0] - batch_size)
                batch_x = train_x[offset:(offset + batch_size), :]
                _, c = sess.run([us_optimizer, us_cost_function],
                                feed_dict={X: batch_x})
                epoch_costs = np.append(epoch_costs, c)
            print("Epoch: ", epoch, " Loss: ", np.mean(epoch_costs))
        print(
            "------------------------------------------------------------------"
        )

        # ---------------- Training NN - Supervised Learning ------------------ #
        for epoch in range(training_epochs):
            epoch_costs = np.empty(0)
            for b in range(total_batches):
                offset = (b * batch_size) % (train_x.shape[0] - batch_size)
                batch_x = train_x[offset:(offset + batch_size), :]
                batch_y = train_y[offset:(offset + batch_size), :]
                _, c = sess.run([s_optimizer, s_cost_function],
                                feed_dict={
                                    X: batch_x,
                                    Y: batch_y
                                })
                epoch_costs = np.append(epoch_costs, c)

            accuracy_in_train_set = sess.run(accuracy,
                                             feed_dict={
                                                 X: train_x,
                                                 Y: train_y
                                             })
            accuracy_in_test_set = sess.run(accuracy,
                                            feed_dict={
                                                X: test_x,
                                                Y: test_y
                                            })
            print("Epoch: ", epoch, " Loss: ", np.mean(epoch_costs),
                  " Accuracy: ", accuracy_in_train_set, ' ',
                  accuracy_in_test_set)
Example #7
0
def createModel():                        
    # Create the model
    x = tf.placeholder(tf.float32, [None, 784])
    y_ = tf.placeholder(tf.float32, [None, 10])
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    W_conv1 = weight_variable([5, 5, 1, 32])
    b_conv1 = bias_variable([32])

    x_image = tf.reshape(x, [-1,28,28,1])
    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
    h_pool1 = max_pool_2x2(h_conv1)


    W_conv2 = weight_variable([5, 5, 32, 64])
    b_conv2 = bias_variable([64])

    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
    h_pool2 = max_pool_2x2(h_conv2)

    W_fc1 = weight_variable([7 * 7 * 64, 1024])
    b_fc1 = bias_variable([1024])

    h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    keep_prob = tf.placeholder(tf.float32)
    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    W_fc2 = weight_variable([1024, 10])
    b_fc2 = bias_variable([10])

    y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

    # Define loss and optimizer
    cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
    correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    """
    Train the model and save the model to disk as a model.ckpt file
    file is stored in the same directory as this python script is started
    Based on the documentatoin at
    https://www.tensorflow.org/versions/master/how_tos/variables/index.html
    """
    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    for i in range(20000):
        batch = mnist.train.next_batch(50)
        if i%100 == 0:
            train_accuracy = accuracy.eval(feed_dict={
            x:batch[0], y_: batch[1], keep_prob: 1.0})
        print("step %d, training accuracy %g"%(i, train_accuracy))
        train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    save_path = saver.save(sess, os.path.join(FLAGS.log_dir, 'model.ckpt'))
    print ("Model saved in file: ", save_path)
Example #8
0
 def _inc_loss_psi(self, psi, signal, t):
     return - tf.log(1. + self._expectation(psi, t) * signal / self.A)
#将2D图片结构转换为1D向量结构,并连接一个全连接层
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64]) #对第二个卷积层的输出tensor进行变形,转换为1D向量
#全连接层
W_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_variable([1024])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1) #隐含节点1024,使用relu激活函数
#dropout层,减轻过拟合
keep_prob=tf.placeholder(tf.float32)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
#softmax层
W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)

#定义损失函数和优化器
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) #给予一个较小的学习速率1e-4

#定义评测准确率
correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

#训练
tf.global_variables_initializer().run() #初始化所有参数
for i in range(20000):
    batch=mnist.train.next_batch(50)
    if i%100==0: #训练中评测时的keep_prob设为1,实时监测模型的性能
        train_accuracy=accuracy.eval(feed_dict={x:batch[0],y_:batch[1],keep_prob:1.0})
        print("step %d,training accuracy %g"%(i,train_accuracy))
    train_step.run(feed_dict={x:batch[0],y_:batch[1],keep_prob:0.5})
Example #10
0
def log(x):
    return tf.log(x)
    division = - tf.divide( pairwise_sq, tf.constant(2*sigma**2, dtype=tf.float32))
    
    match_ab   = tf.exp(division, name='match_ab')
    
    
    
    
    p_ab = tf.nn.softmax(match_ab, name='p_ab')
    p_ba = tf.nn.softmax(tf.transpose(match_ab), name='p_ba')
    p_aba = tf.matmul(p_ab, p_ba, name='p_aba')

    model.create_walk_statistics(p_aba, equality_matrix)
    
    loss_aba = tf.losses.softmax_cross_entropy(
        p_target,
        tf.log(1e-8 + p_aba),
        weights=walker_weight,
        scope='loss_aba')
    model.add_visit_loss(p_ab, visit_weight)

    mab_dt, pab_dt, paba_dt, semb_dt, uemb_dt = tf.gradients([loss_aba], [match_ab, p_ab, p_aba, t_sup_emb, t_unsup_emb])
    
    tf.summary.scalar('Loss_aba', loss_aba)
    
    t_learning_rate = tf.train.exponential_decay(
        learning_rate,
        model.step,
        decay_steps,
        decay_factor,
        staircase = True
    )
Example #12
0
w9 = tf.get_variable("w9", shape=[4*4*256,256],initializer=tf.contrib.layers.xavier_initializer())
b9 = tf.Variable(tf.random_normal([256]), tf.float32)
L9 = tf.nn.elu(tf.matmul(L8,w9) + b9)
L9 = tf.nn.dropout(L9, keep_prob=keep_prob)

w10 = tf.get_variable("w10", shape=[256,32],initializer=tf.contrib.layers.xavier_initializer())
b10 = tf.Variable(tf.random_normal([32]), tf.float32)
L10 = tf.nn.elu(tf.matmul(L9,w10) + b10)
L10 = tf.nn.dropout(L10, keep_prob=keep_prob)

w11 = tf.get_variable("w11", shape=[32,10],initializer=tf.contrib.layers.xavier_initializer())
b11 = tf.Variable(tf.random_normal([10]), tf.float32)
hypothesis = tf.nn.softmax(tf.matmul(L10,w11) + b11)


cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(hypothesis), axis = 1)) ## categorical_crossentropy

train = tf.train.AdamOptimizer(learning_rate=lr).minimize(cost)

predicted = tf.cast(tf.argmax(hypothesis, axis=1), dtype = tf.float32)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(training_epochs):
        avg_cost = 0
        for batch in range(total_batch):
            batch_xs, batch_ys = x_train[batch*batch_size : (batch+1)*batch_size], y_train[batch*batch_size : (batch+1)*batch_size]
            feed_dict = {x_imag:batch_xs, y:batch_ys, keep_prob:0.8}
            cost_val, _ = sess.run([cost, train], feed_dict=feed_dict)
            print(step, 'cost :', cost_val)#, '\n',hyp_val)
            avg_cost += cost_val/total_batch
Example #13
0
 def _get_loss(self, eps):
     return -tf.reduce_mean(
         self._tfy * tf.log(self._output + eps) + (1 - self._tfy) * tf.log(1 - self._output + eps)
     )
Example #14
0
    def build(self):
        hparam = self.hparam

        code = tf.placeholder("float32", [None, hparam.code_dim], name='code')
        real_seq = tf.placeholder("int32", [None, hparam.timesteps],
                                  name='real_seq')
        real_seq_img = tf.one_hot(real_seq, hparam.vocab_size)
        dis_train = tf.placeholder('bool', name='is_train')
        bs = tf.shape(code)[0]

        # generator
        with tf.variable_scope('generator'):
            cells = rnn.MultiRNNCell(
                [hparam.basic_cell(c) for c in hparam.cells])
            # play with code
            # code_with_timestep = tf.concat(
            #     [
            #         tf.tile(tf.expand_dims(code, 1),
            #                 (1, hparam.timesteps, 1)),
            #         tf.tile(tf.expand_dims(tf.eye(hparam.timesteps), 0),
            #                 (bs, 1, 1)),
            #     ], -1
            # )
            code_with_timestep = tf.concat([
                tf.tile(tf.expand_dims(code, 1), (1, hparam.timesteps, 1)),
                tf.tile(
                    tf.expand_dims(
                        tf.expand_dims(tf.lin_space(0., 1., hparam.timesteps),
                                       0), -1), (bs, 1, 1)),
            ], -1)
            outputs, states = tf.nn.dynamic_rnn(
                cells,
                code_with_timestep,
                dtype=tf.float32,
            )
            fake_seq_img = tf.nn.softmax(
                layers.linear(outputs[:, :, :], hparam.vocab_size), -1)
            # fake_seq_img = outputs
            # fake_seq_img = tf.nn.softmax(fake_seq_img)
            fake_seq = tf.argmax(fake_seq_img, -1)

        # discriminator
        def dis(seq_img, bn_scope, reuse=False):
            with tf.variable_scope('discriminator', reuse=reuse):
                # x = tf.nn.embedding_lookup(embeddings, seq)
                x = tf.expand_dims(seq_img, -1)
                x = ResNetBuilder(dis_train,
                                  bn_scopes=['fake', 'real'],
                                  bn_scope=bn_scope).\
                    resnet(x, structure=[2, 2, 2, 2], filters=4, nb_class=1)
                x = tf.nn.sigmoid(x)
            return x

        # opt
        # problematic with the reuse bn
        fake_dis_pred = dis(fake_seq_img, bn_scope='fake')
        real_dis_pred = dis(real_seq_img, bn_scope='real', reuse=True)

        G_loss = tf.reduce_mean(-tf.log(fake_dis_pred))
        D_loss = tf.reduce_mean(-tf.log(real_dis_pred)) +\
            tf.reduce_mean(-tf.log(1-fake_dis_pred))

        G_opt = tf.train.AdamOptimizer(learning_rate=hparam.G_lr,
                                       beta1=0.5,
                                       beta2=0.9)
        D_opt = tf.train.AdamOptimizer(learning_rate=hparam.D_lr,
                                       beta1=0.5,
                                       beta2=0.9)
        D_iter = tf.Variable(0, name='D_iter')
        G_iter = tf.Variable(0, name='G_iter')
        G_train_op = slim.learning.create_train_op(
            G_loss,
            G_opt,
            variables_to_train=tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, "generator"),
            global_step=G_iter,
            clip_gradient_norm=hparam.G_clipnorm)
        D_train_op = slim.learning.create_train_op(
            D_loss,
            D_opt,
            variables_to_train=tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES, "discriminator"),
            global_step=D_iter,
        )
        iter_step = tf.Variable(0, name='iter_step')
        iter_step_op = iter_step.assign_add(1)

        # input
        self.code = code
        self.real_seq = real_seq
        # summary
        self.summary_fake_img = tf.summary.image(
            'fake_img', tf.expand_dims(fake_seq_img, -1))
        self.summary_real_img = tf.summary.image(
            'real_img', tf.expand_dims(real_seq_img, -1))
        self.summary_G_loss = tf.summary.scalar('G_loss', G_loss)
        self.summary_D_loss = tf.summary.scalar('D_loss', D_loss)
        self.summary_fake_dis_pred = tf.summary.scalar(
            'fake_dis_pred', tf.reduce_mean(fake_dis_pred))
        self.summary_real_dis_pred = tf.summary.scalar(
            'real_dis_pred', tf.reduce_mean(real_dis_pred))
        # debug
        self.fake_seq_img = fake_seq_img
        # train
        self.dis_train = dis_train
        self.G_train_op = G_train_op
        self.D_train_op = D_train_op
        self.iter_step = iter_step
        self.iter_step_op = iter_step_op
        # output
        self.fake_seq = fake_seq
        self.built = True
Example #15
0
def log2(x):
    with tf.name_scope('Log2'):
        return tf.log(x) * np.float32(1.0 / np.log(2.0))
Example #16
0
    def __init__(self, dropout=1.0):
        tf.reset_default_graph()
        with tf.variable_scope(NAMESPACE):
            config = tf.ConfigProto(allow_soft_placement=True)
            self.sess = tf.Session(config=config)

            # Input variables
            self.item = tf.placeholder(tf.float32, shape=(None, self._nodes_vocab_size), name='item')
            self.question_vectors_fw = tf.placeholder(tf.float32, shape=(None, None, self._question_vocab_size),
                                                      name='question_vectors_inp_fw')
            self.question_vectors_bw = tf.placeholder(tf.float32, shape=(None, None, self._question_vocab_size),
                                                      name='question_vectors_inp_nw')
            self.question_mask = tf.placeholder(tf.float32, shape=(None, None, self._mask_size),
                                                name='question_mask')

            # The question is pre-processed by a bi-GRU
            self.Wq = tf.Variable(tf.random_uniform([self._question_vocab_size,
                                                     self._word_proj_size_for_rnn], -_rw, _rw))
            self.bq = tf.Variable(tf.random_uniform([self._word_proj_size_for_rnn], -_rw, _rw))
            self.internal_projection = lambda x: tf.nn.relu(tf.matmul(x, self.Wq) + self.bq)
            self.question_int_fw = tf.map_fn(self.internal_projection, self.question_vectors_fw)
            self.question_int_bw = tf.map_fn(self.internal_projection, self.question_vectors_bw)

            self.rnn_cell_fw = rnn.MultiRNNCell([rnn.GRUCell(self._memory_dim) for _ in range(self._stack_dimension)],
                                                state_is_tuple=True)
            self.rnn_cell_bw = rnn.MultiRNNCell([rnn.GRUCell(self._memory_dim) for _ in range(self._stack_dimension)],
                                                state_is_tuple=True)
            with tf.variable_scope('fw'):
                output_fw, state_fw = tf.nn.dynamic_rnn(self.rnn_cell_fw, self.question_int_fw, time_major=True,
                                                        dtype=tf.float32)
            with tf.variable_scope('bw'):
                output_bw, state_bw = tf.nn.dynamic_rnn(self.rnn_cell_bw, self.question_int_bw, time_major=True,
                                                        dtype=tf.float32)

            self.states = tf.concat(values=[output_fw, tf.reverse(output_bw, [0])], axis=2)
            self.question_vector_pre = tf.reduce_mean(tf.multiply(self.question_mask, self.states), axis=0)
            self.Wqa = tf.Variable(
                tf.random_uniform([2 * self._memory_dim, self._question_vector_size], -_rw, _rw),
                name='Wqa')
            self.bqa = tf.Variable(tf.random_uniform([self._question_vector_size], -_rw, _rw), name='bqa')
            self.question_vector = tf.nn.relu(tf.matmul(self.question_vector_pre, self.Wqa) + self.bqa)

            # Item
            self.Wit = tf.Variable(tf.random_uniform([self._nodes_vocab_size,
                                                     self._word_proj_size_for_item], -_rw, _rw))
            self.bit = tf.Variable(tf.random_uniform([self._word_proj_size_for_item], -_rw, _rw))
            self.item_proj = tf.nn.relu(tf.matmul(self.item, self.Wit) + self.bit)


            # Concatenate
            self.concatenated = tf.concat(values=[self.question_vector, self.item_proj], axis=1)

            # Final feedforward layers
            self.Ws1 = tf.Variable(
                tf.random_uniform([self._question_vector_size
                                   + self._word_proj_size_for_item,
                                   self._hidden_layer2_size], -_rw, _rw),
                name='Ws1')
            self.bs1 = tf.Variable(tf.random_uniform([self._hidden_layer2_size], -_rw, _rw), name='bs1')
            self.first_hidden = tf.nn.relu(tf.matmul(self.concatenated, self.Ws1) + self.bs1)
            self.first_hidden_dropout = tf.nn.dropout(self.first_hidden, dropout)

            self.Wf = tf.Variable(
                tf.random_uniform([self._hidden_layer2_size, self._output_size], -_rw,
                                  _rw),
                name='Wf')
            self.bf = tf.Variable(tf.random_uniform([self._output_size], -_rw, _rw), name='bf')
            self.outputs = tf.nn.softmax(tf.matmul(self.first_hidden_dropout, self.Wf) + self.bf)

            # Loss function and training
            self.y_ = tf.placeholder(tf.float32, shape=(None, self._output_size), name='y_')
            self.outputs2 = tf.squeeze(self.outputs)
            self.y2_ = tf.squeeze(self.y_)
            self.one = tf.ones_like(self.outputs)
            self.tiny = self.one * TINY
            self.cross_entropy = (tf.reduce_mean(
                -tf.reduce_sum(self.y_ * tf.log(self.outputs + self.tiny) * _weight_for_positive_matches
                               + (self.one - self.y_) * tf.log(
                    self.one - self.outputs + self.tiny))
            ))

        # Clipping the gradient
        optimizer = tf.train.AdamOptimizer(1e-4)
        gvs = optimizer.compute_gradients(self.cross_entropy)
        capped_gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs if var.name.find(NAMESPACE) != -1]
        self.train_step = optimizer.apply_gradients(capped_gvs)
        self.sess.run(tf.global_variables_initializer())

        # Adding the summaries
        tf.summary.scalar('cross_entropy', self.cross_entropy)
        self.merged = tf.summary.merge_all()
        self.train_writer = tf.summary.FileWriter('./train', self.sess.graph)
Example #17
0
            # 乘出来之后每一列就是对应的一组weight值。
            # shape of tf.matmul(input, Weights): [batch_size, layer_size]
            # shape of Biases: [layer_size]
            output = tf.matmul(input, Weights) + Biases
            # 用sigmoid不行,应该是在BP更新参数的时候会非常受影响。
            output = tf.nn.softmax(output)

        # [batch_size, layer_size]
        return output

    x = tf.placeholder(shape=[None, 784], dtype=tf.float32)
    y = tf.placeholder(shape=[None, 10], dtype=tf.float32)

    prediction = hidden_layer(x)
    cross_entropy = tf.reduce_mean(
        -tf.reduce_sum(y * tf.log(prediction), reduction_indices=[1]))

    optimizer = tf.train.GradientDescentOptimizer(0.5)
    train = optimizer.minimize(cross_entropy)

    correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    init = tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)
    for i in range(epoch):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        # 以下这个形式是错的,因为每次next batch 会随机产生不同的数据
        # batch_xs = mnist.train.next_batch(batch_size)[0]
        # batch_ys = mnist.train.next_batch(batch_size)[1]
Example #18
0
b_enc = tf.Variable(tf.zeros([625]))
b_dec = tf.Variable(tf.zeros([784]))


# Create the model
def model(X, w_e, b_e, w_d, b_d):
    encoded = tf.sigmoid(tf.matmul(X, w_e) + b_e)
    decoded = tf.sigmoid(tf.matmul(encoded, w_d) + b_d)

    return encoded, decoded


encoded, decoded = model(x, w_enc, b_enc, w_dec, b_dec)

# Cost Function basic term
cross_entropy = -1. * x * tf.log(decoded) - (1. - x) * tf.log(1. - decoded)
loss = tf.reduce_mean(cross_entropy)
train_step = tf.train.AdagradOptimizer(0.1).minimize(loss)

# Train
init = tf.initialize_all_variables()
#Create a summary to monitor cost tensor
summa = tf.summary.scalar("loss", loss)

with tf.Session() as sess:
    sess.run(init)
    summary_writer = tf.summary.FileWriter('./graphs/simsc',
                                           graph=tf.get_default_graph())
    print('Training...')
    for i in range(1001):
        batch_xs, batch_ys = mnist.train.next_batch(128)
Example #19
0
    def setup_model(self):
        with SetVerbosity(self.verbose):

            assert issubclass(self.policy, ActorCriticPolicy), "Error: the input policy for the ACER model must be " \
                                                               "an instance of common.policies.ActorCriticPolicy."

            if isinstance(self.action_space, Discrete):
                self.n_act = self.action_space.n
                continuous = False
            elif isinstance(self.action_space, Box):
                # self.n_act = self.action_space.shape[-1]
                # continuous = True
                raise NotImplementedError(
                    "WIP: Acer does not support Continuous actions yet.")
            else:
                raise ValueError(
                    "Error: ACER does not work with {} actions space.".format(
                        self.action_space))

            self.n_batch = self.n_envs * self.n_steps

            self.graph = tf.Graph()
            with self.graph.as_default():
                self.sess = tf_util.make_session(num_cpu=self.n_cpu_tf_sess,
                                                 graph=self.graph)
                self.set_random_seed(self.seed)
                n_batch_step = None
                if issubclass(self.policy, RecurrentActorCriticPolicy):
                    n_batch_step = self.n_envs
                n_batch_train = self.n_envs * (self.n_steps + 1)

                step_model = self.policy(self.sess,
                                         self.observation_space,
                                         self.action_space,
                                         self.n_envs,
                                         1,
                                         n_batch_step,
                                         reuse=False,
                                         **self.policy_kwargs)

                self.params = tf_util.get_trainable_vars("model")

                with tf.variable_scope(
                        "train_model",
                        reuse=True,
                        custom_getter=tf_util.outer_scope_getter(
                            "train_model")):
                    train_model = self.policy(self.sess,
                                              self.observation_space,
                                              self.action_space,
                                              self.n_envs,
                                              self.n_steps + 1,
                                              n_batch_train,
                                              reuse=True,
                                              **self.policy_kwargs)

                with tf.variable_scope("moving_average"):
                    # create averaged model
                    ema = tf.train.ExponentialMovingAverage(self.alpha)
                    ema_apply_op = ema.apply(self.params)

                    def custom_getter(getter, name, *args, **kwargs):
                        name = name.replace("polyak_model/", "")
                        val = ema.average(getter(name, *args, **kwargs))
                        return val

                with tf.variable_scope("polyak_model",
                                       reuse=True,
                                       custom_getter=custom_getter):
                    self.polyak_model = polyak_model = self.policy(
                        self.sess,
                        self.observation_space,
                        self.action_space,
                        self.n_envs,
                        self.n_steps + 1,
                        self.n_envs * (self.n_steps + 1),
                        reuse=True,
                        **self.policy_kwargs)

                with tf.variable_scope("loss", reuse=False):
                    self.done_ph = tf.placeholder(tf.float32,
                                                  [self.n_batch])  # dones
                    self.reward_ph = tf.placeholder(
                        tf.float32, [self.n_batch])  # rewards, not returns
                    self.mu_ph = tf.placeholder(
                        tf.float32, [self.n_batch, self.n_act])  # mu's
                    self.action_ph = train_model.pdtype.sample_placeholder(
                        [self.n_batch])
                    self.learning_rate_ph = tf.placeholder(tf.float32, [])
                    eps = 1e-6

                    # Notation: (var) = batch variable, (var)s = sequence variable,
                    # (var)_i = variable index by action at step i
                    # shape is [n_envs * (n_steps + 1)]
                    if continuous:
                        value = train_model.value_flat
                    else:
                        value = tf.reduce_sum(train_model.policy_proba *
                                              train_model.q_value,
                                              axis=-1)

                    rho, rho_i_ = None, None
                    if continuous:
                        action_ = strip(
                            train_model.proba_distribution.sample(),
                            self.n_envs, self.n_steps)
                        distribution_f = tf.contrib.distributions.MultivariateNormalDiag(
                            loc=strip(train_model.proba_distribution.mean,
                                      self.n_envs, self.n_steps),
                            scale_diag=strip(
                                train_model.proba_distribution.logstd,
                                self.n_envs, self.n_steps))
                        f_polyak = tf.contrib.distributions.MultivariateNormalDiag(
                            loc=strip(polyak_model.proba_distribution.mean,
                                      self.n_envs, self.n_steps),
                            scale_diag=strip(
                                polyak_model.proba_distribution.logstd,
                                self.n_envs, self.n_steps))

                        f_i = distribution_f.prob(self.action_ph)
                        f_i_ = distribution_f.prob(action_)
                        f_polyak_i = f_polyak.prob(self.action_ph)
                        phi_i = strip(train_model.proba_distribution.mean,
                                      self.n_envs, self.n_steps)

                        q_value = strip(train_model.value_fn, self.n_envs,
                                        self.n_steps)
                        q_i = q_value[:, 0]

                        rho_i = tf.reshape(f_i, [-1, 1]) / (self.mu_ph + eps)
                        rho_i_ = tf.reshape(f_i_, [-1, 1]) / (self.mu_ph + eps)

                        qret = q_retrace(self.reward_ph, self.done_ph, q_i,
                                         value, tf.pow(rho_i, 1 / self.n_act),
                                         self.n_envs, self.n_steps, self.gamma)
                    else:
                        # strip off last step
                        # f is a distribution, chosen to be Gaussian distributions
                        # with fixed diagonal covariance and mean \phi(x)
                        # in the paper
                        distribution_f, f_polyak, q_value = \
                            map(lambda variables: strip(variables, self.n_envs, self.n_steps),
                                [train_model.policy_proba, polyak_model.policy_proba, train_model.q_value])

                        # Get pi and q values for actions taken
                        f_i = get_by_index(distribution_f, self.action_ph)
                        f_i_ = distribution_f
                        phi_i = distribution_f
                        f_polyak_i = f_polyak

                        q_i = get_by_index(q_value, self.action_ph)

                        # Compute ratios for importance truncation
                        rho = distribution_f / (self.mu_ph + eps)
                        rho_i = get_by_index(rho, self.action_ph)

                        # Calculate Q_retrace targets
                        qret = q_retrace(self.reward_ph, self.done_ph, q_i,
                                         value, rho_i, self.n_envs,
                                         self.n_steps, self.gamma)

                    # Calculate losses
                    # Entropy
                    entropy = tf.reduce_sum(
                        train_model.proba_distribution.entropy())

                    # Policy Gradient loss, with truncated importance sampling & bias correction
                    value = strip(value, self.n_envs, self.n_steps, True)
                    # check_shape([qret, value, rho_i, f_i], [[self.n_envs * self.n_steps]] * 4)
                    # check_shape([rho, distribution_f, q_value], [[self.n_envs * self.n_steps, self.n_act]] * 2)

                    # Truncated importance sampling
                    adv = qret - value
                    log_f = tf.log(f_i + eps)
                    # [n_envs * n_steps]
                    gain_f = log_f * tf.stop_gradient(
                        adv * tf.minimum(self.correction_term, rho_i))
                    loss_f = -tf.reduce_mean(gain_f)

                    # Bias correction for the truncation
                    adv_bc = (
                        q_value -
                        tf.reshape(value, [self.n_envs * self.n_steps, 1])
                    )  # [n_envs * n_steps, n_act]

                    # check_shape([adv_bc, log_f_bc], [[self.n_envs * self.n_steps, self.n_act]] * 2)
                    if continuous:
                        gain_bc = tf.stop_gradient(
                            adv_bc * tf.nn.relu(1.0 - (self.correction_term /
                                                       (rho_i_ + eps))) * f_i_)
                    else:
                        log_f_bc = tf.log(f_i_ + eps)  # / (f_old + eps)
                        gain_bc = tf.reduce_sum(log_f_bc * tf.stop_gradient(
                            adv_bc * tf.nn.relu(1.0 - (self.correction_term /
                                                       (rho + eps))) * f_i_),
                                                axis=1)
                    # IMP: This is sum, as expectation wrt f
                    loss_bc = -tf.reduce_mean(gain_bc)

                    loss_policy = loss_f + loss_bc

                    # Value/Q function loss, and explained variance
                    check_shape([qret, q_i],
                                [[self.n_envs * self.n_steps]] * 2)
                    explained_variance = q_explained_variance(
                        tf.reshape(q_i, [self.n_envs, self.n_steps]),
                        tf.reshape(qret, [self.n_envs, self.n_steps]))
                    loss_q = tf.reduce_mean(
                        tf.square(tf.stop_gradient(qret) - q_i) * 0.5)

                    # Net loss
                    check_shape([loss_policy, loss_q, entropy], [[]] * 3)
                    loss = loss_policy + self.q_coef * loss_q - self.ent_coef * entropy

                    tf.summary.scalar('entropy_loss', entropy)
                    tf.summary.scalar('policy_gradient_loss', loss_policy)
                    tf.summary.scalar('value_function_loss', loss_q)
                    tf.summary.scalar('loss', loss)

                    norm_grads_q, norm_grads_policy, avg_norm_grads_f = None, None, None
                    avg_norm_k, avg_norm_g, avg_norm_k_dot_g, avg_norm_adj = None, None, None, None
                    if self.trust_region:
                        # [n_envs * n_steps, n_act]
                        grad = tf.gradients(
                            -(loss_policy - self.ent_coef * entropy) *
                            self.n_steps * self.n_envs, phi_i)
                        # [n_envs * n_steps, n_act] # Directly computed gradient of KL divergence wrt f
                        kl_grad = -f_polyak_i / (f_i_ + eps)
                        k_dot_g = tf.reduce_sum(kl_grad * grad, axis=-1)
                        adj = tf.maximum(
                            0.0, (tf.reduce_sum(kl_grad * grad, axis=-1) -
                                  self.delta) /
                            (tf.reduce_sum(tf.square(kl_grad), axis=-1) +
                             eps))  # [n_envs * n_steps]

                        # Calculate stats (before doing adjustment) for logging.
                        avg_norm_k = avg_norm(kl_grad)
                        avg_norm_g = avg_norm(grad)
                        avg_norm_k_dot_g = tf.reduce_mean(tf.abs(k_dot_g))
                        avg_norm_adj = tf.reduce_mean(tf.abs(adj))

                        grad = grad - tf.reshape(
                            adj, [self.n_envs * self.n_steps, 1]) * kl_grad
                        # These are turst region adjusted gradients wrt f ie statistics of policy pi
                        grads_f = -grad / (self.n_envs * self.n_steps)
                        grads_policy = tf.gradients(f_i_, self.params, grads_f)
                        grads_q = tf.gradients(loss_q * self.q_coef,
                                               self.params)
                        grads = [
                            gradient_add(g1, g2, param, verbose=self.verbose)
                            for (g1, g2, param
                                 ) in zip(grads_policy, grads_q, self.params)
                        ]

                        avg_norm_grads_f = avg_norm(grads_f) * (self.n_steps *
                                                                self.n_envs)
                        norm_grads_q = tf.global_norm(grads_q)
                        norm_grads_policy = tf.global_norm(grads_policy)
                    else:
                        grads = tf.gradients(loss, self.params)

                    norm_grads = None
                    if self.max_grad_norm is not None:
                        grads, norm_grads = tf.clip_by_global_norm(
                            grads, self.max_grad_norm)
                    grads = list(zip(grads, self.params))

                with tf.variable_scope("input_info", reuse=False):
                    tf.summary.scalar('rewards',
                                      tf.reduce_mean(self.reward_ph))
                    tf.summary.scalar('learning_rate',
                                      tf.reduce_mean(self.learning_rate))
                    tf.summary.scalar('advantage', tf.reduce_mean(adv))
                    tf.summary.scalar('action_probability',
                                      tf.reduce_mean(self.mu_ph))

                    if self.full_tensorboard_log:
                        tf.summary.histogram('rewards', self.reward_ph)
                        tf.summary.histogram('learning_rate',
                                             self.learning_rate)
                        tf.summary.histogram('advantage', adv)
                        tf.summary.histogram('action_probability', self.mu_ph)
                        if tf_util.is_image(self.observation_space):
                            tf.summary.image('observation', train_model.obs_ph)
                        else:
                            tf.summary.histogram('observation',
                                                 train_model.obs_ph)

                trainer = tf.train.RMSPropOptimizer(
                    learning_rate=self.learning_rate_ph,
                    decay=self.rprop_alpha,
                    epsilon=self.rprop_epsilon)
                _opt_op = trainer.apply_gradients(grads)

                # so when you call _train, you first do the gradient step, then you apply ema
                with tf.control_dependencies([_opt_op]):
                    _train = tf.group(ema_apply_op)

                # Ops/Summaries to run, and their names for logging
                assert norm_grads is not None
                run_ops = [
                    _train, loss, loss_q, entropy, loss_policy, loss_f,
                    loss_bc, explained_variance, norm_grads
                ]
                names_ops = [
                    'loss', 'loss_q', 'entropy', 'loss_policy', 'loss_f',
                    'loss_bc', 'explained_variance', 'norm_grads'
                ]
                if self.trust_region:
                    self.run_ops = run_ops + [
                        norm_grads_q, norm_grads_policy, avg_norm_grads_f,
                        avg_norm_k, avg_norm_g, avg_norm_k_dot_g, avg_norm_adj
                    ]
                    self.names_ops = names_ops + [
                        'norm_grads_q', 'norm_grads_policy',
                        'avg_norm_grads_f', 'avg_norm_k', 'avg_norm_g',
                        'avg_norm_k_dot_g', 'avg_norm_adj'
                    ]

                self.train_model = train_model
                self.step_model = step_model
                self.step = step_model.step
                self.proba_step = step_model.proba_step
                self.initial_state = step_model.initial_state

                tf.global_variables_initializer().run(session=self.sess)

                self.summary = tf.summary.merge_all()
Example #20
0
def _MixN(fractions, Xs, name=None):
    # Convert Xs to be iterable incase we are dealing with the 1D case
    Xs = [(x, ) if isinstance(x, tf.Tensor) else tuple(x) for x in Xs]

    # Ensure all subdistributions have the same dimensionality
    if len(set(len(x) for x in Xs)) != 1:
        raise DistributionError("All components passed to 'MixN' must have "
                                "the same dimensionality.")

    n_dims = len(Xs[0])

    nd_X = tuple(
        tf.placeholder(config.dtype, name=name) for i in range(n_dims))
    nd_mix_bounds = Distribution.bounds(n_dims)

    current_model = Model._current_model

    full_pdf = []
    all_integrals = []
    for dists, f_scale in zip(Xs, fractions):
        nd_logps = []
        nd_bounds = []
        nd_integrals = []
        nd_normalisation_1 = []
        for dist, mix_bounds, X in zip(dists, nd_mix_bounds, nd_X):
            logp, integral, bounds, frac, _ = current_model._description[dist]
            bounds = find_common_bounds(mix_bounds, bounds)
            normalisation_1 = _integrate_component(bounds, integral)

            nd_logps.append(logp - tf.log(normalisation_1))
            nd_bounds.append(bounds)
            nd_integrals.append(integral)
            nd_normalisation_1.append(normalisation_1)

            # Modify the current model to recognize that 'deps' has been removed
            if dist in current_model._silently_replace.values():
                # We need to copy the items to a list as we're adding items
                for key, value in list(
                        current_model._silently_replace.items()):
                    if value != dist:
                        continue
                    current_model._silently_replace[value] = X
                    current_model._silently_replace[key] = X
                    if dist in current_model._description:
                        del current_model._description[dist]

            else:
                current_model._silently_replace[dist] = X
                del current_model._description[dist]

            _recurse_deps(dist, f_scale, bounds)

        full_pdf.append(f_scale * tf.exp(tf.add_n(nd_logps)))

        all_integrals.append(
            (f_scale, nd_bounds, nd_integrals, nd_normalisation_1))

    # Set properties on Distribution
    Distribution.logp = tf.log(tf.add_n(full_pdf))

    def _integral(lower, upper):
        result = []
        for f_scale, nd_bounds, nd_integral, normalisation_1 in all_integrals:
            nd_normalisation_2 = []
            for bounds, integral in zip(nd_bounds, nd_integral):
                integral_bounds = find_common_bounds([Region(lower, upper)],
                                                     bounds)
                nd_normalisation_2.append(
                    _integrate_component(integral_bounds, integral))
            result.append(f_scale / tf.add_n(normalisation_1) *
                          tf.add_n(nd_normalisation_2))
        return tf.add_n(result)

    Distribution.integral = _integral

    if n_dims == 1:
        Distribution.depends = [x[0] for x in Xs]
    else:
        Distribution.depends = Xs

    return nd_X
Example #21
0
    def build_graph(self):
        para_embeddings_op = self.elmo_bilm(self.para)
        que_embeddings_op = self.elmo_bilm(self.que)
        with tf.variable_scope('elmo_encodings_input'):
            elmo_para_input = weight_layers('input', para_embeddings_op, l2_coef=0.)['weighted_op']
        with tf.variable_scope('elmo_encodings_input', reuse=True):
            elmo_que_input = weight_layers('input', que_embeddings_op, l2_coef=0.)['weighted_op']
        with tf.variable_scope('elmo_encodings_output'):
            elmo_para_output = weight_layers('output', para_embeddings_op, l2_coef=0.)['weighted_op']
        with tf.variable_scope('elmo_encodings_output', reuse=True):
            elmo_que_output = weight_layers('output', que_embeddings_op, l2_coef=0.)['weighted_op']

        with tf.variable_scope("embedding"):
            with tf.device("/cpu:0"):
                ch_emb = tf.reshape(tf.nn.embedding_lookup(
                        self.char_mat, self.para_char), [self.N * self.PL, self.CL, self.dc])
                qh_emb = tf.reshape(tf.nn.embedding_lookup(
                        self.char_mat, self.que_char), [self.N * self.QL, self.CL, self.dc])

            # Bidaf style conv-highway encoder
            ch_emb = conv(ch_emb, self.dc, bias=True, activation=tf.nn.relu,
                          kernel_size=5, name="char_conv", reuse=None)
            qh_emb = conv(qh_emb, self.dc, bias=True, activation=tf.nn.relu,
                          kernel_size=5, name="char_conv", reuse=True)

            ch_emb = tf.reduce_max(ch_emb, axis=1)
            qh_emb = tf.reduce_max(qh_emb, axis=1)

            ch_emb = tf.reshape(ch_emb, [self.N, self.PL, ch_emb.shape[-1]])
            qh_emb = tf.reshape(qh_emb, [self.N, self.QL, qh_emb.shape[-1]])

            with tf.device("/cpu:0"):
                c_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.para1), 1.0 - self.dropout)
                q_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.que1), 1.0 - self.dropout)

            para_emb = tf.concat([c_emb, ch_emb], axis=2)
            que_emb = tf.concat([q_emb, qh_emb], axis=2)

        # add elmo
        embedded_para = tf.concat([para_emb, elmo_para_input], -1)
        embedded_que = tf.concat([que_emb, elmo_que_input], -1)
        print("word embedding:", embedded_para.get_shape().as_list(), embedded_que.get_shape().as_list())

        # input encoder
        with tf.variable_scope("para_encoder"):
            para_rep, _ = bi_cudnn_rnn_encoder('gru', self.d, 1, self.dropout,
                                               embedded_para, self.para_len, self.trainable)
        with tf.variable_scope("para_encoder", reuse=True):
            que_rep, _ = bi_cudnn_rnn_encoder('gru', self.d, 1, self.dropout,
                                              embedded_que, self.que_len, self.trainable)
        print("input encoder:", para_rep.get_shape().as_list(), que_rep.get_shape().as_list())

        # add elmo
        para_rep = tf.concat([para_rep, elmo_para_output], -1)
        que_rep = tf.concat([que_rep, elmo_que_output], -1)
        print("contextual word embedding:", para_rep.get_shape().as_list(), que_rep.get_shape().as_list())

        # bi attention
        with tf.variable_scope("bi_attention"):
            joint_mask = tf.to_float(tf.expand_dims(self.para_mask, 2)) * tf.to_float(tf.expand_dims(self.que_mask, 1))
            para_rep = bidaf_attention(para_rep, que_rep, joint_mask, tri_linear_attention)
            para_rep = tf.nn.dropout(para_rep, 1.0 - self.dropout)
            para_proj = tf.layers.dense(inputs=para_rep, units=self.d * 2, activation=tf.nn.relu,
                                        name="self_attn_input_proj")
        print("bi attention:", para_rep.get_shape().as_list())
        # self attneiton
        with tf.variable_scope("self_attention"):
            with tf.variable_scope("input_proj"):
                self_attn_para_input, _ = bi_cudnn_rnn_encoder('gru', self.d, 1, self.dropout,
                                                               para_proj, self.para_len,
                                                               self.trainable)
            diag_mask = 1.0 - tf.diag(tf.ones((self.PL)))
            context_mask = tf.to_float(tf.expand_dims(self.para_mask, 2)) * tf.to_float(
                    tf.expand_dims(self.para_mask, 1))
            self_attn_para = self_attention(self_attn_para_input, context_mask * diag_mask, tri_linear_attention)
            self_attn_para = tf.nn.dropout(self_attn_para, 1.0 - self.dropout)
            self_attn_para = tf.layers.dense(inputs=self_attn_para, units=self.d * 2, activation=tf.nn.relu,
                                             name="self_attn_output_proj")
            self_attn_para += para_proj
        print("self attention:", self_attn_para.get_shape().as_list())
        # model encoder
        with tf.variable_scope("model_encoder"):
            with tf.variable_scope("start_pointer_proj"):
                start_pointer_input, _ = bi_cudnn_rnn_encoder('gru', self.d, 1, self.dropout,
                                                              self_attn_para, self.para_len, self.trainable)
            start_pointer_input = tf.nn.dropout(start_pointer_input, 1.0 - self.dropout)
            logits1 = tf.layers.dense(inputs=start_pointer_input, units=1, use_bias=False,
                                      name="start_pointer")
            logits1 = mask_logits(tf.reshape(logits1, [self.N, -1]), tf.reshape(self.para_mask, [self.N, -1]))

            with tf.variable_scope("end_pointer_proj"):
                end_pointer_input, _ = bi_cudnn_rnn_encoder('gru', self.d, 1, self.dropout,
                                                            tf.concat([self_attn_para, start_pointer_input], axis=-1),
                                                            self.para_len, self.trainable)
            end_pointer_input = tf.nn.dropout(end_pointer_input, 1.0 - self.dropout)
            logits2 = tf.layers.dense(inputs=end_pointer_input, units=1, use_bias=False,
                                      name="end_pointer")
            logits2 = mask_logits(tf.reshape(logits2, [self.N, -1]), tf.reshape(self.para_mask, [self.N, -1]))

        self.probs1 = tf.nn.softmax(logits1)
        self.probs2 = tf.nn.softmax(logits2)
        # get loss
        losses = tf.nn.softmax_cross_entropy_with_logits(logits=logits1, labels=tf.reshape(self.y1, [self.N, -1]))
        losses2 = tf.nn.softmax_cross_entropy_with_logits(logits=logits2, labels=tf.reshape(self.y2, [self.N, -1]))
        self.batch_loss = losses + losses2
        self.loss = tf.reduce_sum(self.batch_loss * tf.to_float(self.labels)) / (tf.reduce_sum(tf.to_float(self.labels)) + 1e-10)

        # get prediction
        outer = tf.matmul(tf.expand_dims(tf.nn.softmax(logits1), axis=2),
                          tf.expand_dims(tf.nn.softmax(logits2), axis=1))
        outer = tf.matrix_band_part(outer, 0, self.AL)
        bprobs, bindex = tf.nn.top_k(tf.reshape(outer, [-1, self.PL * self.PL]), k=self.config.beam_size)
        self.byp1 = bindex // self.PL
        self.byp2 = bindex % self.PL
        self.bprobs = -tf.log(bprobs)
Example #22
0
    def _init_graph(self):
        with self.graph.as_default():
            # Model.
            u_ids = self.train_features[:, 0]
            i_ids = self.train_features[:, 1]

            # cold sampling
            drop_u_pos = tf.cast(tf.multinomial(
                tf.log([[1 - self.cs_ratio, self.cs_ratio]]),
                tf.shape(u_ids)[0]),
                                 dtype=self.d_type)
            drop_i_pos = tf.cast(tf.multinomial(
                tf.log([[1 - self.cs_ratio, self.cs_ratio]]),
                tf.shape(i_ids)[0]),
                                 dtype=self.d_type)
            drop_u_pos = tf.reshape(drop_u_pos, shape=[-1])
            drop_i_pos = tf.reshape(drop_i_pos, shape=[-1])
            drop_u_pos_zero = tf.zeros(shape=tf.shape(drop_u_pos),
                                       dtype=self.d_type)
            drop_i_pos_zero = tf.zeros(shape=tf.shape(drop_i_pos),
                                       dtype=self.d_type)

            drop_u_pos = tf.cond(self.train_phase, lambda: drop_u_pos,
                                 lambda: drop_u_pos_zero)
            drop_i_pos = tf.cond(self.train_phase, lambda: drop_i_pos,
                                 lambda: drop_i_pos_zero)
            drop_u_pos_v = tf.reshape(drop_u_pos, shape=[-1, 1])
            drop_i_pos_v = tf.reshape(drop_i_pos, shape=[-1, 1])

            # bias
            self.u_bias = tf.nn.embedding_lookup(self.weights['user_bias'],
                                                 u_ids) * (1 - drop_u_pos)
            self.i_bias = tf.nn.embedding_lookup(self.weights['item_bias'],
                                                 i_ids) * (1 - drop_i_pos)
            self.cross_bias = tf.reduce_sum(tf.reduce_sum(
                tf.nn.embedding_lookup(self.weights['cross_bias'],
                                       self.train_features[:, 2:]),
                axis=1),
                                            axis=1)
            self.bias = self.cross_bias + self.weights['global_bias']
            self.bias += self.u_bias + self.i_bias

            # cf part
            cf_u_vectors = tf.nn.embedding_lookup(
                self.weights['uid_embeddings'], u_ids)
            cf_i_vectors = tf.nn.embedding_lookup(
                self.weights['iid_embeddings'], i_ids)

            random_u_vectors = tf.random_normal(tf.shape(cf_u_vectors),
                                                0,
                                                0.01,
                                                dtype=self.d_type)
            random_i_vectors = tf.random_normal(tf.shape(cf_i_vectors),
                                                0,
                                                0.01,
                                                dtype=self.d_type)

            cf_u_vectors = random_u_vectors * drop_u_pos_v + cf_u_vectors * (
                1 - drop_u_pos_v)
            cf_i_vectors = random_i_vectors * drop_i_pos_v + cf_i_vectors * (
                1 - drop_i_pos_v)

            cf_u_vectors = tf.nn.dropout(cf_u_vectors, self.dropout_keep)
            cf_i_vectors = tf.nn.dropout(cf_i_vectors, self.dropout_keep)

            self.cf_prediction = tf.reduce_sum(tf.multiply(
                cf_u_vectors, cf_i_vectors),
                                               axis=1)

            # cb part
            u_fs = self.train_features[:, 2:2 + self.user_feature_num]
            i_fs = self.train_features[:, 2 + self.user_feature_num:]
            uf_vectors = tf.nn.embedding_lookup(
                self.weights['feature_embeddings'], u_fs)
            if_vectors = tf.nn.embedding_lookup(
                self.weights['feature_embeddings'], i_fs)

            summed_u_features_emb = tf.reduce_sum(uf_vectors, axis=1)
            summed_u_features_emb_square = tf.square(summed_u_features_emb)
            squared_u_features_emb = tf.square(uf_vectors)
            squared_sum_u_features_emb = tf.reduce_sum(squared_u_features_emb,
                                                       axis=1)
            u_fm = 0.5 * tf.subtract(summed_u_features_emb_square,
                                     squared_sum_u_features_emb)

            summed_i_features_emb = tf.reduce_sum(if_vectors, axis=1)
            summed_i_features_emb_square = tf.square(summed_i_features_emb)
            squared_i_features_emb = tf.square(if_vectors)
            squared_sum_i_features_emb = tf.reduce_sum(squared_i_features_emb,
                                                       axis=1)
            i_fm = 0.5 * tf.subtract(summed_i_features_emb_square,
                                     squared_sum_i_features_emb)

            uf_layer = tf.reshape(
                uf_vectors, (-1, self.f_vector_size * self.user_feature_num))
            uf_layer = tf.concat([uf_layer, u_fm], axis=1)
            uf_layer = tf.layers.batch_normalization(uf_layer,
                                                     training=self.train_phase,
                                                     name='u_bn_fs')
            uf_layer = tf.nn.dropout(uf_layer, self.dropout_keep)
            if_layer = tf.reshape(
                if_vectors, (-1, self.f_vector_size * self.item_feature_num))
            if_layer = tf.concat([if_layer, i_fm], axis=1)
            if_layer = tf.layers.batch_normalization(if_layer,
                                                     training=self.train_phase,
                                                     name='i_bn_fs')
            if_layer = tf.nn.dropout(if_layer, self.dropout_keep)

            self.lrp_layers_u, self.lrp_layers_i = [uf_layer], [if_layer]
            for i in range(0, len(self.cb_hidden_layers) + 1):
                uf_layer = tf.add(
                    tf.matmul(uf_layer, self.weights['cb_user_layer_%d' % i]),
                    self.weights['cb_user_bias_%d' % i])
                if_layer = tf.add(
                    tf.matmul(if_layer, self.weights['cb_item_layer_%d' % i]),
                    self.weights['cb_item_bias_%d' % i])
                uf_layer = tf.layers.batch_normalization(
                    uf_layer, training=self.train_phase, name='u_bn_%d' % i)
                if_layer = tf.layers.batch_normalization(
                    if_layer, training=self.train_phase, name='i_bn_%d' % i)
                if i < len(self.cb_hidden_layers):
                    uf_layer = tf.nn.relu(uf_layer)
                    uf_layer = tf.nn.dropout(uf_layer, self.dropout_keep)
                    if_layer = tf.nn.relu(if_layer)
                    if_layer = tf.nn.dropout(if_layer, self.dropout_keep)
                    self.lrp_layers_u.append(uf_layer)
                    self.lrp_layers_i.append(if_layer)
            cb_u_vectors, cb_i_vectors = uf_layer, if_layer
            self.cb_prediction = tf.reduce_sum(tf.multiply(
                cb_u_vectors, cb_i_vectors),
                                               axis=1)

            # attention
            ah_cf_u = tf.add(
                tf.matmul(cf_u_vectors, self.weights['attention_weights']),
                self.weights['attention_bias'])
            ah_cf_u = tf.tanh(ah_cf_u)
            # ah_cf_u = tf.nn.relu(ah_cf_u)
            ah_cf_u = tf.nn.dropout(ah_cf_u, self.dropout_keep)

            a_cf_u = tf.reduce_sum(tf.multiply(ah_cf_u,
                                               self.weights['attention_pre']),
                                   axis=1)
            a_cf_u = tf.exp(a_cf_u)
            ah_cb_u = tf.add(
                tf.matmul(cb_u_vectors, self.weights['attention_weights']),
                self.weights['attention_bias'])
            ah_cb_u = tf.tanh(ah_cb_u)
            # ah_cb_u = tf.nn.relu(ah_cb_u)
            ah_cb_u = tf.nn.dropout(ah_cb_u, self.dropout_keep)

            a_cb_u = tf.reduce_sum(tf.multiply(ah_cb_u,
                                               self.weights['attention_pre']),
                                   axis=1)
            a_cb_u = tf.exp(a_cb_u)
            a_sum = a_cf_u + a_cb_u

            self.a_cf_u = tf.reshape(a_cf_u / a_sum, shape=[-1, 1])
            self.a_cb_u = tf.reshape(a_cb_u / a_sum, shape=[-1, 1])

            ah_cf_i = tf.add(
                tf.matmul(cf_i_vectors, self.weights['attention_weights']),
                self.weights['attention_bias'])
            ah_cf_i = tf.tanh(ah_cf_i)
            # ah_cf_i = tf.nn.relu(ah_cf_i)
            ah_cf_i = tf.nn.dropout(ah_cf_i, self.dropout_keep)

            a_cf_i = tf.reduce_sum(tf.multiply(ah_cf_i,
                                               self.weights['attention_pre']),
                                   axis=1)
            a_cf_i = tf.exp(a_cf_i)
            ah_cb_i = tf.add(
                tf.matmul(cb_i_vectors, self.weights['attention_weights']),
                self.weights['attention_bias'])
            ah_cb_i = tf.tanh(ah_cb_i)
            # ah_cb_i = tf.nn.relu(ah_cb_i)
            ah_cb_i = tf.nn.dropout(ah_cb_i, self.dropout_keep)

            a_cb_i = tf.reduce_sum(tf.multiply(ah_cb_i,
                                               self.weights['attention_pre']),
                                   axis=1)
            a_cb_i = tf.exp(a_cb_i)
            a_sum = a_cf_i + a_cb_i

            self.a_cf_i = tf.reshape(a_cf_i / a_sum, shape=[-1, 1])
            self.a_cb_i = tf.reshape(a_cb_i / a_sum, shape=[-1, 1])

            # prediction
            self.u_vector = self.a_cf_u * cf_u_vectors + self.a_cb_u * cb_u_vectors
            self.i_vector = self.a_cf_i * cf_i_vectors + self.a_cb_i * cb_i_vectors
            self.prediction = self.bias + tf.reduce_sum(
                tf.multiply(self.u_vector, self.i_vector), axis=1)
Example #23
0
 def _inc_loss_rho(self, rho, signal, t):
     return - tf.log(1. + self._expectation(rho, t) * signal / self.A)
def binary_crossentropy(t,o):
    return -(t*tf.log(o+eps) + (1.0-t)*tf.log(1.0-o+eps))
Example #25
0
y_data = xy[:, [-1]]

print(x_data.shape, y_data.shape)

# placeholders for a tensor that will be always fed.
X = tf.placeholder(tf.float32, shape=[None, 8])
Y = tf.placeholder(tf.float32, shape=[None, 1])

W = tf.Variable(tf.random_normal([8, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')

# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)

# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) +
                       (1 - Y) * tf.log(1 - hypothesis))

train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))

# Launch graph
with tf.Session() as sess:
    # Initialize TensorFlow variables
    sess.run(tf.global_variables_initializer())

    for step in range(10001):
c = apply_depthwise_conv(X,kernel_size,num_channels,depth)
p = apply_max_pool(c,20,2)
c = apply_depthwise_conv(p,6,depth*num_channels,depth//10)

shape = c.get_shape().as_list()
c_flat = tf.reshape(c, [-1, shape[1] * shape[2] * shape[3]])

f_weights_l1 = weight_variable([shape[1] * shape[2] * depth * num_channels * (depth//10), num_hidden])
f_biases_l1 = bias_variable([num_hidden])
f = tf.nn.tanh(tf.add(tf.matmul(c_flat, f_weights_l1),f_biases_l1))

out_weights = weight_variable([num_hidden, num_labels])
out_biases = bias_variable([num_labels])
y_ = tf.nn.softmax(tf.matmul(f, out_weights) + out_biases)

loss = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(loss)

correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

cost_history = np.empty(shape=[1], dtype=float)

# 开始训练
with tf.Session() as session:
    tf.initialize_all_variables().run()
    # 开始迭代
    for epoch in range(training_epochs):
        for b in range(total_batchs):
            offset = (b * batch_size) % (train_y.shape[0] - batch_size)
            batch_x = train_x[offset:(offset + batch_size), :, :, :]
Example #27
0
def tf_psnr(im1, im2):
    # assert pixel value range is 0-1
    #mse = tf.losses.mean_squared_error(labels=im2 * 255.0, predictions=im1 * 255.0)
    mse = tf.compat.v1.losses.mean_squared_error(labels=im2 * 255.0,
                                                 predictions=im1 * 255.0)
    return 10.0 * (tf.log(255.0**2 / mse) / tf.log(10.0))
Example #28
0
# padding = 'SAME' 时,输出并不一定和原图size一致
# 但会保证覆盖原图所有像素,不会舍弃边上的某些元素
## fully connection 1 layer ##
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = biase_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
## fully connection 2 layer ##
W_fc2 = weight_variable([1024, 10])
b_fc2 = biase_variable([10])
prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

# 2 定义损失函数与反向传播方法
cross_entropy = tf.reduce_mean(-tf.reduce_sum(
    y * tf.log(prediction), reduction_indices=[1]))  # sum(- y * log(y0))/n 交叉熵
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

#3 生成会话,训练
with tf.Session() as sess:
    # 变量初始化
    init = tf.global_variables_initializer()
    sess.run(init)
    # 训练
    for i in range(5):
        # 提取 batch
        batch_x, batch_y = mnist.train.next_batch(100)

        sess.run(train_step,
                 feed_dict={
                     x: batch_x,
Example #29
0
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)

# print (mnist.train.next_batch(1))

x = tf.placeholder(tf.float32, [None, 784])
w = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, w) + b)

y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(
    -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for xx in range(1000):
    bx, by = mnist.train.next_batch(500)
    # bx=mnist.train.images
    # by=mnist.train.labels
    sess.run(train_step, feed_dict={x: bx, y_: by})

correct_prediction = tf.equal(tf.argmax(y_, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(
Example #30
0
    def create_model(self):
        g = tf.Graph()
        with g.as_default():
            # Define model variables
            var_linear = tf.get_variable('linear',
                    [self.feature_dim, 1],
                    initializer=tf.random_uniform_initializer(
                        -self.args.init_mean, self.args.init_mean))

            var_emb_factors = tf.get_variable('emb_factors',
                    [self.feature_dim, self.args.num_dims],
                    initializer=tf.random_uniform_initializer(
                        -self.args.init_mean, self.args.init_mean))

            # Sparse placeholders
            pl_user_list = tf.placeholder(tf.int64, shape=[None], name='pos_list')

            pl_pos_indices = tf.placeholder(tf.int64, shape=[None, 2], name='pos_indices')
            pl_pos_values = tf.placeholder(tf.float32, shape=[None], name='pos_values')
            pl_pos_shape = tf.placeholder(tf.int64, shape=[2], name='pos_shape')

            pl_neg_indices = tf.placeholder(tf.int64, shape=[None, 2], name='neg_indices')
            pl_neg_values = tf.placeholder(tf.float32, shape=[None], name='neg_values')
            pl_neg_shape = tf.placeholder(tf.int64, shape=[2], name='neg_shape')

            placeholders = {
                    'pl_user_list': pl_user_list,
                    'pl_pos_indices': pl_pos_indices,
                    'pl_pos_values': pl_pos_values,
                    'pl_pos_shape': pl_pos_shape,
                    'pl_neg_indices': pl_neg_indices,
                    'pl_neg_values': pl_neg_values,
                    'pl_neg_shape': pl_neg_shape
            }

            # Input positive features, shape = (batch_size * feature_dim)
            sparse_pos_feats = tf.SparseTensor(pl_pos_indices, pl_pos_values, pl_pos_shape)

            # Input negative features, shape = (batch_size * feature_dim)
            sparse_neg_feats = tf.SparseTensor(pl_neg_indices, pl_neg_values, pl_neg_shape)

            pos_preds, neg_preds = self.get_preds(var_linear, var_emb_factors,
                    sparse_pos_feats, sparse_neg_feats)

            l2_reg = tf.add_n([
                self.args.linear_reg * tf.reduce_sum(tf.square(var_linear)),
                self.args.emb_reg    * tf.reduce_sum(tf.square(var_emb_factors)),
            ])

            # BPR training op (add 1e-10 to help numerical stability)
            bprloss_op = tf.reduce_sum(tf.log(1e-10 + tf.sigmoid(pos_preds - neg_preds))) - l2_reg
            bprloss_op = -bprloss_op

            global_step = tf.Variable(0, trainable=False)
            learning_rate = tf.train.exponential_decay(self.args.starting_lr,
                global_step, self.args.lr_decay_freq,
                self.args.lr_decay_factor, staircase=False)
            optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
            train_op = optimizer.minimize(bprloss_op, global_step=global_step)

            # AUC
            binary_ranks = tf.to_float((pos_preds - neg_preds) > 0)
            auc_per_user = tf.segment_mean(binary_ranks, pl_user_list)
            auc_op = tf.divide(tf.reduce_sum(auc_per_user),
                    tf.to_float(tf.size(tf.unique(pl_user_list)[0])))

        self.var_linear = var_linear
        self.var_emb_factors = var_emb_factors
        return (g, bprloss_op, optimizer, train_op, auc_op, l2_reg, placeholders)
learning_rate = 0.01
training_epochs = 15
batch_size = 100
display_step = 1
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

# tf Graph Input
x = tf.placeholder("float", [None, 784])  # mnist data image of shape 28*28=784 (흑백이므로 color를 위한 차원은 없음)
y = tf.placeholder("float", [None, 10])  # 0-9 digits recognition => 10 classes

# set model weight
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

activation = tf.nn.softmax(tf.matmul(x, W) + b)
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(activation), reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# 4) 학습 실행 (Launch the graph)
with tf.Session() as sess:
    sess.run(init)
    for epoch in range(training_epochs):
        avg_cost = 0.
        # mnist.train.num_examples = 55000
        # batch_size = 100
        # total_batch = 550 ==> 1번의 training_epoch에 550개의 이미지를 학습한다
        total_batch = int(mnist.train.num_examples / batch_size)
        for i in range(total_batch):
Example #32
0
File: GAN.py Project: BBuf/GAN-Code
    return G_prob


# 判别网络
def discriminator(x):
    D_h1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1)
    D_logit = tf.matmul(D_h1, D_W2) + D_b2
    D_prob = tf.nn.sigmoid(D_logit)
    return D_prob, D_logit


G_sample = generator(Z)
D_real, D_logit_real = discriminator(X)
D_fake, D_logit_fake = discriminator(G_sample)
# 损失函数
D_loss = -tf.reduce_mean(tf.log(D_real) + tf.log(1. - D_fake))
G_loss = -tf.reduce_mean(tf.log(D_fake))
# 更新D(x)的参数
D_solver = tf.train.AdamOptimizer().minimize(D_loss, var_list=theta_D)
# 更新G(Z)的参数
G_solver = tf.train.AdamOptimizer().minimize(G_loss, var_list=theta_G)


def sample_Z(m, n):
    return np.random.uniform(-1., 1., size=[m, n])


batch_size = 128
Z_dim = 100

sess = tf.Session()
w1 = tf.Variable(tf.random_normal(shape=[10, 6]))
b1 = tf.Variable(tf.random_normal(shape=[6]))

w2 = tf.Variable(tf.random_normal(shape=[6, 2]))
b2 = tf.Variable(tf.random_normal(shape=[2]))

interval = 50
epoch = 4000
LR = .005

Y1 = tf.nn.relu(tf.add(tf.matmul(X, w1), b1))
Y2 = tf.nn.softmax(tf.add(tf.matmul(Y1, w2), b2))

with tf.name_scope("loss"):
    loss = -tf.reduce_sum(y_ * tf.log(Y2))
    optimizer = tf.train.AdamOptimizer(LR).minimize(
        loss)  # Add scalar summary for cost tensor
    tf.summary.scalar("loss", loss)

with tf.name_scope("accuracy"):
    correct_pred = tf.equal(tf.argmax(y_, 1),
                            tf.argmax(Y2, 1))  # Count correct predictions
    acc_op = tf.reduce_mean(tf.cast(
        correct_pred, "float"))  # Cast boolean to float to average
    # Add scalar summary for accuracy tensor
    tf.summary.scalar("accuracy", acc_op)

with tf.Session() as sess:
    #tensorboard config ... use: tensorboard --logdir=/logs/churn/
    fw = tf.summary.FileWriter("churn_prediction/logs/churn", sess.graph)
    def __init__(self,
                 num_emb,
                 batch_size,
                 emb_dim,
                 hidden_dim,
                 sequence_length,
                 start_token,
                 good_id,
                 pos,
                 learning_rate=0.01,
                 reward_gamma=0.95):
        self.num_emb = num_emb
        self.batch_size = batch_size
        self.emb_dim = emb_dim
        self.hidden_dim = hidden_dim
        self.sequence_length = sequence_length
        self.start_token = tf.constant([start_token] * self.batch_size,
                                       dtype=tf.int32)
        self.learning_rate = tf.Variable(float(learning_rate), trainable=False)
        self.reward_gamma = reward_gamma
        self.g_params = []
        self.d_params = []
        self.good_id = tf.constant(good_id * self.batch_size, dtype=tf.int32)
        self.temperature = 2
        self.grad_clip = 5.0
        self.pos = pos

        self.expected_reward = tf.Variable(tf.zeros([self.sequence_length]))

        with tf.variable_scope('generator'):
            self.g_embeddings = tf.Variable(
                self.init_matrix_embedding([self.num_emb, self.emb_dim],
                                           self.pos))
            self.g_params.append(self.g_embeddings)
            self.g_recurrent_unit = self.create_recurrent_unit(
                self.g_params)  # maps h_tm1 to h_t for generator
            self.g_output_unit = self.create_output_unit(
                self.g_params)  # maps h_t to o_t (output token logits)

        # placeholder definition
        self.x = tf.placeholder(tf.int32,
                                shape=[self.batch_size, self.sequence_length])
        # sequence of indices of true data, not including start token

        self.rewards = tf.placeholder(
            tf.float32, shape=[self.batch_size, self.sequence_length])
        # get from rollout policy and discriminator

        # processed for batch
        with tf.device("/cpu:0"):
            inputs = tf.split(
                1, self.sequence_length,
                tf.nn.embedding_lookup(self.g_embeddings, self.x))
            self.processed_x = tf.pack([
                tf.squeeze(input_, [1]) for input_ in inputs
            ])  # seq_length x batch_size x emb_dim
        with tf.device("/cpu:0"):
            inputs = tf.split(1, self.sequence_length, self.x)
            self.processed_token_x = tf.pack(
                [tf.squeeze(input_, [1]) for input_ in inputs])

        self.h0 = tf.zeros([self.batch_size, self.hidden_dim])
        self.h0 = tf.pack([self.h0, self.h0])

        gen_o = tensor_array_ops.TensorArray(dtype=tf.float32,
                                             size=self.sequence_length,
                                             dynamic_size=False,
                                             infer_shape=True)
        gen_x = tensor_array_ops.TensorArray(dtype=tf.int32,
                                             size=self.sequence_length,
                                             dynamic_size=False,
                                             infer_shape=True)

        ta_emb_x2 = tensor_array_ops.TensorArray(dtype=tf.int32,
                                                 size=self.sequence_length)
        ta_emb_x2 = ta_emb_x2.unpack(self.processed_token_x)

        x_t = tf.nn.embedding_lookup(self.g_embeddings, self.start_token)
        h_t1 = self.g_recurrent_unit(x_t, self.h0)  # hidden_memory_tuple
        o_t = self.g_output_unit(h_t1)  # batch x vocab , logits not prob
        log_prob = tf.divide(tf.log(tf.nn.softmax(o_t)), self.temperature)
        next_token = tf.cast(tf.reshape(ta_emb_x2.read(0), [self.batch_size]),
                             tf.int32)
        #next_token = tf.cast(tf.reshape(tf.multinomial(log_prob, 1), [self.batch_size]), tf.int32)
        x_tp1 = tf.nn.embedding_lookup(self.g_embeddings,
                                       next_token)  # batch x emb_dim
        gen_o = gen_o.write(
            tf.constant(0, dtype=tf.int32),
            tf.reduce_sum(
                tf.mul(tf.one_hot(next_token, self.num_emb, 1.0, 0.0),
                       tf.nn.softmax(o_t)), 1))  # [batch_size] , prob
        gen_x = gen_x.write(tf.constant(0, dtype=tf.int32),
                            next_token)  # indices, batch_size

        #random sampling:
        '''
        x_t = tf.nn.embedding_lookup(self.g_embeddings,self.start_token)
        h_t1 = self.g_recurrent_unit(x_t, self.h0)  # hidden_memory_tuple
        o_t = self.g_output_unit(h_t1)  # batch x vocab , logits not prob
        next_token = tf.multinomial(tf.nn.softmax(o_t),1)
        next_token = tf.cast(tf.reshape(tf.multinomial(tf.nn.softmax(o_t),1),[self.batch_size]),tf.int32)    
        x_tp1 = tf.nn.embedding_lookup(self.g_embeddings, next_token)  # batch x emb_dim
        gen_o = gen_o.write(tf.constant(0,dtype=tf.int32), tf.reduce_sum(tf.mul(tf.one_hot(next_token, self.num_emb, 1.0, 0.0),tf.nn.softmax(o_t)), 1))  # [batch_size] , prob
        gen_x = gen_x.write(tf.constant(0,dtype=tf.int32), next_token)  # indices, batch_size
        '''
        def _g_recurrence(i, x_t, h_tm1, gen_o, gen_x):
            h_t = self.g_recurrent_unit(x_t, h_tm1)  # hidden_memory_tuple
            o_t = self.g_output_unit(h_t)  # batch x vocab , logits not prob
            print('now here')
            next_token = tf.argmax(tf.nn.softmax(o_t), 1)
            next_token = tf.cast(
                tf.reshape(tf.argmax(tf.nn.softmax(o_t), 1),
                           [self.batch_size]), tf.int32)
            x_tp1 = tf.nn.embedding_lookup(self.g_embeddings, next_token)
            gen_o = gen_o.write(
                i,
                tf.reduce_sum(
                    tf.mul(tf.one_hot(next_token, self.num_emb, 1.0, 0.0),
                           tf.nn.softmax(o_t)), 1))  # [batch_size] , prob
            gen_x = gen_x.write(i, next_token)  # indices, batch_size
            return i + 1, x_tp1, h_t, gen_o, gen_x

        _, _, _, self.gen_o, self.gen_x = tf.while_loop(
            cond=lambda i, _1, _2, _3, _4: i < self.sequence_length,
            body=_g_recurrence,
            loop_vars=(tf.constant(1,
                                   dtype=tf.int32), x_tp1, h_t1, gen_o, gen_x))

        self.gen_x = self.gen_x.pack()  # seq_length x batch_size
        self.gen_x = tf.transpose(self.gen_x,
                                  perm=[1, 0])  # batch_size x seq_length

        self.h0 = tf.zeros([self.batch_size, self.hidden_dim])
        self.h0 = tf.pack([self.h0, self.h0])

        gen_o_argmax = tensor_array_ops.TensorArray(dtype=tf.float32,
                                                    size=self.sequence_length,
                                                    dynamic_size=False,
                                                    infer_shape=True)
        gen_x_argmax = tensor_array_ops.TensorArray(dtype=tf.int32,
                                                    size=self.sequence_length,
                                                    dynamic_size=False,
                                                    infer_shape=True)

        def _g_recurrence_argmax(i, x_t, h_tm1, gen_o, gen_x):
            h_t = self.g_recurrent_unit(x_t, h_tm1)  # hidden_memory_tuple
            o_t = self.g_output_unit(h_t)  # batch x vocab , logits not prob

            next_token = tf.argmax(tf.nn.softmax(o_t), 1)
            next_token = tf.cast(
                tf.reshape(tf.argmax(tf.nn.softmax(o_t), 1),
                           [self.batch_size]), tf.int32)

            x_tp1 = tf.nn.embedding_lookup(self.g_embeddings,
                                           next_token)  # batch x emb_dim
            print(x_tp1)
            gen_o = gen_o.write(
                i,
                tf.reduce_sum(
                    tf.mul(tf.one_hot(next_token, self.num_emb, 1.0, 0.0),
                           tf.nn.softmax(o_t)), 1))  # [batch_size] , prob
            gen_x = gen_x.write(i, next_token)  # indices, batch_size
            return i + 1, x_tp1, h_t, gen_o, gen_x

        _, _, _, self.gen_o_argmax, self.gen_x_argmax = tf.while_loop(
            cond=lambda i, _1, _2, _3, _4: i < self.sequence_length,
            body=_g_recurrence_argmax,
            loop_vars=(tf.constant(0, dtype=tf.int32),
                       tf.nn.embedding_lookup(self.g_embeddings,
                                              self.start_token), self.h0,
                       gen_o_argmax, gen_x_argmax))

        self.gen_x_argmax = self.gen_x_argmax.pack()  # seq_length x batch_size
        self.gen_x_argmax = tf.transpose(self.gen_x_argmax,
                                         perm=[1,
                                               0])  # batch_size x seq_length
        ###################################################################
        # wgan pretraining for generator
        g_predictions_wgan = tensor_array_ops.TensorArray(
            dtype=tf.float32,
            size=self.sequence_length,
            dynamic_size=False,
            infer_shape=True)

        def _wgantrain_recurrence(i, x_t, h_tm1, g_predictions_wgan):
            h_t = self.g_recurrent_unit(x_t, h_tm1)
            o_t = self.g_output_unit(h_t)
            g_predictions_wgan = g_predictions_wgan.write(
                i, tf.nn.softmax(o_t))  # batch x vocab_size
            next_token = tf.argmax(tf.nn.softmax(o_t), 1)
            x_tp1 = tf.nn.embedding_lookup(self.g_embeddings, next_token)
            #x_tp1 = ta_emb_x.read(i)
            return i + 1, x_tp1, h_t, g_predictions_wgan

        _, _, _, self.g_predictions_wgan = tf.while_loop(
            cond=lambda i, _1, _2, _3: i < self.sequence_length,
            body=_wgantrain_recurrence,
            loop_vars=(tf.constant(0, dtype=tf.int32),
                       tf.nn.embedding_lookup(self.g_embeddings,
                                              self.start_token), self.h0,
                       g_predictions_wgan))

        self.g_predictions_wgan = tf.transpose(
            self.g_predictions_wgan.pack(),
            perm=[1, 0, 2])  # batch_size x seq_length x vocab_size

        ###################################################################
        # supervised pretraining for generator
        g_predictions = tensor_array_ops.TensorArray(dtype=tf.float32,
                                                     size=self.sequence_length,
                                                     dynamic_size=False,
                                                     infer_shape=True)

        ta_emb_x = tensor_array_ops.TensorArray(dtype=tf.float32,
                                                size=self.sequence_length)
        ta_emb_x = ta_emb_x.unpack(self.processed_x)

        def _pretrain_recurrence(i, x_t, h_tm1, g_predictions):
            h_t = self.g_recurrent_unit(x_t, h_tm1)
            o_t = self.g_output_unit(h_t)
            g_predictions = g_predictions.write(
                i, tf.nn.softmax(o_t))  # batch x vocab_size
            x_tp1 = ta_emb_x.read(i)
            return i + 1, x_tp1, h_t, g_predictions

        _, _, _, self.g_predictions = tf.while_loop(
            cond=lambda i, _1, _2, _3: i < self.sequence_length,
            body=_pretrain_recurrence,
            loop_vars=(tf.constant(0, dtype=tf.int32),
                       tf.nn.embedding_lookup(self.g_embeddings,
                                              self.start_token), self.h0,
                       g_predictions))

        self.g_predictions = tf.transpose(
            self.g_predictions.pack(),
            perm=[1, 0, 2])  # batch_size x seq_length x vocab_size

        # pretraining loss
        self.pretrain_loss = -tf.reduce_sum(
            tf.one_hot(tf.to_int32(tf.reshape(
                self.x, [-1])), self.num_emb, 1.0, 0.0) * tf.log(
                    tf.clip_by_value(
                        tf.reshape(self.g_predictions, [-1, self.num_emb]),
                        1e-20, 1.0))) / (self.sequence_length *
                                         self.batch_size)

        # training updates
        pretrain_opt = self.g_optimizer(self.learning_rate)

        self.pretrain_grad, _ = tf.clip_by_global_norm(
            tf.gradients(self.pretrain_loss, self.g_params), self.grad_clip)
        self.pretrain_updates = pretrain_opt.apply_gradients(
            zip(self.pretrain_grad, self.g_params))

        #######################################################################################################
        #  Unsupervised Training
        #######################################################################################################
        self.g_loss = -tf.reduce_sum(
            tf.reduce_sum(
                tf.one_hot(tf.to_int32(tf.reshape(
                    self.x, [-1])), self.num_emb, 1.0, 0.0) * tf.log(
                        tf.clip_by_value(
                            tf.reshape(self.g_predictions, [-1, self.num_emb]),
                            1e-20, 1.0)), 1) * tf.reshape(self.rewards, [-1]))

        g_opt = self.g_optimizer(self.learning_rate)

        self.g_grad, _ = tf.clip_by_global_norm(
            tf.gradients(self.g_loss, self.g_params), self.grad_clip)
        self.g_updates = g_opt.apply_gradients(zip(self.g_grad, self.g_params))