Exemplo n.º 1
0
def killRegions(anchors, image_attr, axis=-1):
    """ Prune the anchors so that only those entirely within the image remain

    This function is the RPN-training analog of clipRegions, just more murderous

    Output:
        The anchors that survive the slaughter, along with their indices
    """

    with tf.device("/cpu:0"):
        # Assumes input of shape (numBaseAnchors, feature_h, feature_w, 4)
        # Or, was previously as above but then got flattened to (-1,4)

        anchors = tf.reshape(anchors, [-1, 4], name="flattened_anchors")
        x1, y1, x2, y2 = tf.unstack(anchors, num=4, axis=axis)

        zero = tf.constant([0.])

        max_x = [tf.subtract(image_attr[1] * image_attr[2], tf.constant([1.]),
            name="murder_img_w")]
        max_y = [tf.subtract(image_attr[0] * image_attr[2], tf.constant([1.]),
            name="murder_img_h")]

        x1_valid = x1 >= zero
        x2_valid = x2 <= max_x
        y1_valid = y1 >= zero
        y2_valid = y2 <= max_y

        anchor_valid = x1_valid and x2_valid and y1_valid and y2_valid
        valid_indices = tf.where(anchor_valid, name="surviving_indices")
    return tf.gather_nd(anchors, valid_indices, name="surviving_anchors"), valid_indices
Exemplo n.º 2
0
 def addLayer(self, n, activation_function = 'tanh', include_bias = False, sd = 0.35, dropout = 0, normalization = None, weights = None):
     """
     
     :Description:
         Adds a layer to the network, including a weight tensor and an activation tensor.
         
     :Input parameters:
         activation_function:    type of activation function to be applied to each unit (string)
         include_bias:           if true, a column of ones will be added to the weights (boolean)
         sd:                     standard deviation of the zero-mean gaussian from which the weights will be drawn (float)
         dropout:                the chance with which each weight will be set to zero for a given training step (float)
         normalization:          the type of normalization imposed on the layer activations. Can be
                                     a) 'softmax' for softmax normalization
                                     b) 'Shift' for de-meaning
                                     c) 'ShiftScale' for de-meaning and standard deviation normalization
         weights:                if provided, will be used as weights of layer instead of drawing from gaussian (tensor)
         
     """
     
     """ initialize weights and use them to calculate layer activations """
     
     if weights: # if weights are provided, use those
         
         weights = tf.mul(tf.ones(weights.shape),weights)
         activations = tf.matmul(self.data, weights) if not self.weights else tf.matmul(self.Activations[-1], weights)
     
     elif not self.Weights: # else if first layer 
         
         weights = tf.Variable(tf.random_normal([self.data.get_shape()[1].value, n], stddev = sd))      
         weights = tf.concat(1,[weights,tf.ones([weights.get_shape()[0],1])]) if include_bias else weights
         activations = tf.matmul(self.data, weights)
         
     else: # for every other layer
         
         weights = tf.Variable(tf.random_normal([self.Activations[-1].get_shape()[-1].value, n], stddev = sd))
         weights = tf.concat(1,[weights,tf.ones([weights.get_shape()[0],1])]) if include_bias else weights
         activations = tf.matmul(self.Activations[-1], weights)
     
     self.Weights.append(weights)
     self.Activations.append(self.applyActivation(activations, activation_function)) # apply activation function on raw activations
     
     """ add dropout and/or normalization """
     
     if dropout:
         
         self.Activations.append(tf.nn.dropout(self.Activations[-1], dropout))
         
     if normalization == 'softmax': # for softmax normalization
         
         self.Activations.append(tf.nn.softmax(self.Activations[-1]))
         
     elif normalization == 'Shift': # for de-meaning
         
         self.Activations[-1] = tf.subtract(self.Activations[-1],tf.reduce_mean(self.Activations[-1]))
         
     elif normalization == 'ShiftScale': # for de-meaning & and rescaling by variance
         
         mu = tf.reduce_mean(self.Activations[-1])
         diff = tf.subtract(self.Activations[-1],mu)
         self.Activations[-1] = tf.div(diff,tf.reduce_sum(tf.mul(diff,diff)))            
Exemplo n.º 3
0
def cosineface_losses(embedding, labels, out_num, w_init=None, s=30., m=0.4):
    '''
    :param embedding: the input embedding vectors
    :param labels:  the input labels, the shape should be eg: (batch_size, 1)
    :param s: scalar value, default is 30
    :param out_num: output class num
    :param m: the margin value, default is 0.4
    :return: the final cacualted output, this output is send into the tf.nn.softmax directly
    '''
    with tf.variable_scope('cosineface_loss'):
        # inputs and weights norm
        embedding_norm = tf.norm(embedding, axis=1, keep_dims=True)
        embedding = tf.div(embedding, embedding_norm, name='norm_embedding')
        weights = tf.get_variable(name='embedding_weights', shape=(embedding.get_shape().as_list()[-1], out_num),
                                  initializer=w_init, dtype=tf.float32)
        weights_norm = tf.norm(weights, axis=0, keep_dims=True)
        weights = tf.div(weights, weights_norm, name='norm_weights')
        # cos_theta - m
        cos_t = tf.matmul(embedding, weights, name='cos_t')
        cos_t_m = tf.subtract(cos_t, m, name='cos_t_m')

        mask = tf.one_hot(labels, depth=out_num, name='one_hot_mask')
        inv_mask = tf.subtract(1., mask, name='inverse_mask')

        output = tf.add(s * tf.multiply(cos_t, inv_mask), s * tf.multiply(cos_t_m, mask), name='cosineface_loss_output')
    return output
Exemplo n.º 4
0
    def __build(self):
        self.__init_global_epoch()
        self.__init_global_step()
        self.__init_input()

        with tf.name_scope('Preprocessing'):
            red, green, blue = tf.split(self.X, num_or_size_splits=3, axis=3)
            preprocessed_input = tf.concat([
                tf.subtract(blue, ShuffleNet.MEAN[0]) * ShuffleNet.NORMALIZER,
                tf.subtract(green, ShuffleNet.MEAN[1]) * ShuffleNet.NORMALIZER,
                tf.subtract(red, ShuffleNet.MEAN[2]) * ShuffleNet.NORMALIZER,
            ], 3)
        x_padded = tf.pad(preprocessed_input, [[0, 0], [1, 1], [1, 1], [0, 0]], "CONSTANT")
        conv1 = conv2d('conv1', x=x_padded, w=None, num_filters=self.output_channels['conv1'], kernel_size=(3, 3),
                       stride=(2, 2), l2_strength=self.args.l2_strength, bias=self.args.bias,
                       batchnorm_enabled=self.args.batchnorm_enabled, is_training=self.is_training,
                       activation=tf.nn.relu, padding='VALID')
        padded = tf.pad(conv1, [[0, 0], [0, 1], [0, 1], [0, 0]], "CONSTANT")
        max_pool = max_pool_2d(padded, size=(3, 3), stride=(2, 2), name='max_pool')
        stage2 = self.__stage(max_pool, stage=2, repeat=3)
        stage3 = self.__stage(stage2, stage=3, repeat=7)
        stage4 = self.__stage(stage3, stage=4, repeat=3)
        global_pool = avg_pool_2d(stage4, size=(7, 7), stride=(1, 1), name='global_pool', padding='VALID')

        logits_unflattened = conv2d('fc', global_pool, w=None, num_filters=self.args.num_classes,
                                    kernel_size=(1, 1),
                                    l2_strength=self.args.l2_strength,
                                    bias=self.args.bias,
                                    is_training=self.is_training)
        self.logits = flatten(logits_unflattened)

        self.__init_output()
Exemplo n.º 5
0
def r2_op(predictions, targets):
    """ r2_op.

    An op that calculates the standard error.

    Examples:
        ```python
        input_data = placeholder(shape=[None, 784])
        y_pred = my_network(input_data) # Apply some ops
        y_true = placeholder(shape=[None, 10]) # Labels
        stderr_op = r2_op(y_pred, y_true)

        # Calculate standard error by feeding data X and labels Y
        std_error = sess.run(stderr_op, feed_dict={input_data: X, y_true: Y})
        ```

    Arguments:
        predictions: `Tensor`.
        targets: `Tensor`.

    Returns:
        `Float`. The standard error.

    """
    with tf.name_scope('StandardError'):
        a = tf.reduce_sum(tf.square(tf.subtract(targets, predictions)))
        b = tf.reduce_sum(tf.square(tf.subtract(targets, tf.reduce_mean(targets))))
        return tf.subtract(1.0, tf.divide(a, b))
    def __init__(
        self, sequence_length, vocab_size, embedding_size, hidden_units, l2_reg_lambda, batch_size, trainableEmbeddings):

        # Placeholders for input, output and dropout
        self.input_x1 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x1")
        self.input_x2 = tf.placeholder(tf.int32, [None, sequence_length], name="input_x2")
        self.input_y = tf.placeholder(tf.float32, [None], name="input_y")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")

        # Keeping track of l2 regularization loss (optional)
        l2_loss = tf.constant(0.0, name="l2_loss")
          
        # Embedding layer
        with tf.name_scope("embedding"):
            self.W = tf.Variable(
                tf.constant(0.0, shape=[vocab_size, embedding_size]),
                trainable=trainableEmbeddings,name="W")
            self.embedded_words1 = tf.nn.embedding_lookup(self.W, self.input_x1)
            self.embedded_words2 = tf.nn.embedding_lookup(self.W, self.input_x2)
        print self.embedded_words1
        # Create a convolution + maxpool layer for each filter size
        with tf.name_scope("output"):
            self.out1=self.stackedRNN(self.embedded_words1, self.dropout_keep_prob, "side1", embedding_size, sequence_length, hidden_units)
            self.out2=self.stackedRNN(self.embedded_words2, self.dropout_keep_prob, "side2", embedding_size, sequence_length, hidden_units)
            self.distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(self.out1,self.out2)),1,keep_dims=True))
            self.distance = tf.div(self.distance, tf.add(tf.sqrt(tf.reduce_sum(tf.square(self.out1),1,keep_dims=True)),tf.sqrt(tf.reduce_sum(tf.square(self.out2),1,keep_dims=True))))
            self.distance = tf.reshape(self.distance, [-1], name="distance")
        with tf.name_scope("loss"):
            self.loss = self.contrastive_loss(self.input_y,self.distance, batch_size)
        #### Accuracy computation is outside of this class.
        with tf.name_scope("accuracy"):
            self.temp_sim = tf.subtract(tf.ones_like(self.distance),tf.rint(self.distance), name="temp_sim") #auto threshold 0.5
            correct_predictions = tf.equal(self.temp_sim, self.input_y)
            self.accuracy=tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
Exemplo n.º 7
0
def logits_to_log_prob(logits):
  """Computes log probabilities using numerically stable trick.

  This uses two numerical stability tricks:
  1) softmax(x) = softmax(x - c) where c is a constant applied to all
  arguments. If we set c = max(x) then the softmax is more numerically
  stable.
  2) log softmax(x) is not numerically stable, but we can stabilize it
  by using the identity log softmax(x) = x - log sum exp(x)

  Args:
    logits: Tensor of arbitrary shape whose last dimension contains logits.

  Returns:
    A tensor of the same shape as the input, but with corresponding log
    probabilities.
  """

  with tf.variable_scope('log_probabilities'):
    reduction_indices = len(logits.shape.as_list()) - 1
    max_logits = tf.reduce_max(
        logits, reduction_indices=reduction_indices, keep_dims=True)
    safe_logits = tf.subtract(logits, max_logits)
    sum_exp = tf.reduce_sum(
        tf.exp(safe_logits),
        reduction_indices=reduction_indices,
        keep_dims=True)
    log_probs = tf.subtract(safe_logits, tf.log(sum_exp))
  return log_probs
Exemplo n.º 8
0
def tf_image_processing(tf_images, basenet, crop_size, distort=False, hp_filter=False):
    if len(tf_images.shape) == 3:
        tf_images = tf.expand_dims(tf_images, -1)
    if basenet == 'sketchanet':
        mean_value = 250.42
        tf_images = tf.subtract(tf_images, mean_value)
        if distort:
            print("Distorting photos")
            FLAGS.crop_size = crop_size
            FLAGS.dist_chn_size = 1
            tf_images = data_augmentation(tf_images)
        else:
            tf_images = tf.image.resize_images(tf_images, (crop_size, crop_size))
    elif basenet in ['inceptionv1', 'inceptionv3', 'gen_cnn']:
        tf_images = tf.divide(tf_images, 255.0)
        tf_images = tf.subtract(tf_images, 0.5)
        tf_images = tf.multiply(tf_images, 2.0)
        if int(tf_images.shape[-1]) != 3:
            tf_images = tf.concat([tf_images, tf_images, tf_images], axis=-1)
        if distort:
            print("Distorting photos")
            FLAGS.crop_size = crop_size
            FLAGS.dist_chn_size = 3
            tf_images = data_augmentation(tf_images)
            # Display the training images in the visualizer.
            # tf.image_summary('input_images', input_images)
        else:
            tf_images = tf.image.resize_images(tf_images, (crop_size, crop_size))

    if hp_filter:
        tf_images = tf_high_pass_filter(tf_images)

    return tf_images
    def attention_mechanism_parallel(self,c_full,m,q,i):
        """ parallel implemtation of gate function given a list of candidate sentence, a query, and previous memory.
        Input:
           c_full: candidate fact. shape:[batch_size,story_length,hidden_size]
           m: previous memory. shape:[batch_size,hidden_size]
           q: question. shape:[batch_size,hidden_size]
        Output: a scalar score (in batch). shape:[batch_size,story_length]
        """
        q=tf.expand_dims(q,axis=1) #[batch_size,1,hidden_size]
        m=tf.expand_dims(m,axis=1) #[batch_size,1,hidden_size]

        # 1.define a large feature vector that captures a variety of similarities between input,memory and question vector: z(c,m,q)
        c_q_elementwise=tf.multiply(c_full,q)          #[batch_size,story_length,hidden_size]
        c_m_elementwise=tf.multiply(c_full,m)          #[batch_size,story_length,hidden_size]
        c_q_minus=tf.abs(tf.subtract(c_full,q))        #[batch_size,story_length,hidden_size]
        c_m_minus=tf.abs(tf.subtract(c_full,m))        #[batch_size,story_length,hidden_size]
        # c_transpose Wq
        c_w_q=self.x1Wx2_parallel(c_full,q,"c_w_q"+str(i))   #[batch_size,story_length,hidden_size]
        c_w_m=self.x1Wx2_parallel(c_full,m,"c_w_m"+str(i))   #[batch_size,story_length,hidden_size]
        # c_transposeWm
        q_tile=tf.tile(q,[1,self.story_length,1])     #[batch_size,story_length,hidden_size]
        m_tile=tf.tile(m,[1,self.story_length,1])     #[batch_size,story_length,hidden_size]
        z=tf.concat([c_full,m_tile,q_tile,c_q_elementwise,c_m_elementwise,c_q_minus,c_m_minus,c_w_q,c_w_m],2) #[batch_size,story_length,hidden_size*9]
        # 2. two layer feed foward
        g=tf.layers.dense(z,self.hidden_size*3,activation=tf.nn.tanh)  #[batch_size,story_length,hidden_size*3]
        g=tf.layers.dense(g,1,activation=tf.nn.sigmoid)                #[batch_size,story_length,1]
        g=tf.squeeze(g,axis=2)                                         #[batch_size,story_length]
        return g
def triplet_loss(y_true, y_pred, alpha = 0.2):
    """
    Implementation of the triplet loss as defined by formula

    Arguments:
    y_true -- true labels, required when you define a loss in Keras, you don't need it in this function.
    y_pred -- python list containing three objects:
            anchor -- the encodings for the anchor images, of shape (None, 128)
            positive -- the encodings for the positive images, of shape (None, 128)
            negative -- the encodings for the negative images, of shape (None, 128)

    Returns:
    loss -- real number, value of the loss
    """

    anchor, positive, negative = y_pred[0], y_pred[1], y_pred[2]

    ### START CODE HERE ### (≈ 4 lines)
    # Step 1: Compute the (encoding) distance between the anchor and the positive, you will need to sum over axis=-1
    pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)))
    # Step 2: Compute the (encoding) distance between the anchor and the negative, you will need to sum over axis=-1
    neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)))
    # Step 3: subtract the two previous distances and add alpha.
    basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
    # Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
    loss = tf.reduce_sum(tf.maximum(basic_loss, 0.))
    ### END CODE HERE ###

    return loss
Exemplo n.º 11
0
 def getLoss(trueCosSim, falseCosSim, margin):
     zero = tf.fill(tf.shape(trueCosSim), 0.0)
     tfMargin = tf.fill(tf.shape(trueCosSim), margin)
     with tf.name_scope("loss"):
         losses = tf.maximum(zero, tf.subtract(tfMargin, tf.subtract(trueCosSim, falseCosSim)))
         loss = tf.reduce_sum(losses)
     return loss
Exemplo n.º 12
0
def auto_encoder(x_1, x_2, x_mask_1, x_mask_2, y, dropout, opt):
    x_1_emb, W_emb = embedding(x_1, opt)  # batch L emb
    x_2_emb = tf.nn.embedding_lookup(W_emb, x_2)

    x_1_emb = tf.nn.dropout(x_1_emb, dropout)  # batch L emb
    x_2_emb = tf.nn.dropout(x_2_emb, dropout)  # batch L emb

    biasInit = tf.constant_initializer(0.001, dtype=tf.float32)
    x_1_emb = layers.fully_connected(tf.squeeze(x_1_emb), num_outputs=opt.embed_size, biases_initializer=biasInit, activation_fn=tf.nn.relu, scope='trans', reuse=None)  # batch L emb
    x_2_emb = layers.fully_connected(tf.squeeze(x_2_emb), num_outputs=opt.embed_size, biases_initializer=biasInit, activation_fn=tf.nn.relu, scope='trans', reuse=True)

    x_1_emb = tf.expand_dims(x_1_emb, 3)  # batch L emb 1
    x_2_emb = tf.expand_dims(x_2_emb, 3)

    if opt.encoder == 'aver':
        H_enc_1 = aver_emb_encoder(x_1_emb, x_mask_1)
        H_enc_2 = aver_emb_encoder(x_2_emb, x_mask_2)

    elif opt.encoder == 'max':
        H_enc_1 = max_emb_encoder(x_1_emb, x_mask_1, opt)
        H_enc_2 = max_emb_encoder(x_2_emb, x_mask_2, opt)

    elif opt.encoder == 'concat':
        H_enc_1 = concat_emb_encoder(x_1_emb, x_mask_1, opt)
        H_enc_2 = concat_emb_encoder(x_2_emb, x_mask_2, opt)

    # discriminative loss term
    if opt.combine_enc == 'mult':
        H_enc = tf.multiply(H_enc_1, H_enc_2)  # batch * n_gan

    if opt.combine_enc == 'concat':
        H_enc = tf.concat([H_enc_1, H_enc_2], 1)

    if opt.combine_enc == 'sub':
        H_enc = tf.subtract(H_enc_1, H_enc_2)

    if opt.combine_enc == 'mix':
        H_1 = tf.multiply(H_enc_1, H_enc_2)
        H_2 = tf.concat([H_enc_1, H_enc_2], 1)
        H_3 = tf.subtract(H_enc_1, H_enc_2)
        H_enc = tf.concat([H_1, H_2, H_3], 1)

    # calculate the accuracy
    logits = discriminator_2layer(H_enc, opt, dropout, prefix='classify_', num_outputs=opt.category, is_reuse=None)
    prob = tf.nn.softmax(logits)

    correct_prediction = tf.equal(tf.argmax(prob, 1), tf.argmax(y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=logits))

    train_op = layers.optimize_loss(
        loss,
        framework.get_global_step(),
        optimizer='Adam',
        # variables=d_vars,
        learning_rate=opt.lr)

    return accuracy, loss, train_op, W_emb
Exemplo n.º 13
0
    def single_image_TV(patch, patch_size):
        result = tf.Variable(tf.zeros([1, patch_size - 1, 3]))
        slice_result = tf.assign(result, patch[0: 1, 1:, 0: 3])
        for iter in range(1, patch_size - 1):
            temp = tf.assign(result,tf.add(tf.subtract(patch[iter:iter + 1, 1:, 0: 3], patch[iter:iter + 1, 0:-1, 0: 3]),
                                           tf.subtract(patch[iter:iter + 1, 0:-1, 0: 3],patch[iter + 1:iter + 2, 0:-1, 0: 3])))
            slice_result = tf.concat([slice_result, temp], 0)

            return slice_result
    def __init__(self, config, is_training=True):
        self.batch_size = tf.Variable(0, dtype=tf.int32, trainable=False)

        num_step = config.num_step
        embed_dim = config.embed_dim
        self.input_data_s1 = tf.placeholder(tf.float64, [None, num_step, embed_dim])
        self.input_data_s2 = tf.placeholder(tf.float64, [None, num_step, embed_dim])
        self.target = tf.placeholder(tf.float64, [None])
        self.mask_s1 = tf.placeholder(tf.float64, [None, num_step])
        self.mask_s2 = tf.placeholder(tf.float64, [None, num_step])

        self.hidden_neural_size = config.hidden_neural_size
        self.new_batch_size = tf.placeholder(tf.int32, shape=[], name="new_batch_size")
        self._batch_size_update = tf.assign(self.batch_size, self.new_batch_size)

        with tf.name_scope('lstm_output_layer'):
            self.cell_outputs1 = self.singleRNN(x=self.input_data_s1, scope='side1', cell='lstm', reuse=None)
            self.cell_outputs2 = self.singleRNN(x=self.input_data_s2, scope='side1', cell='lstm', reuse=True)

        with tf.name_scope('Sentence_Layer'):
            # self.sent1 = tf.reduce_sum(self.cell_outputs1 * self.mask_s1[:, :, None], axis=1)
            # self.sent2 = tf.reduce_sum(self.cell_outputs2 * self.mask_s2[:, :, None], axis=1)
            # self.mask_s1_sum=tf.reduce_sum(self.mask_s1,axis=0)
            # self.mask_s2_sum=tf.reduce_sum(self.mask_s2,axis=0)
            # self.mask_s1_sum1 = tf.reduce_sum(self.mask_s1, axis=1)
            # self.mask_s2_sum1 = tf.reduce_sum(self.mask_s2, axis=1)
            self.sent1 = tf.reduce_sum(self.cell_outputs1 * self.mask_s1[:, :, None], axis=1)
            self.sent2 = tf.reduce_sum(self.cell_outputs2 * self.mask_s2[:, :, None], axis=1)

        with tf.name_scope('loss'):
            diff = tf.abs(tf.subtract(self.sent1, self.sent2), name='err_l1')
            diff = tf.reduce_sum(diff, axis=1)
            self.sim = tf.clip_by_value(tf.exp(-1.0 * diff), 1e-7, 1.0 - 1e-7)
            self.loss = tf.square(tf.subtract(self.sim, tf.clip_by_value((self.target - 1.0) / 4.0, 1e-7, 1.0 - 1e-7)))

        with tf.name_scope('cost'):
            self.cost = tf.reduce_mean(self.loss)
            self.truecost = tf.reduce_mean(tf.square(tf.subtract(self.sim * 4.0 + 1.0, self.target)))

        if not is_training:
            return

        self.globle_step = tf.Variable(0, name="globle_step", trainable=False, dtype=tf.float64)
        self.lr = tf.Variable(0.0, trainable=False, dtype=tf.float64)

        tvars = tf.trainable_variables()
        grads = tf.gradients(self.cost, tvars)
        optimizer = tf.train.AdadeltaOptimizer(learning_rate=self.lr, epsilon=1e-6)

        with tf.name_scope('train'):
            self.train_op = optimizer.apply_gradients(zip(grads, tvars))

        self.new_lr = tf.placeholder(tf.float64, shape=[], name="new_learning_rate")
        self._lr_update = tf.assign(self.lr, self.new_lr)
Exemplo n.º 15
0
def triplet_loss(y_true, y_pred):
  import tensorflow as tf
  anchor = y_pred[:,0]
  positive = y_pred[:,1]
  negative = y_pred[:,2]
  #anchor, positive, negative = y_pred
  alpha = 0.2
  pos_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, positive)))
  neg_dist = tf.reduce_sum(tf.square(tf.subtract(anchor, negative)))
  basic_loss = tf.add(tf.subtract(pos_dist, neg_dist), alpha)
  loss = tf.maximum(tf.reduce_mean(basic_loss), 0.0)
  return loss
Exemplo n.º 16
0
 def tf_2d_normal(x1, x2, mu1, mu2, s1, s2, rho):
   """Returns result of eq # 24 and 25 of http://arxiv.org/abs/1308.0850."""
   norm1 = tf.subtract(x1, mu1)
   norm2 = tf.subtract(x2, mu2)
   s1s2 = tf.multiply(s1, s2)
   z = (tf.square(tf.div(norm1, s1)) + tf.square(tf.div(norm2, s2)) -
        2 * tf.div(tf.multiply(rho, tf.multiply(norm1, norm2)), s1s2))
   neg_rho = 1 - tf.square(rho)
   result = tf.exp(tf.div(-z, 2 * neg_rho))
   denom = 2 * np.pi * tf.multiply(s1s2, tf.sqrt(neg_rho))
   result = tf.div(result, denom)
   return result
Exemplo n.º 17
0
 def _loss(self, predictions):
     with tf.name_scope("loss"):
         # if training then crop center of y, else, padding was applied
         slice_amt = (np.sum(self.filter_sizes) - len(self.filter_sizes)) / 2
         slice_y = self.y_norm[:,slice_amt:-slice_amt, slice_amt:-slice_amt]
         _y = tf.cond(self.is_training, lambda: slice_y, lambda: self.y_norm)
         tf.subtract(predictions, _y)
         err = tf.square(predictions - _y)
         err_filled = utils.fill_na(err, 0)
         finite_count = tf.reduce_sum(tf.cast(tf.is_finite(err), tf.float32))
         mse = tf.reduce_sum(err_filled) / finite_count
         return mse
Exemplo n.º 18
0
 def loss_with_step(self):
     margin = 5.0
     labels_t = self.y_
     labels_f = tf.subtract(1.0, self.y_, name="1-yi")          # labels_ = !labels;
     eucd2 = tf.pow(tf.subtract(self.o1, self.o2), 2)
     eucd2 = tf.reduce_sum(eucd2, 1)
     eucd = tf.sqrt(eucd2+1e-6, name="eucd")
     C = tf.constant(margin, name="C")
     pos = tf.multiply(labels_t, eucd, name="y_x_eucd")
     neg = tf.multiply(labels_f, tf.maximum(0.0, tf.subtract(C, eucd)), name="Ny_C-eucd")
     losses = tf.add(pos, neg, name="losses")
     loss = tf.reduce_mean(losses, name="loss")
     return loss
Exemplo n.º 19
0
    def __init__(self, _margin, sequence_length, batch_size,
            vocab_size, embedding_size,
            filter_sizes, num_filters, l2_reg_lambda=0.0):
        self.L, self.B, self.V, self.E, self.FS, self.NF = sequence_length, batch_size, \
                vocab_size, embedding_size, filter_sizes, num_filters 

        #用户问题,字向量使用embedding_lookup
        self.q = tf.placeholder(tf.int32, [self.B, self.L], name="q")
        #待匹配正向问题
        self.qp = tf.placeholder(tf.int32, [self.B, self.L], name="qp")
        #负向问题
        self.qn = tf.placeholder(tf.int32, [self.B, self.L], name="qn")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
        l2_loss = tf.constant(0.0)

        # Embedding layer
        with tf.device('/cpu:0'), tf.name_scope("embedding"):
            W = tf.get_variable(
                    initializer=tf.random_uniform([self.V, self.E], -1.0, 1.0), 
                    name='We')
            self.qe = tf.nn.embedding_lookup(W, self.q)
            self.qpe = tf.nn.embedding_lookup(W, self.qp)
            self.qne = tf.nn.embedding_lookup(W, self.qn)
        self.qe = tf.expand_dims(self.qe, -1)
        self.qpe = tf.expand_dims(self.qpe, -1)
        self.qne = tf.expand_dims(self.qne, -1)
        
        with tf.variable_scope('shared-conv') as scope:
            self.qe = self.conv(self.qe)
            scope.reuse_variables()
            #tf.get_variable_scope().reuse_variables()
            self.qpe = self.conv(self.qpe)
            scope.reuse_variables()
            #tf.get_variable_scope().reuse_variables()
            self.qne = self.conv(self.qne)
        self.cos_q_qp = self.cosine(self.qe, self.qpe)
        self.cos_q_qn = self.cosine(self.qe, self.qne)
        zero = tf.constant(0, shape=[self.B], dtype=tf.float32)
        margin = tf.constant(_margin, shape=[self.B], dtype=tf.float32)
        with tf.name_scope("loss"):
            self.losses = tf.maximum(zero, tf.subtract(margin, tf.subtract(self.cos_q_qp, self.cos_q_qn)))
            self.loss = tf.reduce_sum(self.losses) + l2_reg_lambda * l2_loss
            print('loss ', self.loss)

        # Accuracy
        with tf.name_scope("accuracy"):
            self.correct = tf.equal(zero, self.losses)
            self.accuracy = tf.reduce_mean(tf.cast(self.correct, "float"), name="accuracy")

        for v in tf.trainable_variables():
            print(v)
def simple_margin_loss(anchor, positive, negative, alpha):
    '''Calculate the simple margin loss according to paper 'Sampling matters in Deep Embedding Learning'
    Args:
        alpha: here alpha is not in triplet loss, instead, it means the alpha in simple margin loss, need experiment
        belta: default 1.1
    '''
    belta = 1.1
    with tf.variable_scope('triplet_loss'):
        pos_dist = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(anchor, positive)), 1))
        neg_dist = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(anchor, negative)), 1))
        basic_loss = tf.maximum(alpha+pos_dist-belta,0.0) + tf.maximum(alpha-neg_dist+belta,0.0)
        #basic_loss = tf.add(tf.subtract(pos_dist,neg_dist), alpha)
        loss = tf.reduce_mean(basic_loss)
    return loss
Exemplo n.º 21
0
def crop_features(feature, out_size):
    """Crop the center of a feature map
    Args:
    feature: Feature map to crop
    out_size: Size of the output feature map
    Returns:
    Tensor that performs the cropping
    """
    up_size = tf.shape(feature)
    ini_w = tf.div(tf.subtract(up_size[1], out_size[1]), 2)
    ini_h = tf.div(tf.subtract(up_size[2], out_size[2]), 2)
    slice_input = tf.slice(feature, (0, ini_w, ini_h, 0), (-1, out_size[1], out_size[2], -1))
    # slice_input = tf.slice(feature, (0, ini_w, ini_w, 0), (-1, out_size[1], out_size[2], -1))  # Caffe cropping way
    return tf.reshape(slice_input, [int(feature.get_shape()[0]), out_size[1], out_size[2], int(feature.get_shape()[3])])
Exemplo n.º 22
0
    def _build_graph(self, inputs):
        image_pos = inputs[0]
        image_pos = image_pos / 128.0 - 1

        z = tf.random_uniform([args.batch, args.z_dim], minval=-1, maxval=1, name='z_train')
        z = tf.placeholder_with_default(z, [None, args.z_dim], name='z')

        def summary_image(name, x):
            x = (x + 1.0) * 128.0
            x = tf.clip_by_value(x, 0, 255)
            tf.summary.image(name, tf.cast(x, tf.uint8), max_outputs=30)

        with argscope([Conv2D, FullyConnected],
                      W_init=tf.truncated_normal_initializer(stddev=0.02)):
            with tf.variable_scope('gen'):
                image_gen = self.decoder(z)

            with tf.variable_scope('discrim'):
                with tf.variable_scope('enc'):
                    hidden_pos = self.encoder(image_pos)
                    hidden_neg = self.encoder(image_gen)

                with tf.variable_scope('dec'):
                    recon_pos = self.decoder(hidden_pos)
                    recon_neg = self.decoder(hidden_neg)

        with tf.name_scope('viz'):
            summary_image('generated-samples', image_gen)
            summary_image('reconstruct-real', recon_pos)
            summary_image('reconstruct-fake', recon_neg)

        with tf.name_scope('losses'):
            L_pos = tf.reduce_mean(tf.abs(recon_pos - image_pos), name='loss_pos')
            L_neg = tf.reduce_mean(tf.abs(recon_neg - image_gen), name='loss_neg')

            eq = tf.subtract(GAMMA * L_pos, L_neg, name='equilibrium')
            measure = tf.add(L_pos, tf.abs(eq), name='measure')

            kt = tf.get_variable('kt', dtype=tf.float32, initializer=0.0)

            update_kt = kt.assign_add(1e-3 * eq)
            with tf.control_dependencies([update_kt]):
                self.d_loss = tf.subtract(L_pos, kt * L_neg, name='loss_D')
                self.g_loss = L_neg

        add_moving_summary(L_pos, L_neg, eq, measure, self.d_loss)
        tf.summary.scalar('kt', kt)

        self.collect_variables()
Exemplo n.º 23
0
  def preprocess_image(self, image):
    """Applies tf-slim preprocessing operations to image.

    Args:
      image: An single image Tensor of shape [height, width, channel] with
             uint8 values.

    Returns:
      A single image of shape [height, width, channel] containing floating point
      values, with all points rescaled between -1 and 1 and rescaled via
      resize_image_with_crop_or_pad.
    """
    image = tf.to_float(image)
    image = tf.subtract(image, 128.0)
    image = tf.div(image, 128.0)
    # redacted
    # Specifically, our current image height is 100, which is too small so that
    # in inception_v3, this final pool:
    # net = slim.avg_pool2d(net, kernel_size, padding='VALID',
    #                       scope='AvgPool_1a_{}x{}'.format(*kernel_size))
    # actually end up having a kernel_size dimension as 1, but the default
    # stride of that function is (2, 2). So, for inception_v3, we had to resize
    # the height to at least 107 if the height is smaller than that.
    if self.name == 'inception_v3':
      h, w, _ = image.get_shape().as_list()
      image = tf.image.resize_image_with_crop_or_pad(image, max(h, 107), w)
    return image
Exemplo n.º 24
0
    def read_tensor_from_image_file(self, file_name, input_height=299, input_width=299, input_mean=0, input_std=255):
        input_name = "file_reader"
        output_name = "normalized"

        file_reader = tf.read_file(file_name, input_name)

        if file_name.endswith(".png"):
            image_reader = tf.image.decode_png(file_reader, channels=3, name='png_reader')
        elif file_name.endswith(".gif"):
            image_reader = tf.squeeze(tf.image.decode_gif(file_reader, name='gif_reader'))
        elif file_name.endswith(".bmp"):
            image_reader = tf.image.decode_bmp(file_reader, name='bmp_reader')
        else:
            image_reader = tf.image.decode_jpeg(file_reader, channels=3, name='jpeg_reader')

        float_caster = tf.cast(image_reader, tf.float32)
        dims_expander = tf.expand_dims(float_caster, 0);
        resized = tf.image.resize_bilinear(dims_expander, [self.input_height, self.input_width])
        normalized = tf.divide(tf.subtract(resized, [input_mean]), [input_std])
        sess = tf.Session()

        result = sess.run(normalized)
        sess.close()

        return result
Exemplo n.º 25
0
    def __init__(self, n_layers, transfer_function=tf.nn.softplus, optimizer=tf.train.AdamOptimizer()):
        self.n_layers = n_layers
        self.transfer = transfer_function

        network_weights = self._initialize_weights()
        self.weights = network_weights

        # model
        self.x = tf.placeholder(tf.float32, [None, self.n_layers[0]])
        self.hidden_encode = []
        h = self.x
        for layer in range(len(self.n_layers)-1):
            h = self.transfer(
                tf.add(tf.matmul(h, self.weights['encode'][layer]['w']),
                       self.weights['encode'][layer]['b']))
            self.hidden_encode.append(h)

        self.hidden_recon = []
        for layer in range(len(self.n_layers)-1):
            h = self.transfer(
                tf.add(tf.matmul(h, self.weights['recon'][layer]['w']),
                       self.weights['recon'][layer]['b']))
            self.hidden_recon.append(h)
        self.reconstruction = self.hidden_recon[-1]

        # cost
        self.cost = 0.5 * tf.reduce_sum(tf.pow(tf.subtract(self.reconstruction, self.x), 2.0))
        self.optimizer = optimizer.minimize(self.cost)

        init = tf.global_variables_initializer()
        self.sess = tf.Session()
        self.sess.run(init)
Exemplo n.º 26
0
def batch_iou(bboxes, bbox):
  """Compute iou of a batch of boxes with another box. Box format '[y_min, x_min, y_max, x_max]'.
  Args:
    bboxes: A batch of boxes. 2-D with shape `[B, 4]`.
    bbox: A single box. 1-D with shape `[4]`.

  Returns:
    Batch of IOUs
  """
  lr = tf.maximum(
    tf.minimum(bboxes[:, 3], bbox[3]) -
    tf.maximum(bboxes[:, 1], bbox[1]),
    0
  )
  tb = tf.maximum(
    tf.minimum(bboxes[:, 2], bbox[2]) -
    tf.maximum(bboxes[:, 0], bbox[0]),
    0
  )
  intersection = tf.multiply(tb, lr)
  union = tf.subtract(
    tf.multiply((bboxes[:, 3] - bboxes[:, 1]), (bboxes[:, 2] - bboxes[:, 0])) +
    tf.multiply((bbox[3] - bbox[1]), (bbox[2] - bbox[0])),
    intersection
  )
  iou = tf.div(intersection, union)
  return iou
Exemplo n.º 27
0
def softmax(x):
    """
    Compute the softmax function in tensorflow.

    You might find the tensorflow functions tf.exp, tf.reduce_max,
    tf.reduce_sum, tf.expand_dims useful. (Many solutions are possible, so you may
    not need to use all of these functions). Recall also that many common
    tensorflow operations are sugared (e.g. x * y does a tensor multiplication
    if x and y are both tensors). Make sure to implement the numerical stability
    fixes as in the previous homework!

    Args:
        x:   tf.Tensor with shape (n_samples, n_features). Note feature vectors are
                  represented by row-vectors. (For simplicity, no need to handle 1-d
                  input as in the previous homework)
    Returns:
        out: tf.Tensor with shape (n_sample, n_features). You need to construct this
                  tensor in this problem.
    """

    ### YOUR CODE HERE
    x_max = tf.reduce_max(x,1,keep_dims=True)          # find row-wise maximums
    x_sub = tf.subtract(x,x_max)                       # subtract maximums
    x_exp = tf.exp(x_sub)                              # exponentiation
    sum_exp = tf.reduce_sum(x_exp,1,keep_dims=True)    # row-wise sums
    out = tf.div(x_exp,sum_exp)                        # divide
    ### END YOUR CODE

    return out
Exemplo n.º 28
0
def ApplyPcaAndWhitening(data,
                         pca_matrix,
                         pca_mean,
                         output_dim,
                         use_whitening=False,
                         pca_variances=None):
  """Applies PCA/whitening to data.

  Args:
    data: [N, dim] float tensor containing data which undergoes PCA/whitening.
    pca_matrix: [dim, dim] float tensor PCA matrix, row-major.
    pca_mean: [dim] float tensor, mean to subtract before projection.
    output_dim: Number of dimensions to use in output data, of type int.
    use_whitening: Whether whitening is to be used.
    pca_variances: [dim] float tensor containing PCA variances. Only used if
      use_whitening is True.

  Returns:
    output: [N, output_dim] float tensor with output of PCA/whitening operation.
  """
  output = tf.matmul(
      tf.subtract(data, pca_mean),
      tf.slice(pca_matrix, [0, 0], [output_dim, -1]),
      transpose_b=True,
      name='pca_matmul')

  # Apply whitening if desired.
  if use_whitening:
    output = tf.divide(
        output,
        tf.sqrt(tf.slice(pca_variances, [0], [output_dim])),
        name='whitening')

  return output
Exemplo n.º 29
0
def image_preprocessing(image_buffer, bbox, train, thread_id=0):
  """Decode and preprocess one image for evaluation or training.

  Args:
    image_buffer: JPEG encoded string Tensor
    bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
      where each coordinate is [0, 1) and the coordinates are arranged as
      [ymin, xmin, ymax, xmax].
    train: boolean
    thread_id: integer indicating preprocessing thread

  Returns:
    3-D float Tensor containing an appropriately scaled image

  Raises:
    ValueError: if user does not provide bounding box
  """
  if bbox is None:
    raise ValueError('Please supply a bounding box.')

  image = decode_jpeg(image_buffer)
  height = FLAGS.image_size
  width = FLAGS.image_size

  if train:
    image = distort_image(image, height, width, bbox, thread_id)
  else:
    image = eval_image(image, height, width)

  # Finally, rescale to [-1,1] instead of [0, 1)
  image = tf.subtract(image, 0.5)
  image = tf.multiply(image, 2.0)
  return image
def read_and_decode(filename):
    # 根据文件名生成一个队列
    filename_queue = tf.train.string_input_producer([filename])
    reader = tf.TFRecordReader()
    # 返回文件名和文件
    _, serialized_example = reader.read(filename_queue)   
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'image' : tf.FixedLenFeature([], tf.string),
                                           'label0': tf.FixedLenFeature([], tf.int64),
                                           'label1': tf.FixedLenFeature([], tf.int64),
                                           'label2': tf.FixedLenFeature([], tf.int64),
                                           'label3': tf.FixedLenFeature([], tf.int64),
                                       })
    # 获取图片数据
    image = tf.decode_raw(features['image'], tf.uint8)
    # tf.train.shuffle_batch必须确定shape
    image = tf.reshape(image, [224, 224])
    # 图片预处理
    image = tf.cast(image, tf.float32) / 255.0
    image = tf.subtract(image, 0.5)
    image = tf.multiply(image, 2.0)
    # 获取label
    label0 = tf.cast(features['label0'], tf.int32)
    label1 = tf.cast(features['label1'], tf.int32)
    label2 = tf.cast(features['label2'], tf.int32)
    label3 = tf.cast(features['label3'], tf.int32)

    return image, label0, label1, label2, label3
Exemplo n.º 31
0
    def __init__(self, user_num, item_num, f, user_pos_length, user_neg_length, item_pos_length, item_neg_length,
                 user_pos_vocab_size, user_neg_vocab_size, item_pos_vocab_size, item_neg_vocab_size, embedding_size,
                 filter_sizes, num_filters, n_pos_aspect, n_neg_aspect):

        self.input_u_pos = tf.placeholder(tf.int32, [None, user_pos_length], name='input_u_pos')
        self.input_u_neg = tf.placeholder(tf.int32, [None, user_neg_length], name='input_u_neg')
        self.input_i_pos = tf.placeholder(tf.int32, [None, item_pos_length], name='input_i_pos')
        self.input_i_neg = tf.placeholder(tf.int32, [None, item_neg_length], name='input_i_neg')
        self.input_y = tf.placeholder(tf.float32, [None, 1], name="input_y")
        self.input_uid = tf.placeholder(tf.int32, [None, 1], name="input_uid")
        self.input_iid = tf.placeholder(tf.int32, [None, 1], name="input_iid")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")

        with tf.name_scope("user_pos_embedding"):
            self.Wu_pos = tf.Variable(tf.random_uniform([user_pos_vocab_size, embedding_size], -1.0, 1.0), trainable=False, name='Wu_pos')
            self.embedded_user_pos = tf.nn.embedding_lookup(self.Wu_pos, self.input_u_pos)
            self.embedded_users_pos = tf.expand_dims(self.embedded_user_pos, -1)

        with tf.name_scope("user_neg_embedding"):
            self.Wu_neg = tf.Variable(tf.random_uniform([user_neg_vocab_size, embedding_size], -1.0, 1.0), trainable=False, name='Wu_neg')
            self.embedded_user_neg = tf.nn.embedding_lookup(self.Wu_neg, self.input_u_neg)
            self.embedded_users_neg = tf.expand_dims(self.embedded_user_neg, -1)

        with tf.name_scope("item_pos_embedding"):
            self.Wi_pos = tf.Variable(tf.random_uniform([item_pos_vocab_size, embedding_size], -1.0, 1.0), trainable=False, name='Wi_pos')
            self.embedded_item_pos = tf.nn.embedding_lookup(self.Wi_pos, self.input_i_pos)
            self.embedded_items_pos = tf.expand_dims(self.embedded_item_pos, -1)

        with tf.name_scope("item_neg_embedding"):
            self.Wi_neg = tf.Variable(tf.random_uniform([item_neg_vocab_size, embedding_size], -1.0, 1.0), trainable=False, name='Wi_neg')
            self.embedded_item_neg = tf.nn.embedding_lookup(self.Wi_neg, self.input_i_neg)
            self.embedded_items_neg = tf.expand_dims(self.embedded_item_neg, -1)

        with tf.name_scope("user_latent_factors"):
            self.user_Matrix = tf.Variable(tf.random_uniform([user_num, f], -1.0, 1.0), name='user_Matrix')
            self.user_latent = tf.nn.embedding_lookup(self.user_Matrix, self.input_uid)
            self.user_latent = tf.reshape(self.user_latent, [-1, f])

        with tf.name_scope("item_latent_factors"):
            self.item_Matrix = tf.Variable(tf.random_uniform([item_num, f], -1.0, 1.0), name='item_Matrix')
            self.item_latent = tf.nn.embedding_lookup(self.item_Matrix, self.input_iid)
            self.item_latent = tf.reshape(self.item_latent, [-1, f])

        with tf.name_scope("pos_aspect_weight"):
            self.pos_W = tf.Variable(tf.random_uniform([n_pos_aspect, f], -1.0, 1.0), name='pos_W')

        with tf.name_scope("neg_aspect_weight"):
            self.neg_W = tf.Variable(tf.random_uniform([n_neg_aspect, f], -1.0, 1.0), name='neg_W')

        output_u_pos = []
        for i, filter_size in enumerate(filter_sizes):
            with tf.name_scope("user_pos_conv-%s" % filter_size):
                # Convolution Layer
                filter_shape = [filter_size, embedding_size, 1, num_filters]
                W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
                b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
                conv = tf.nn.conv2d(
                    self.embedded_users_pos,
                    W,
                    strides=[1, 1, embedding_size, 1],
                    padding="SAME",
                    name="conv")  # batch_size * user_pos_length * 1 * num_filters
                # Apply nonlinearity
                h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
                h1 = tf.reshape(h, [-1, user_pos_length, num_filters])
                output_u_pos.append(h1)

        num_filters_total = num_filters * len(filter_sizes)
        self.output_u_pos_con = tf.concat(output_u_pos, 2)
        self.output_u_pos_res = tf.reshape(self.output_u_pos_con, [-1, num_filters_total])
        # Layer 1
        Wu_pos_1 = tf.get_variable("Wu_pos_1", shape=[num_filters_total, n_pos_aspect],
                                   initializer=tf.contrib.layers.xavier_initializer())
        bu_pos_1 = tf.Variable(tf.constant(0.1, shape = [n_pos_aspect]))
        self.u_pos_l1 = tf.nn.softmax(tf.nn.relu(tf.matmul(self.output_u_pos_res, Wu_pos_1) + bu_pos_1))


        self.pos_asp = tf.reduce_sum(tf.reshape(self.u_pos_l1, [-1, user_pos_length, n_pos_aspect]), axis=1)
        self.pos_asp_imp = tf.nn.softmax(self.pos_asp)  # batch_size * n_pos_aspect


        output_u_neg = []
        for i, filter_size in enumerate(filter_sizes):
            with tf.name_scope("user_neg_conv-%s" % filter_size):
                # Convolution Layer
                filter_shape = [filter_size, embedding_size, 1,
                                num_filters]  # [filter_height, filter_width, in_channels, out_channels]
                W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
                b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
                conv = tf.nn.conv2d(
                    self.embedded_users_neg,
                    W,
                    strides=[1, 1, embedding_size, 1],
                    padding="SAME",
                    name="conv")
                # Apply nonlinearity
                h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
                h1 = tf.reshape(h, [-1, user_neg_length, num_filters])
                output_u_neg.append(h1)

        self.output_u_neg_con = tf.concat(output_u_neg, 2)
        self.output_u_neg_res = tf.reshape(self.output_u_neg_con, [-1, num_filters_total])
        # Layer 1
        Wu_neg_1 = tf.get_variable("Wu_neg_1", shape=[num_filters_total, n_neg_aspect],
                                   initializer=tf.contrib.layers.xavier_initializer())
        bu_neg_1 = tf.Variable(tf.constant(0.1, shape=[n_neg_aspect]))
        self.u_neg_l1 = tf.nn.softmax(tf.nn.relu(tf.matmul(self.output_u_neg_res, Wu_neg_1) + bu_neg_1))


        self.neg_asp = tf.reduce_sum(tf.reshape(self.u_neg_l1, [-1, user_neg_length, n_neg_aspect]), axis=1)
        self.neg_asp_imp = tf.nn.softmax(self.neg_asp)  # batch_size * n_neg_aspect



        neg_asp_imp_add = []
        with tf.name_scope("pos2neg_imp"):
            W = tf.Variable(tf.truncated_normal(shape=[f, f], stddev=0.1), name="W")
            b = tf.Variable(tf.constant(0.1, shape=[f]), name='b')
            h = tf.Variable(tf.truncated_normal(shape=[f, 1], stddev=0.1), name="h")
            for i in range(n_neg_aspect):
                neg_Wi = self.neg_W[i]
                mul = tf.multiply(self.pos_W, neg_Wi)
                rel = tf.nn.relu(tf.matmul(mul, W) + b)
                attn = tf.nn.softmax(tf.matmul(rel, h), dim=0)  # n_pos_aspect * 1
                neg_asp_imp_i = tf.matmul(self.pos_asp_imp, attn)  # batch_size * 1
                neg_asp_imp_add.append(neg_asp_imp_i)

        pos_asp_imp_add = []
        with tf.name_scope("neg2pos_imp"):
            W = tf.Variable(tf.truncated_normal(shape=[f, f], stddev=0.1), name="W")
            b = tf.Variable(tf.constant(0.1, shape=[f]), name='b')
            h = tf.Variable(tf.truncated_normal(shape=[f, 1], stddev=0.1), name="h")
            for i in range(n_pos_aspect):
                pos_Wi = self.pos_W[i]
                mul = tf.multiply(self.neg_W, pos_Wi)
                rel = tf.nn.relu(tf.matmul(mul, W) + b)
                attn = tf.nn.softmax(tf.matmul(rel, h), dim=0)
                pos_asp_imp_i = tf.matmul(self.neg_asp_imp, attn)
                pos_asp_imp_add.append(pos_asp_imp_i)

        with tf.name_scope("prediction"):
            # print(self.user_latent.shape())
            self.interaction = tf.multiply(self.user_latent, self.item_latent)
            self.pos_asp_r = tf.matmul(self.interaction, tf.transpose(self.pos_W))  # batch_size * n_pos_asp
            self.pos_imp = self.pos_asp_imp + tf.concat(pos_asp_imp_add, -1)
            self.pos_r = tf.reduce_sum(tf.multiply(self.pos_asp_r, self.pos_imp), axis=-1)

            self.neg_asp_r = tf.matmul(self.interaction, tf.transpose(self.neg_W))
            self.neg_imp = self.neg_asp_imp + tf.concat(neg_asp_imp_add, -1)
            self.neg_r = tf.reduce_sum(tf.multiply(self.neg_asp_r, self.neg_imp), axis=-1)

            self.predictions = self.pos_r - self.neg_r

        regularizer = layers.l2_regularizer(scale=1.0)
        Var_list_1 = [Wu_pos_1, bu_pos_1, Wu_neg_1, bu_neg_1]

        for i, filter_size in enumerate(filter_sizes):
            Var_list_1 += tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="user_pos_conv-%s" % filter_size)
            Var_list_1 += tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="user_neg_conv-%s" % filter_size)

        reg_1 = layers.apply_regularization(regularizer, weights_list=Var_list_1)

        Var_list_2 = []

        Var_list_3 = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="pos2neg_imp") \
                     + tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope="neg2pos_imp")

        reg_3 = layers.apply_regularization(regularizer, weights_list=Var_list_3)

        self.variables = Var_list_1 + Var_list_2 + Var_list_3

        reg_4 = layers.apply_regularization(regularizer, weights_list=[self.user_Matrix, self.item_Matrix, self.pos_W, self.neg_W])


        with tf.name_scope("loss"):
            beta_1 = 1e-4
            beta_2 = 0.001
            losses = tf.reduce_mean(tf.square(tf.subtract(self.predictions, self.input_y)))
            self.loss = losses + beta_2 * (reg_1 + reg_3 + reg_4)


        with tf.name_scope("accuracy"):
            self.mae = tf.reduce_mean(tf.abs(tf.subtract(self.predictions, self.input_y)))
            self.accuracy = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.predictions, self.input_y))))
Exemplo n.º 32
0
def kl_div(rho, rho_hat):
    with tf.name_scope('KL_divergence'):
        invrho = tf.subtract(tf.constant(1.), rho)
        invrhohat = tf.subtract(tf.constant(1.), rho_hat)
        logrho = tf.add(logfunc(rho, rho_hat), logfunc(invrho, invrhohat))
    return logrho
Exemplo n.º 33
0
tf.app.flags.DEFINE_string('data_path', None, 'path of dataset.')
tf.app.flags.DEFINE_string('subset', None,
                           'can be "dress" or "shirt" or "toptee".')
tf.app.flags.DEFINE_integer('threads', 16, "image processing threads.")
tf.app.flags.DEFINE_boolean('constant_lr', True,
                            'whether to use a constant learning rate.')
tf.app.flags.DEFINE_boolean('augmentation', False,
                            'whether to use data augmentation.')

FLAGS = tf.app.flags.FLAGS

squeeze = lambda x: tf.squeeze(x, [1, 2])
normalization = lambda x: tf.nn.l2_normalize(x, 1, 1e-10)
expand_dims = lambda x: tf.expand_dims(tf.expand_dims(x, 1), 1)
pairwise_distance = lambda f1, f2, dim: tf.reduce_sum(
    tf.square(tf.subtract(f1, f2)), dim)


def _build_model(source_images, target_images, modify_texts, seqlengths,
                 vocab_size):
    """ define model & loss """
    with tf.variable_scope(tf.get_variable_scope()):
        cnn_features_source = image_model_ml(source_images)

    with tf.variable_scope(tf.get_variable_scope(), reuse=True):
        cnn_features_target = image_model_ml(target_images)

    lstm_features = text_model(modify_texts, seqlengths, vocab_size)
    mod_text = projection_layer(lstm_features,
                                FLAGS.text_projection_dropout,
                                scope='text')
Exemplo n.º 34
0
Using Case

Learnings - 
1. tf.case
2. typecasting in tensorflow
3. tf.Interactivesession
'''

import tensorflow as tf

sess = tf.InteractiveSession()
x = tf.random_uniform([])
y = tf.random_uniform([])
out1 = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda:
               (tf.subtract(x, y)))
print(x.eval(), y.eval(), out1.eval())

x = tf.random_uniform([], -1, 1)
y = tf.random_uniform([], -1, 1)


def f1():
    return tf.cast(tf.add(x, y), tf.float32)


def f2():
    return tf.cast(tf.subtract(x, y), tf.float32)


def f3():
Exemplo n.º 35
0
def get_class(name):
    Chot = np.array([0.0] * class_dim)
    Chot[int(name)] = 1.0
    return Chot


def get_class_number(name):
    res = re.findall('[0-9]+', name)

    return int(res[0]) - 1


batch_size = 10

image = tf.placeholder(tf.float32, [None, 224, 224, 3])
image = tf.div(tf.subtract(image, tf.reduce_mean(image)), reduce_std(image))

labels = tf.placeholder(tf.float32, [None, 54])
net = VGG_CNN_F({'data': image})

fc7 = net.layers['fc7']

print fc7.get_shape()

w1 = tf.Variable(tf.random_normal(shape=[4096, 100],
                                  mean=0,
                                  stddev=2 / np.sqrt(4096)),
                 name='feature_layer')
b1 = tf.Variable(tf.zeros([100]), name='feature_layer_bias')
output = tf.matmul(fc7, w1) + b1
w = tf.Variable(tf.random_normal(shape=[100, 54],
Exemplo n.º 36
0
# variable based on our loss function
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()

# 创建一个计算图会话
sess = tf.Session()

a = tf.Variable(tf.constant(4.))
x_val = 5.
x_data = tf.placeholder(dtype=tf.float32)

multiplication = tf.multiply(a, x_data)
loss = tf.square(tf.subtract(multiplication, 50.))

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

my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

print('Optimizing a Multiplication Gate Output to 50.')
for i in range(10):
    sess.run(train_step, feed_dict={x_data: x_val})
    a_val = sess.run(a)
    mult_output = sess.run(multiplication, feed_dict={x_data: x_val})
    print(str(a_val) + ' * ' + str(x_val) + ' = ' + str(mult_output))

#----------------------------------
def preprocess_for_train(image,
                         height,
                         width,
                         bbox,
                         fast_mode=True,
                         scope=None,
                         add_image_summaries=True,
                         random_crop=True,
                         use_grayscale=False):
  """Distort one image for training a network.

  Distorting images provides a useful technique for augmenting the data
  set during training in order to make the network invariant to aspects
  of the image that do not effect the label.

  Additionally it would create image_summaries to display the different
  transformations applied to the image.

  Args:
    image: 3-D Tensor of image. If dtype is tf.float32 then the range should be
      [0, 1], otherwise it would converted to tf.float32 assuming that the range
      is [0, MAX], where MAX is largest positive representable number for
      int(8/16/32) data type (see `tf.image.convert_image_dtype` for details).
    height: integer
    width: integer
    bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords]
      where each coordinate is [0, 1) and the coordinates are arranged
      as [ymin, xmin, ymax, xmax].
    fast_mode: Optional boolean, if True avoids slower transformations (i.e.
      bi-cubic resizing, random_hue or random_contrast).
    scope: Optional scope for name_scope.
    add_image_summaries: Enable image summaries.
    random_crop: Enable random cropping of images during preprocessing for
      training.
    use_grayscale: Whether to convert the image from RGB to grayscale.
  Returns:
    3-D float Tensor of distorted image used for training with range [-1, 1].
  """
  with tf.name_scope(scope, 'distort_image', [image, height, width, bbox]):
    if bbox is None:
      bbox = tf.constant([0.0, 0.0, 1.0, 1.0],
                         dtype=tf.float32,
                         shape=[1, 1, 4])
    if image.dtype != tf.float32:
      image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    # Each bounding box has shape [1, num_boxes, box coords] and
    # the coordinates are ordered [ymin, xmin, ymax, xmax].
    image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0),
                                                  bbox)
    if add_image_summaries:
      tf.summary.image('image_with_bounding_boxes', image_with_box)

    if not random_crop:
      distorted_image = image
    else:
      distorted_image, distorted_bbox = distorted_bounding_box_crop(image, bbox)
      # Restore the shape since the dynamic slice based upon the bbox_size loses
      # the third dimension.
      distorted_image.set_shape([None, None, 3])
      image_with_distorted_box = tf.image.draw_bounding_boxes(
          tf.expand_dims(image, 0), distorted_bbox)
      if add_image_summaries:
        tf.summary.image('images_with_distorted_bounding_box',
                         image_with_distorted_box)

    # This resizing operation may distort the images because the aspect
    # ratio is not respected. We select a resize method in a round robin
    # fashion based on the thread number.
    # Note that ResizeMethod contains 4 enumerated resizing methods.

    # We select only 1 case for fast_mode bilinear.
    num_resize_cases = 1 if fast_mode else 4
    distorted_image = apply_with_random_selector(
        distorted_image,
        lambda x, method: tf.image.resize_images(x, [height, width], method),
        num_cases=num_resize_cases)

    if add_image_summaries:
      tf.summary.image(('cropped_' if random_crop else '') + 'resized_image',
                       tf.expand_dims(distorted_image, 0))

    # Randomly flip the image horizontally.
    distorted_image = tf.image.random_flip_left_right(distorted_image)

    # Randomly distort the colors. There are 1 or 4 ways to do it.
    num_distort_cases = 1 if fast_mode else 4
    distorted_image = apply_with_random_selector(
        distorted_image,
        lambda x, ordering: distort_color(x, ordering, fast_mode),
        num_cases=num_distort_cases)

    if use_grayscale:
      distorted_image = tf.image.rgb_to_grayscale(distorted_image)

    if add_image_summaries:
      tf.summary.image('final_distorted_image',
                       tf.expand_dims(distorted_image, 0))
    distorted_image = tf.subtract(distorted_image, 0.5)
    distorted_image = tf.multiply(distorted_image, 2.0)
    return distorted_image
Exemplo n.º 38
0
    def build_graph(self):
        # Reset previous graph.
        reset_graph()

        # Placeholders.
        x_source = tf.placeholder(tf.int32,
                                  shape=[None, None],
                                  name="x_source")

        source_seq_length = tf.placeholder(tf.int32,
                                           shape=[None],
                                           name="source_seq_length")

        x_target = tf.placeholder(tf.int32,
                                  shape=[None, None],
                                  name="x_target")

        target_seq_length = tf.placeholder(tf.int32,
                                           shape=[None],
                                           name="target_seq_length")

        labels = tf.placeholder(tf.float32, shape=[None], name="labels")

        input_dropout = tf.placeholder_with_default(1.0,
                                                    shape=[],
                                                    name="input_dropout")

        output_dropout = tf.placeholder_with_default(1.0,
                                                     shape=[],
                                                     name="output_dropout")

        decision_threshold = tf.placeholder_with_default(
            0.5, shape=[], name="decision_threshold")

        # Embedding layer.
        with tf.variable_scope("embeddings"):
            if self.config.source_embeddings_path is not None and self.config.target_embeddings_path is not None:
                source_pretrained_embeddings,\
                target_pretrained_embeddings = get_pretrained_embeddings(
                    source_embeddings_path,
                    target_embeddings_path,
                    source_vocab,
                    target_vocab)
                assert source_pretrained_embeddings.shape[
                    1] == target_pretrained_embeddings.shape[1]
                self.config.embedding_size = source_pretrained_embeddings.shape[
                    1]
                if self.config.fix_pretrained:
                    source_embeddings = tf.get_variable(
                        name="source_embeddings_matrix",
                        shape=[
                            self.config.source_vocab_size,
                            self.config.embedding_size
                        ],
                        initializer=tf.constant_initializer(
                            source_pretrained_embeddings),
                        trainable=False)
                    target_embeddings = tf.get_variable(
                        name="target_embeddings_matrix",
                        shape=[
                            self.config.target_vocab_size,
                            self.config.embedding_size
                        ],
                        initializer=tf.constant_initializer(
                            target_pretrained_embeddings),
                        trainable=False)
                else:
                    source_embeddings = tf.get_variable(
                        name="source_embeddings_matrix",
                        shape=[
                            self.config.source_vocab_size,
                            self.config.embedding_size
                        ],
                        initializer=tf.constant_initializer(
                            source_pretrained_embeddings))
                    target_embeddings = tf.get_variable(
                        name="target_embeddings_matrix",
                        shape=[
                            self.config.target_vocab_size,
                            self.config.embedding_size
                        ],
                        initializer=tf.constant_initializer(
                            target_pretrained_embeddings))
            else:
                source_embeddings = tf.get_variable(
                    name="source_embeddings_matrix",
                    shape=[
                        self.config.source_vocab_size,
                        self.config.embedding_size
                    ])
                target_embeddings = tf.get_variable(
                    name="target_embeddings_matrix",
                    shape=[
                        self.config.target_vocab_size,
                        self.config.embedding_size
                    ])

            source_rnn_inputs = tf.nn.embedding_lookup(source_embeddings,
                                                       x_source)
            target_rnn_inputs = tf.nn.embedding_lookup(target_embeddings,
                                                       x_target)
            source_rnn_inputs = tf.nn.dropout(source_rnn_inputs,
                                              keep_prob=input_dropout,
                                              name="source_seq_embeddings")
            target_rnn_inputs = tf.nn.dropout(target_rnn_inputs,
                                              keep_prob=input_dropout,
                                              name="target_seq_embeddings")

        # BiRNN encoder.
        with tf.variable_scope("birnn") as scope:
            if self.config.use_lstm:
                cell_fw = tf.nn.rnn_cell.LSTMCell(self.config.state_size,
                                                  use_peepholes=True)
                cell_bw = tf.nn.rnn_cell.LSTMCell(self.config.state_size,
                                                  use_peepholes=True)
            else:
                cell_fw = tf.nn.rnn_cell.GRUCell(self.config.state_size)
                cell_bw = tf.nn.rnn_cell.GRUCell(self.config.state_size)

            cell_fw = tf.nn.rnn_cell.DropoutWrapper(
                cell_fw, output_keep_prob=output_dropout)
            cell_bw = tf.nn.rnn_cell.DropoutWrapper(
                cell_bw, output_keep_prob=output_dropout)

            if self.config.num_layers > 1:
                if self.config.use_lstm:
                    cell_fw = tf.nn.rnn_cell.MultiRNNCell([
                        tf.nn.rnn_cell.LSTMCell(self.config.state_size,
                                                use_peepholes=True)
                        for _ in range(self.config.num_layers)
                    ])
                    cell_bw = tf.nn.rnn_cell.MultiRNNCell([
                        tf.nn.rnn_cell.LSTMCell(self.config.state_size,
                                                use_peepholes=True)
                        for _ in range(self.config.num_layers)
                    ])
                else:
                    cell_fw = tf.nn.rnn_cell.MultiRNNCell([
                        tf.nn.rnn_cell.GRUCell(self.config.state_size)
                        for _ in range(self.config.num_layers)
                    ])
                    cell_bw = tf.nn.rnn_cell.MultiRNNCell([
                        tf.nn.rnn_cell.GRUCell(self.config.state_size)
                        for _ in range(self.config.num_layers)
                    ])

            with tf.variable_scope(scope):
                source_rnn_outputs, source_final_state = tf.nn.bidirectional_dynamic_rnn(
                    cell_fw=cell_fw,
                    cell_bw=cell_bw,
                    inputs=source_rnn_inputs,
                    sequence_length=source_seq_length,
                    dtype=tf.float32)

            with tf.variable_scope(scope, reuse=True):
                target_rnn_outputs, target_final_state = tf.nn.bidirectional_dynamic_rnn(
                    cell_fw=cell_fw,
                    cell_bw=cell_bw,
                    inputs=target_rnn_inputs,
                    sequence_length=target_seq_length,
                    dtype=tf.float32)

            self.config.state_size *= 2
            # Mean and max pooling only work for 1 layer BiRNN.
            if self.config.use_mean_pooling:
                source_final_state = self.average_pooling(
                    source_rnn_outputs, source_seq_length)
                target_final_state = self.average_pooling(
                    target_rnn_outputs, target_seq_length)
            elif self.config.use_max_pooling:
                source_final_state = self.max_pooling(source_rnn_outputs)
                target_final_state = self.max_pooling(target_rnn_outputs)
            else:
                source_final_state_fw, source_final_state_bw = source_final_state
                target_final_state_fw, target_final_state_bw = target_final_state
                if self.config.num_layers > 1:
                    source_final_state_fw = source_final_state_fw[-1]
                    source_final_state_bw = source_final_state_bw[-1]
                    target_final_state_fw = target_final_state_fw[-1]
                    target_final_state_bw = target_final_state_bw[-1]
                if self.config.use_lstm:
                    source_final_state_fw = source_final_state_fw.h
                    source_final_state_bw = source_final_state_bw.h
                    target_final_state_fw = target_final_state_fw.h
                    target_final_state_bw = target_final_state_bw.h
                source_final_state = tf.concat(
                    [source_final_state_fw, source_final_state_bw], axis=1)
                target_final_state = tf.concat(
                    [target_final_state_fw, target_final_state_bw], axis=1)

        # Feed-forward neural network.
        with tf.variable_scope("feed_forward"):
            h_multiply = tf.multiply(source_final_state, target_final_state)
            h_abs_diff = tf.abs(
                tf.subtract(source_final_state, target_final_state))

            W_1 = tf.get_variable(
                name="W_1",
                shape=[self.config.state_size, self.config.hidden_size])
            W_2 = tf.get_variable(
                name="W_2",
                shape=[self.config.state_size, self.config.hidden_size])
            b_1 = tf.get_variable(name="b_1",
                                  shape=[self.config.hidden_size],
                                  initializer=tf.constant_initializer(0.0))

            h_semantic = tf.tanh(
                tf.matmul(h_multiply, W_1) + tf.matmul(h_abs_diff, W_2) + b_1)

            W_3 = tf.get_variable(name="W_3",
                                  shape=[self.config.hidden_size, 1])
            b_2 = tf.get_variable(name="b_2",
                                  shape=[1],
                                  initializer=tf.constant_initializer(0.0))

            logits = tf.matmul(h_semantic, W_3) + b_2
            logits = tf.squeeze(logits, name="logits")

            # Sigmoid output layer.
            with tf.name_scope("output"):
                probs = tf.sigmoid(logits, name="probs")
                predicted_class = tf.cast(tf.greater(probs,
                                                     decision_threshold),
                                          tf.float32,
                                          name="predicted_class")

        # Loss.
        with tf.name_scope("cross_entropy"):
            losses = tf.nn.sigmoid_cross_entropy_with_logits(
                logits=logits,
                labels=labels,
                name="cross_entropy_per_sequence")
            mean_loss = tf.reduce_mean(losses, name="cross_entropy_loss")

        # Optimization.
        with tf.name_scope("optimization"):
            global_step = tf.Variable(initial_value=0,
                                      trainable=False,
                                      name="global_step")
            optimizer = tf.train.AdamOptimizer(self.config.learning_rate)
            trainable_variables = tf.trainable_variables()
            gradients = tf.gradients(mean_loss,
                                     trainable_variables,
                                     name="gradients")
            clipped_gradients, global_norm = tf.clip_by_global_norm(
                gradients,
                clip_norm=self.config.max_gradient_norm,
                name="clipped_gradients")
            train_op = optimizer.apply_gradients(zip(clipped_gradients,
                                                     trainable_variables),
                                                 global_step=global_step)

        # Evaluation metrics.
        accuracy = tf.metrics.accuracy(labels,
                                       predicted_class,
                                       name="accuracy")
        precision = tf.metrics.precision(labels,
                                         predicted_class,
                                         name="precision")
        recall = tf.metrics.recall(labels, predicted_class, name="recall")

        # Add summaries.
        tf.summary.scalar("loss", mean_loss)
        tf.summary.scalar("global_norm", global_norm)
        tf.summary.scalar("accuracy", accuracy[0])
        tf.summary.scalar("precision", precision[0])
        tf.summary.scalar("recall", recall[0])
        tf.summary.scalar("logits" + "/sparsity", tf.nn.zero_fraction(logits))
        tf.summary.histogram("logits" + "/activations", logits)
        tf.summary.histogram("probs", probs)

        # Add histogram for trainable variables.
        for var in trainable_variables:
            tf.summary.histogram(var.op.name, var)

        # Add histogram for gradients.
        for grad, var in zip(clipped_gradients, trainable_variables):
            if grad is not None:
                tf.summary.histogram(var.op.name + "/gradients", grad)

        # Assign placeholders and operations.
        self.x_source = x_source
        self.x_target = x_target
        self.source_seq_length = source_seq_length
        self.target_seq_length = target_seq_length
        self.labels = labels
        self.input_dropout = input_dropout
        self.output_dropout = output_dropout
        self.decision_threshold = decision_threshold
        self.train_op = train_op
        self.probs = probs
        self.predicted_class = predicted_class
        self.mean_loss = mean_loss
        self.accuracy = accuracy
        self.precision = precision
        self.recall = recall
        self.summaries = tf.summary.merge_all()
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=5)
Exemplo n.º 39
0
    def __init__(self,
                 n_actions,
                 hidden=1024,
                 learning_rate=0.00001,
                 frame_height=84,
                 frame_width=84,
                 agent_history_length=4):
        """
        Args:
            n_actions: Integer, number of possible actions
            hidden: Integer, Number of filters in the final convolutional layer.
                    This is different from the DeepMind implementation
            learning_rate: Float, Learning rate for the Adam optimizer
            frame_height: Integer, Height of a frame of an Atari game
            frame_width: Integer, Width of a frame of an Atari game
            agent_history_length: Integer, Number of frames stacked together to create a state
        """
        self.n_actions = n_actions
        self.hidden = hidden
        self.learning_rate = learning_rate
        self.frame_height = frame_height
        self.frame_width = frame_width
        self.agent_history_length = agent_history_length

        self.input = tf.placeholder(shape=[
            None, self.frame_height, self.frame_width,
            self.agent_history_length
        ],
                                    dtype=tf.float32)
        # Normalizing the input
        self.inputscaled = self.input / 255

        # Convolutional layers
        self.conv1 = tf.layers.conv2d(
            inputs=self.inputscaled,
            filters=32,
            kernel_size=[8, 8],
            strides=4,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            padding="valid",
            activation=tf.nn.relu,
            use_bias=False,
            name='conv1')
        self.conv2 = tf.layers.conv2d(
            inputs=self.conv1,
            filters=64,
            kernel_size=[4, 4],
            strides=2,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            padding="valid",
            activation=tf.nn.relu,
            use_bias=False,
            name='conv2')
        self.conv3 = tf.layers.conv2d(
            inputs=self.conv2,
            filters=64,
            kernel_size=[3, 3],
            strides=1,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            padding="valid",
            activation=tf.nn.relu,
            use_bias=False,
            name='conv3')
        self.conv4 = tf.layers.conv2d(
            inputs=self.conv3,
            filters=hidden,
            kernel_size=[7, 7],
            strides=1,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            padding="valid",
            activation=tf.nn.relu,
            use_bias=False,
            name='conv4')

        # Splitting into value and advantage stream
        self.valuestream, self.advantagestream = tf.split(self.conv4, 2, 3)
        self.valuestream = tf.layers.flatten(self.valuestream)
        self.advantagestream = tf.layers.flatten(self.advantagestream)
        self.advantage = tf.layers.dense(
            inputs=self.advantagestream,
            units=self.n_actions,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            name="advantage")
        self.value = tf.layers.dense(
            inputs=self.valuestream,
            units=1,
            kernel_initializer=tf.variance_scaling_initializer(scale=2),
            name='value')

        # Combining value and advantage into Q-values as described above
        self.q_values = self.value + tf.subtract(
            self.advantage,
            tf.reduce_mean(self.advantage, axis=1, keep_dims=True))
        self.best_action = tf.argmax(self.q_values, 1)
        self.best_q_value = tf.reduce_max(self.q_values, 1)

        # The next lines perform the parameter update.

        # targetQ according to Bellman equation:
        # Q = r + gamma*max Q', calculated in the function learn()
        self.target_q = tf.placeholder(shape=[None], dtype=tf.float32)
        # Action that was performed
        self.action = tf.placeholder(shape=[None], dtype=tf.int32)
        # Q value of the action that was performed
        self.Q = tf.reduce_sum(tf.multiply(
            self.q_values,
            tf.one_hot(self.action, self.n_actions, dtype=tf.float32)),
                               axis=1)

        # Parameter updates
        self.loss = tf.reduce_mean(
            tf.losses.huber_loss(labels=self.target_q, predictions=self.Q))
        self.optimizer = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate)
        self.update = self.optimizer.minimize(self.loss)
    def _init_graph(self):
        self.graph = tf.Graph()  # 新建图
        with self.graph.as_default():  # 把该图作为默认图

            tf.set_random_seed(self.random_seed)  # 设置随机数种子
            np.random.seed(self.random_seed)

            self.feat_index = tf.placeholder(
                tf.int32, shape=[None,
                                 None], name="feat_index")  # 使用样本数*field_size
            self.feat_value = tf.placeholder(
                tf.float32, shape=[None, None],
                name="feat_value")  # 使用样本数*field_size
            self.label = tf.placeholder(tf.float32,
                                        shape=[None, 1],
                                        name="label")  # 使用样本数 * 1
            self.dropout_keep_fm = tf.placeholder(
                tf.float32, shape=[None],
                name="dropout_keep_fm")  # fm层dropout保留的比例
            self.dropout_keep_deep = tf.placeholder(
                tf.float32, shape=[None],
                name="dropout_keep_deep")  # deep层dropout保留的比例
            self.train_phase = tf.placeholder(
                tf.bool, name="train_phase")  # 是否是训练阶段的flag

            self.weights = self._initialize_weights()  # 初始化权重,即变量

            # model
            # embedding查表
            self.embeddings = tf.nn.embedding_lookup(
                self.weights["feature_embeddings"],
                self.feat_index)  # 使用样本数*field_size*field_size*embedding_size
            feat_value = tf.reshape(self.feat_value,
                                    shape=[-1, self.field_size, 1])
            self.embeddings = tf.multiply(
                self.embeddings,
                tf.reshape(self.feat_value, shape=[-1, self.field_size, 1, 1]))

            # ---------- first order term ----------
            self.y_first_order = tf.nn.embedding_lookup(
                self.weights["feature_bias"],
                self.feat_index)  # None * field_size * 1
            # reduce_sum函数用于在某一维度求和
            self.y_first_order = tf.reduce_sum(
                tf.multiply(self.y_first_order, feat_value), 2)  # None * F
            self.y_first_order = tf.nn.dropout(
                self.y_first_order, self.dropout_keep_fm[0])  # None * F

            # ---------- element_wise ---------------
            element_wise_product_list = []
            for i in range(self.field_size):
                for j in range(i + 1, self.field_size):
                    element_wise_product_list.append(
                        tf.multiply(self.embeddings[:, i, j, :],
                                    self.embeddings[:, j, i, :]))  # None * K

            self.element_wise_product = tf.stack(
                element_wise_product_list)  # (F * F - 1 / 2) * None * K
            self.element_wise_product = tf.transpose(
                self.element_wise_product,
                perm=[1, 0, 2],
                name='element_wise_product')  # None * (F * F - 1 / 2) *  K
            self.element_wise_product = tf.nn.dropout(
                self.element_wise_product, self.dropout_keep_fm[1])  # None * K

            if self.use_attention:
                # attention part
                num_interactions = int(self.field_size *
                                       (self.field_size - 1) / 2)
                # wx+b -> relu(wx+b) -> h*relu(wx+b)
                self.attention_wx_plus_b = tf.reshape(
                    tf.add(
                        tf.matmul(
                            tf.reshape(self.element_wise_product,
                                       shape=(-1, self.embedding_size)),
                            self.weights['attention_w']),
                        self.weights['attention_b']),
                    shape=[-1, num_interactions,
                           self.attention_size])  # N * ( F * F - 1 / 2) * A

                self.attention_exp = tf.exp(
                    tf.reduce_sum(tf.multiply(
                        tf.nn.relu(self.attention_wx_plus_b),
                        self.weights['attention_h']),
                                  axis=2,
                                  keep_dims=True))  # N * ( F * F - 1 / 2) * 1

                self.attention_exp_sum = tf.reduce_sum(
                    self.attention_exp, axis=1, keep_dims=True)  # N * 1 * 1

                self.attention_out = tf.div(
                    self.attention_exp,
                    self.attention_exp_sum,
                    name='attention_out')  # N * ( F * F - 1 / 2) * 1

                self.attention_x_product = tf.reduce_sum(tf.multiply(
                    self.attention_out, self.element_wise_product),
                                                         axis=1,
                                                         name='afm')  # N * K

                self.second_order_part_sum = tf.matmul(
                    self.attention_x_product,
                    self.weights['attention_p'])  # N * 1
            else:
                self.second_order_part_sum = tf.reshape(tf.reduce_sum(
                    self.element_wise_product, axis=[1, 2]),
                                                        shape=[-1, 1])

            # ---------- Deep component ----------
            self.y_deep = tf.reshape(self.embeddings,
                                     shape=[
                                         -1, self.field_size *
                                         self.field_size * self.embedding_size
                                     ])  # None * (F*F*K)
            # self.y_deep = tf.nn.dropout(self.y_deep, self.dropout_keep_deep[0])  # 这一行存疑,在输入层是否需要dropout
            for i in range(0, len(self.deep_layers)):
                self.y_deep = tf.add(
                    tf.matmul(self.y_deep, self.weights["layer_%d" % i]),
                    self.weights["bias_%d" % i])  # None * layer[i] * 1
                if self.batch_norm:
                    self.y_deep = self.batch_norm_layer(
                        self.y_deep,
                        train_phase=self.train_phase,
                        scope_bn="bn_%d" % i)  # None * layer[i] * 1
                self.y_deep = self.deep_layers_activation(self.y_deep)
                self.y_deep = tf.nn.dropout(
                    self.y_deep,
                    self.dropout_keep_deep[1 +
                                           i])  # dropout at each Deep layer

            # ---------- DeepFFM ----------
            if self.use_ffm and self.use_deep:
                concat_input = tf.concat([
                    self.y_first_order, self.second_order_part_sum, self.y_deep
                ],
                                         axis=1)
            elif self.use_ffm:
                concat_input = tf.concat(
                    [self.y_first_order, self.second_order_part_sum], axis=1)
            elif self.use_deep:
                concat_input = self.y_deep
            self.out = tf.add(
                tf.matmul(concat_input, self.weights["concat_projection"]),
                self.weights["concat_bias"])

            # loss
            if self.loss_type == "logloss":
                self.out = tf.nn.sigmoid(self.out)
                self.loss = tf.losses.log_loss(self.label, self.out)
            elif self.loss_type == "mse":
                self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
            # l2 regularization on weights
            if self.l2_reg > 0:
                self.loss += tf.contrib.layers.l2_regularizer(self.l2_reg)(
                    self.weights["concat_projection"])
                if self.use_deep:
                    for i in range(len(self.deep_layers)):
                        self.loss += tf.contrib.layers.l2_regularizer(
                            self.l2_reg)(self.weights["layer_%d" % i])

            # optimizer
            if self.optimizer_type == "adam":
                self.optimizer = tf.train.AdamOptimizer(
                    learning_rate=self.learning_rate,
                    beta1=0.9,
                    beta2=0.999,
                    epsilon=1e-8).minimize(self.loss)
            elif self.optimizer_type == "adagrad":
                self.optimizer = tf.train.AdagradOptimizer(
                    learning_rate=self.learning_rate,
                    initial_accumulator_value=1e-8).minimize(self.loss)
            elif self.optimizer_type == "gd":
                self.optimizer = tf.train.GradientDescentOptimizer(
                    learning_rate=self.learning_rate).minimize(self.loss)
            elif self.optimizer_type == "momentum":
                self.optimizer = tf.train.MomentumOptimizer(
                    learning_rate=self.learning_rate,
                    momentum=0.95).minimize(self.loss)

            # init
            self.saver = tf.train.Saver()
            init = tf.global_variables_initializer()
            self.sess = self._init_session()
            self.sess.run(init)

            # number of params
            total_parameters = 0
            for variable in self.weights.values():
                shape = variable.get_shape()
                variable_parameters = 1
                for dim in shape:
                    variable_parameters *= dim.value
                total_parameters += variable_parameters
            if self.verbose > 0:
                logging.warning("#params: %d" % total_parameters)
Exemplo n.º 41
0
def create_computational_graph(n, N_hat, net_params, num_dt = 10, method = 'RK4', gamma = 1e-5, beta = 1e-8, weight_decay = 'exp', decay_const = 0.9):

    assert(n == N_hat.shape[0])
    m = N_hat.shape[1]

    ###########################################################################
    #
    # Placeholders for initial condition
    #
    ###########################################################################
    Y_0 = tf.placeholder(tf.float32, [n,None], name = "Y_0")  # noisy measurements of state
    T_0 = tf.placeholder(tf.float32, [1,None], name = "T_0")  # time

    ###########################################################################
    #
    # Placeholders for true forward and backward predictions
    #
    ###########################################################################
    true_forward_Y = []
    true_backward_Y = []

    for j in range(num_dt):
        true_forward_Y.append(tf.placeholder(tf.float32, [n,None], name = "Y"+str(j+1)+"_true"))
        true_backward_Y.append(tf.placeholder(tf.float32, [n,None], name = "Yn"+str(j+1)+"_true"))

    H = tf.placeholder(tf.float32, [1,m-1], name = "H") # timestep

    ###########################################################################
    #
    #  Forward and backward predictions of true state
    #
    ###########################################################################

    (weights, biases, N) = net_params
    X_0 = tf.subtract(Y_0, tf.slice(N, [0,num_dt],[n,m-2*num_dt]))  # estimate of true state

    pred_forward_X = [RK_timestepper(X_0, T_0, simple_net, tf.slice(H, [0,num_dt],[1,m-2*num_dt]), \
        weights, biases, method = method)]
    pred_backward_X = [RK_timestepper(X_0, T_0, simple_net, tf.slice(H, [0,num_dt-1],[1,m-2*num_dt]), \
        weights, biases, method = method, direction = 'B')]

    for j in range(1,num_dt):
        pred_forward_X.append(RK_timestepper(pred_forward_X[-1], T_0, simple_net, tf.slice(H, [0,num_dt+j],[1,m-2*num_dt]), \
            weights, biases, method = method))
        pred_backward_X.append(RK_timestepper(pred_backward_X[-1], T_0, simple_net, tf.slice(H, [0,num_dt-1-j],[1,m-2*num_dt]), \
            weights, biases, method = method, direction = 'B'))
        
    ###########################################################################
    #
    #  Forward and backward predictions of measured (noisy) state
    #
    ###########################################################################

    pred_forward_Y = [pred_forward_X[j] + tf.slice(N, [0,num_dt+1+j],[n,m-2*num_dt]) for j in range(num_dt)]
    pred_backward_Y = [pred_backward_X[j] + tf.slice(N, [0,num_dt-1-j],[n,m-2*num_dt]) for j in range(num_dt)]

    ###########################################################################
    #
    #  Set up cost function
    #
    ###########################################################################
    
    if weight_decay == 'linear': output_weights = [(1+j)**-1 for j in range(num_dt)] # linearly decreasing importance 
    else: output_weights = [decay_const**j for j in range(num_dt)] # exponentially decreasing importance 

    forward_fidelity = tf.reduce_sum([w*tf.losses.mean_squared_error(true,pred) \
                          for (w,true,pred) in zip(output_weights,true_forward_Y,pred_forward_Y)])

    backward_fidelity = tf.reduce_sum([w*tf.losses.mean_squared_error(true,pred) \
                          for (w,true,pred) in zip(output_weights,true_backward_Y,pred_backward_Y)])

    fidelity = tf.add(forward_fidelity, backward_fidelity)

    # Regularizer for NN weights
    weights_regularizer = tf.reduce_mean([tf.nn.l2_loss(W) for W in weights])

    # Regularizer for explicit noise term
    noise_regularizer = tf.nn.l2_loss(N)

    # Weighted sum of individual cost functions
    cost = tf.reduce_sum(fidelity + beta*weights_regularizer + gamma*noise_regularizer)

    # BFGS optimizer via scipy
    optimizer = tf.contrib.opt.ScipyOptimizerInterface(cost, options={'maxiter': 50000, 
                                                                      'maxfun': 50000,
                                                                      'ftol': 1e-15, 
                                                                      'gtol' : 1e-11,
                                                                      'eps' : 1e-12,
                                                                      'maxls' : 100})

    placeholders = {'Y_0': Y_0,
                    'T_0': T_0,
                    'true_forward_Y': true_forward_Y,
                    'true_backward_Y': true_backward_Y,
                    'H': H}

    return optimizer, placeholders
Exemplo n.º 42
0
    def _image_preprocessingOfTFRecord(self, image):

        # 이미지를 읽는다.
        feature = {
            'image_left': tf.FixedLenFeature([], tf.string),
            'image_right': tf.FixedLenFeature([], tf.string),
            'height': tf.FixedLenFeature([], tf.int64),
            'width': tf.FixedLenFeature([], tf.int64),
            'depth': tf.FixedLenFeature([], tf.int64)
        }

        parser = tf.parse_single_example(image, features=feature)
        img_decoded_raw_left = tf.decode_raw(parser['image_left'], tf.float32)
        img_decoded_raw_right = tf.decode_raw(parser['image_right'],
                                              tf.float32)
        height = tf.cast(parser['height'], tf.int32)
        width = tf.cast(parser['width'], tf.int32)
        depth = tf.cast(parser['depth'], tf.int32)

        # 아래와 같이 shape을 지정해주는 코드작성이 필요하다.
        iL = tf.reshape(img_decoded_raw_left, (height, width, depth))
        iR = tf.reshape(img_decoded_raw_right, (height, width, depth))

        # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.
        iL_scaled = tf.subtract(tf.divide(iL, 127.5),
                                1.0)  # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.
        iR_scaled = tf.subtract(tf.divide(iR, 127.5),
                                1.0)  # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.

        input = iL_scaled
        label = iR_scaled
        '''
        논문에서...
        Random jitter was applied by resizing the 256 x 256 input images to 286 x 286
        and then randomly cropping back to size 256 x 256
        '''
        # Train Dataset 에서만 동작하게 하기 위함
        if self.use_TrainDataset:
            # 이미지를 키운다
            expanded_area = tf.random_uniform(
                (1, ), 0, 31, dtype=tf.int32)[0]  # 0 ~ 30 의 값으로 키워서 자른다.(랜덤)
            left = tf.random_uniform((1, ), 0, 1, dtype=tf.float32)[0]
            right = tf.random_uniform((1, ), 0, 1, dtype=tf.float32)[0]
            '''
            주의 
              BILINEAR = 0 -> 가장 가까운 화소값을 사용
              NEAREST_NEIGHBOR = 1 -> 인접한 4개 화소의 화소값과 거리비를 사용하여 결정
              BICUBIC = 2 -> 인접한 16개 화소의 화소밗과 거리에 따른 가중치의 곱을 사용
              AREA = 3 -> 사이즈 줄일때 사용
            '''
            iL_resized, iR_resized = tf.cond(tf.less(left, right), \
                                             lambda: (tf.image.resize_images(images=input,
                                                                             size=(tf.shape(input)[
                                                                                       0] + expanded_area,
                                                                                   tf.shape(input)[
                                                                                       1] + expanded_area),
                                                                             method=1),
                                                      tf.image.resize_images(images=label,
                                                                             size=(tf.shape(label)[
                                                                                       0] + expanded_area,
                                                                                   tf.shape(label)[
                                                                                       1] + expanded_area),
                                                                             method=1)), \
                                             lambda: (input, label))

            # 이미지를 원본 크기로 자른다.
            concat_resized = tf.concat(values=[iL_resized, iR_resized],
                                       axis=-1)
            concat_cropped = tf.random_crop(
                concat_resized,
                size=(tf.shape(iL_scaled)[0], tf.shape(iL_scaled)[1],
                      tf.shape(concat_resized)[-1]))
            iL_random_crop, iR_random_crop = tf.split(concat_cropped,
                                                      2,
                                                      axis=-1)
            input = iL_random_crop
            label = iR_random_crop

        if self.AtoB:
            return input, label
        else:
            return label, input
Exemplo n.º 43
0
    def _image_preprocessingOfBasic(self, image):

        # 이미지를 읽는다.
        tensor_name = tf.read_file(image)
        tensor_image = tf.image.decode_image(tensor_name, channels=3)
        # tf.image.decode_image는 shape 정보를 반환하지 못하므로, 아래의 코드를 꼭 작성해야한다.
        tensor_image.set_shape([None, None, 3])
        iL, iR = tf.split(tf.cast(tensor_image, tf.float32), 2, axis=1)

        # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.
        iL_scaled = tf.subtract(tf.divide(iL, 127.5),
                                1.0)  # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.
        iR_scaled = tf.subtract(tf.divide(iR, 127.5),
                                1.0)  # gerator의 활성화 함수가 tanh이므로, 스케일을 맞춰준다.

        input = iL_scaled
        label = iR_scaled
        '''
        논문에서...
        Random jitter was applied by resizing the 256 x 256 input images to 286 x 286
        and then randomly cropping back to size 256 x 256
        '''
        # Train Dataset 에서만 동작하게 하기 위함
        if self.use_TrainDataset:
            # 이미지를 키운다
            expanded_area = tf.random_uniform(
                (1, ), 0, 31, dtype=tf.int32)[0]  # 0 ~ 30 의 값으로 키워서 자른다.(랜덤)
            left = tf.random_uniform((1, ), 0, 1, dtype=tf.float32)[0]
            right = tf.random_uniform((1, ), 0, 1, dtype=tf.float32)[0]
            '''
            주의 
              BILINEAR = 0 -> 가장 가까운 화소값을 사용
              NEAREST_NEIGHBOR = 1 -> 인접한 4개 화소의 화소값과 거리비를 사용하여 결정
              BICUBIC = 2 -> 인접한 16개 화소의 화소밗과 거리에 따른 가중치의 곱을 사용
              AREA = 3 -> 사이즈 줄일때 사용
            '''
            iL_resized, iR_resized = tf.cond(tf.less(left, right), \
                                             lambda: (tf.image.resize_images(images=input,
                                                                             size=(tf.shape(input)[
                                                                                       0] + expanded_area,
                                                                                   tf.shape(input)[
                                                                                       1] + expanded_area),
                                                                             method=1),
                                                      tf.image.resize_images(images=label,
                                                                             size=(tf.shape(label)[
                                                                                       0] + expanded_area,
                                                                                   tf.shape(label)[
                                                                                       1] + expanded_area),
                                                                             method=1)), \
                                             lambda: (input, label))

            # 이미지를 원본 크기로 자른다.
            concat_resized = tf.concat(values=[iL_resized, iR_resized],
                                       axis=-1)
            concat_cropped = tf.random_crop(
                concat_resized,
                size=(tf.shape(iL_scaled)[0], tf.shape(iL_scaled)[1],
                      tf.shape(concat_resized)[-1]))
            iL_random_crop, iR_random_crop = tf.split(concat_cropped,
                                                      2,
                                                      axis=-1)
            input = iL_random_crop
            label = iR_random_crop

        else:
            '''
            주의 
              BILINEAR = 0 -> 가장 가까운 화소값을 사용
              NEAREST_NEIGHBOR = 1 -> 인접한 4개 화소의 화소값과 거리비를 사용하여 결정
              BICUBIC = 2 -> 인접한 16개 화소의 화소밗과 거리에 따른 가중치의 곱을 사용
              AREA = 3 -> 사이즈 줄일때 사용
            '''
            # 이미지 사이즈를 self.height_size x self.width_size 으로 조정한다.
            input = tf.image.resize_images(input,
                                           size=(self.height_size,
                                                 self.width_size),
                                           method=2)
            # 이미지 사이즈를 self.height_size x self.width_size 으로 조정한다.
            label = tf.image.resize_images(label,
                                           size=(self.height_size,
                                                 self.width_size),
                                           method=2)

        if self.AtoB:
            return input, label
        else:
            return label, input
Exemplo n.º 44
0
                         maxval=1,
                         dtype=tf.float32,
                         name='random-op')

# create variables initialized to random values specified by rand operation
X = tf.Variable(rand, name='X')
Y = tf.Variable(rand, name='Y')
'''
    Objective: if x(i,j)>y(i,j) then z(i,j) = x(i,j)+y(i,j)
               if x(i,j)<y(i,j) then z(i,j) = x(i,j)-y(i,j)
               if x(i,j)=y(i,j) then z(i,j) = 42
'''

# define operation to execute for each instance of predicate x>y
T1 = tf.where(tf.greater(X, Y), tf.add(X, Y), X)
T2 = tf.where(tf.less(T1, Y), tf.subtract(T1, Y), X)
Z = tf.where(tf.equal(T2, Y), tf.ones_like(X) * 42, T2)

# init computation session
with tf.Session() as sess:

    # init tensorboard logs
    sfw = tf.summary.FileWriter(os.getcwd(), sess.graph)

    # init variables
    sess.run(X.initializer)
    sess.run(Y.initializer)

    # execute the X op to get values
    valX = sess.run(X)
Exemplo n.º 45
0
def f2():
    return tf.cast(tf.subtract(x, y), tf.float32)
Exemplo n.º 46
0
f2 = tf.Variable([[2., 3.], [1., 2.], [3., 1.]])
vector1 = tf.constant([3., 3.])  #这里只有一对中括号 [],就是向量
vector2 = tf.constant([1., 2.])
result3 = tf.multiply(vector1, vector2)  #向量乘法。tf.multiply()
result4 = tf.multiply(vector2, vector1)

sess = tf.Session()
sess.run(tf.global_variables_initializer())
r3 = sess.run(result3)
r4 = sess.run(result4)
print(r3, '\n', r4)

#tf.add 加法函数
print("加法", sess.run(tf.add(a, b)))  # 求和
#tf.subtract 减法函数
print("减法", sess.run(tf.subtract(a, c1)))  #减法
#tf.multiply 乘法函数
print("乘法", sess.run(tf.multiply(a, b)))  # 乘积
#tf.divde   除法函数
print("除法", sess.run(tf.divide(a, 2.0)))  #除法
print("除法", sess.run(tf.div(a, 2.0)))  #除法

#tf.assign 赋值函数
for i in range(101):
    sess.run(tf.assign(sum, tf.add(sum, i)))
print('1到100的和:', sess.run(sum))

for i in range(1, 11):
    sess.run(tf.assign(result, tf.multiply(result, i)))
print('1到10的乘积:', sess.run(result))
Exemplo n.º 47
0
    def __init__(self,
                 n_input,
                 kernel_size,
                 n_hidden,
                 reg_const1=1.0,
                 reg_const2=1.0,
                 reg=None,
                 batch_size=256,
                 max_iter=10,
                 denoise=False,
                 model_path=None,
                 logs_path='./logs'):
        # n_hidden is a arrary contains the number of neurals on every layer
        tf.reset_default_graph()
        self.n_input = n_input
        self.n_hidden = n_hidden
        self.reg = reg
        self.model_path = model_path
        self.kernel_size = kernel_size
        self.iter = 0
        self.batch_size = batch_size
        # weights = self._initialize_weights()
        # # Variable initialization
        weights = dict()
        with tf.variable_scope('weight', reuse=tf.AUTO_REUSE):
            weights['enc_w0'] = tf.get_variable(
                "enc_w0",
                shape=[
                    self.kernel_size[0], self.kernel_size[0], 1,
                    self.n_hidden[0]
                ],
                initializer=layers.xavier_initializer_conv2d(),
                regularizer=self.reg)
            weights['enc_b0'] = tf.Variable(
                tf.zeros([self.n_hidden[0]], dtype=tf.float32))

            weights['dec_w0'] = tf.get_variable(
                "dec_w0",
                shape=[
                    self.kernel_size[0], self.kernel_size[0], 1,
                    self.n_hidden[0]
                ],
                initializer=layers.xavier_initializer_conv2d(),
                regularizer=self.reg)
            weights['dec_b0'] = tf.Variable(tf.zeros([1], dtype=tf.float32))

        self.max_iter = max_iter
        # model
        self.x = tf.placeholder(tf.float32,
                                [None, self.n_input[0], self.n_input[1], 1])
        self.learning_rate = tf.placeholder(tf.float32, [])

        if denoise == False:
            x_input = self.x
            latent, shape = self.encoder(x_input, weights)

        else:
            x_input = tf.add(
                self.x,
                tf.random_normal(shape=tf.shape(self.x),
                                 mean=0,
                                 stddev=0.2,
                                 dtype=tf.float32))

            latent, shape = self.encoder(x_input, weights)
        self.z_conv = tf.reshape(latent, [batch_size, -1])
        self.z_ssc, Coef = self.selfexpressive_moduel(batch_size)
        self.Coef = Coef
        latent_de_ft = tf.reshape(self.z_ssc, tf.shape(latent))
        self.x_r_ft = self.decoder(latent_de_ft, weights, shape)

        self.saver = tf.train.Saver([
            v for v in tf.trainable_variables()
            if not (v.name.startswith("Coef"))
        ])

        self.cost_ssc = 0.5 * tf.reduce_sum(
            tf.pow(tf.subtract(self.z_conv, self.z_ssc), 2))
        self.recon_ssc = tf.reduce_sum(
            tf.pow(tf.subtract(self.x_r_ft, self.x), 2.0))
        self.reg_ssc = tf.reduce_sum(tf.pow(self.Coef, 2))
        tf.summary.scalar("ssc_loss", self.cost_ssc)
        tf.summary.scalar("reg_lose", self.reg_ssc)

        self.loss_ssc = self.cost_ssc * reg_const2 + reg_const1 * self.reg_ssc + self.recon_ssc

        self.merged_summary_op = tf.summary.merge_all()
        self.optimizer_ssc = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate).minimize(self.loss_ssc)
        self.init = tf.global_variables_initializer()
        self.sess = tf.InteractiveSession()
        self.sess.run(self.init)
        self.summary_writer = tf.summary.FileWriter(
            logs_path, graph=tf.get_default_graph())
Exemplo n.º 48
0
 def ms_error(y_target, y_pre):
     return tf.square(tf.subtract(y_target, y_pre))
Exemplo n.º 49
0
l1w, l2w, l3w, l4w, l5w, l6w = l1.getw(), l2.getw(), l3.getw(), l4.getw(
), l5.getw(), l6.getw()

# Make the graph
x = tf.placeholder(shape=[None, 512, 512, 1], dtype="float")
y = tf.placeholder(shape=[None, 512, 512, 1], dtype="float")

layer1 = l1.feed_forward(x, x)
layer2 = l2.feed_forward(layer1, x)
layer3 = l3.feed_forward(layer2, x)

layer4 = l4.feed_forward(layer3, x)
layer5 = l5.feed_forward(layer4, x)
layer6 = l6.feed_forward(layer5, x)

loss = tf.reduce_sum(tf.square(tf.subtract(layer6, y) * 0.5))

grad_6, g6w = l6.backprop(tf.subtract(layer6, y), x)
grad_5, g5w = l5.backprop(grad_6, x)
grad_4, g4w = l4.backprop(grad_5, x)

grad_3, g3w = l3.backprop(grad_4, x)
grad_2, g2w = l2.backprop(grad_3, x)
grad_1, g1w = l1.backprop(grad_2, x)

update = g1w + g2w + g3w + g4w + g5w + g6w

# Make the Session
total_cost = 0
with tf.Session(config=config) as sess:
Exemplo n.º 50
0
    def get_q_values_op(self,
                        state,
                        goal_state,
                        past_a,
                        seq_len,
                        h_state,
                        scope,
                        reuse=False):
        """
        Returns Q values for all actions

        Args:
            state: (tf tensor) 
                shape = (batch_size, seq_len, img_w, img_h, nchannel)
            goal_state: (tf tensor)
                shape = (batch_size, 1, img_w, img_h, nchannel)
            past_a: (tf tensor)
                shape = (batch_size*seq_len,)
            seq_len: (tf tensor)
                shape = (batch_size,)
            h_state: (tf tensor) 
                shape = (batch_size, h_size)
            scope: (string) scope name, that specifies if target network or not
            reuse: (bool) reuse of variables in the scope

        Returns:
            out: (tf tensor) of shape = (batch_size * seq_len, num_actions)
            h_state_out: (tf tensor) of shape = (batch_size, h_size)
        """
        num_actions = self.env.agent.num_actions
        h_size = self.config.h_size
        max_seq_len = tf.shape(state)[1]
        past_a = tf.reshape(tf.one_hot(past_a, num_actions),
                            shape=(-1, max_seq_len, num_actions))
        state_shape = list([
            self.env.args.visible_radius_unit_front + 1,
            2 * self.env.args.visible_radius_unit_side + 1,
            len(self.env.state.xmap.item_class_id) + 1
        ])
        state = tf.reshape(state,
                           shape=[
                               -1, max_seq_len,
                               state_shape[0] * state_shape[1] * state_shape[2]
                           ])
        goal_state = tf.reshape(
            goal_state,
            shape=[-1, max_seq_len, state_shape[0] * state_shape[1]])
        #goal_state = tf.reshape(goal_state, shape=[-1, 1, state_shape[2]])
        #goal_state = tf.tile(goal_state, multiples=[1, max_seq_len, 1])
        with tf.variable_scope(scope, reuse=reuse):
            state = layers.fully_connected(
                state,
                200,
                activation_fn=tf.nn.relu,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            state = layers.fully_connected(
                state,
                100,
                activation_fn=tf.nn.relu,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            goal_state = layers.fully_connected(
                goal_state,
                100,
                activation_fn=tf.nn.relu,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            goal_state = layers.fully_connected(
                goal_state,
                50,
                activation_fn=tf.nn.relu,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            lstm_cell = tf.nn.rnn_cell.LSTMCell(num_units=h_size)
            out = tf.concat([state, goal_state, past_a], axis=2)
            out = layers.fully_connected(
                out,
                100,
                activation_fn=tf.nn.relu,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            out, h_state_out = tf.nn.dynamic_rnn(inputs=out,
                                                 cell=lstm_cell,
                                                 sequence_length=seq_len,
                                                 dtype=tf.float32,
                                                 initial_state=h_state)
            # out here has shape (batch_size, state_history, h_size)
            # h_state_out has shape (batch_size, h_size)
            #out = layers.fully_connected(tf.reshape(out, shape=[-1,h_size]), num_actions, activation_fn = None, weights_initializer=layers.xavier_initializer(), biases_initializer=tf.zeros_initializer())
            out = tf.reshape(out, shape=[-1, h_size])
            streamA, streamV = tf.split(out, 2, axis=1)
            advantage = layers.fully_connected(
                streamA,
                num_actions,
                activation_fn=None,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            value = layers.fully_connected(
                streamV,
                1,
                activation_fn=None,
                weights_initializer=layers.xavier_initializer(),
                biases_initializer=tf.zeros_initializer())
            out = value + tf.subtract(
                advantage, tf.reduce_mean(advantage, axis=1, keep_dims=True))

        return out, h_state_out
Exemplo n.º 51
0
    GRAPH_NAME = args.graph_name
    OUT_DIR_NAME = args.out_dir_name
    IN_DIR_NAME = args.in_dir_name
    PB_DIR_NAME = args.chkp_dir_name
    CHKP_DIR_NAME = os.path.join(args.chkp_dir_name, 'chkp')

    # Subtracts x2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    x_train = [[1, 2, 0, -1, 4, 5, 7, -12, 3], [2, 4, 9, -1, 0, 5, -17, 4, 2]]

    y_train = [[0, 0, -3, -5, -1, -1, 0, -20, -6],
               [1, 2, 6, -5, -5, -1, -24, -4, -7]]

    # Model input and output
    x1 = tf.placeholder(dtype=tf.float32, name='inputx')
    x2 = tf.Variable(tf.random_normal([1, 9]), name='elemwiseSub')
    model = tf.subtract(x1, x2, name='outputx')
    y = tf.placeholder(dtype=tf.float32)

    # loss
    loss = tf.reduce_sum(tf.square(model - y) / 18,
                         name='loss')  # sum of the squares

    # optimizer
    optimizer = tf.train.GradientDescentOptimizer(0.01)
    train = optimizer.minimize(loss)

    # training loop
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    for i in range(1000):
        sess.run(train, {x1: x_train, y: y_train})
Exemplo n.º 52
0
def ms_errro(labels, logits):
    return tf.square(tf.subtract(labels, logits))
Exemplo n.º 53
0
    def create_base(self,
                    images,
                    labels_one_hot,
                    scope='AttentionOcr_v1',
                    reuse=None):
        """Creates a base part of the Model (no gradients, losses or summaries).

    Args:
      images: A tensor of shape [batch_size, height, width, channels] with pixel
        values in the range [0.0, 1.0].
      labels_one_hot: Optional (can be None) one-hot encoding for ground truth
        labels. If provided the function will create a model for training.
      scope: Optional variable_scope.
      reuse: whether or not the network and its variables should be reused. To
        be able to reuse 'scope' must be given.

    Returns:
      A named tuple OutputEndpoints.
    """
        logging.debug('images: %s', images)
        is_training = labels_one_hot is not None

        # Normalize image pixel values to have a symmetrical range around zero.
        images = tf.subtract(images, 0.5)
        images = tf.multiply(images, 2.5)

        with tf.compat.v1.variable_scope(scope, reuse=reuse):
            views = tf.split(value=images,
                             num_or_size_splits=self._params.num_views,
                             axis=2)
            logging.debug('Views=%d single view: %s', len(views), views[0])

            nets = [
                self.conv_tower_fn(v, is_training, reuse=(i != 0))
                for i, v in enumerate(views)
            ]
            logging.debug('Conv tower: %s', nets[0])

            nets = [self.encode_coordinates_fn(net) for net in nets]
            logging.debug('Conv tower w/ encoded coordinates: %s', nets[0])

            net = self.pool_views_fn(nets)
            logging.debug('Pooled views: %s', net)

            chars_logit = self.sequence_logit_fn(net, labels_one_hot)
            logging.debug('chars_logit: %s', chars_logit)

            predicted_chars, chars_log_prob, predicted_scores = (
                self.char_predictions(chars_logit))
            if self._charset:
                character_mapper = CharsetMapper(self._charset)
                predicted_text = character_mapper.get_text(predicted_chars)
            else:
                predicted_text = tf.constant([])

            text_log_prob, predicted_length = null_based_length_prediction(
                chars_log_prob, self._params.null_code)
            predicted_conf = lookup_indexed_value(predicted_length,
                                                  text_log_prob)
            # Convert predicted confidence from sum of logs to geometric mean
            normalized_seq_conf = tf.exp(tf.divide(
                predicted_conf,
                tf.cast(predicted_length + 1, predicted_conf.dtype)),
                                         name='normalized_seq_conf')
            predicted_conf = tf.identity(predicted_conf, name='predicted_conf')
            predicted_text = tf.identity(predicted_text, name='predicted_text')
            predicted_length = tf.identity(predicted_length,
                                           name='predicted_length')

        return OutputEndpoints(chars_logit=chars_logit,
                               chars_log_prob=chars_log_prob,
                               predicted_chars=predicted_chars,
                               predicted_scores=predicted_scores,
                               predicted_length=predicted_length,
                               predicted_text=predicted_text,
                               predicted_conf=predicted_conf,
                               normalized_seq_conf=normalized_seq_conf)
Exemplo n.º 54
0
    def train_model(self,
                    data_seq,
                    val_input,
                    val_output,
                    leanring_rate,
                    no_epochs,
                    batch_size,
                    log_info,
                    use_pretrain=False):
        # with tf.device("/gpu:1"):
        with tf.name_scope("Input") as scope:
            x_image = tf.placeholder(tf.float32, [None, 188, 620, 3])
            x_next_image = tf.placeholder(tf.float32, [None, 188, 620, 3])
            y = tf.placeholder(tf.float32, [None, 3])

        with tf.name_scope("Network") as scope:
            feature_1 = self.convnet_1(x_image)
            feature_2 = self.convnet_2(x_next_image)
            final_output = self.fc_layers(feature_1, feature_2)

        with tf.name_scope("Training") as scope:
            data_loss = tf.reduce_mean(tf.square(tf.subtract(final_output, y)))
            loss = data_loss
            train_op = tf.train.AdamOptimizer(leanring_rate).minimize(loss)

        with tf.name_scope("Loss_Monitoring") as scope:
            train_sum_op = tf.summary.scalar("Training Loss", loss)
            merged_sum = tf.summary.merge_all()
            # val_sum_op = tf.summary.scalar("Vaildation", loss)

        if len(glob.glob("./logs/" + log_info + "/model*")) != 0:
            tf.reset_default_graph()
            saver = tf.train.import_meta_graph("./logs/" + log_info +
                                               "/model.meta")
            saver.restore(sess, "./logs/" + log_info + "/model")
            print "Continuing Training On Previously Saved Weights."
        else:
            init = tf.global_variables_initializer()
            sess.run(init)
            saver = tf.train.Saver()
            if use_pretrain:
                self.load_weights('./vgg-siamese/vgg16_weights.npz', sess)
                print " Training Using Pre-trained VGG Weights."
            else:
                print "Training Started From Beginning. No Pre-Trained Weights Used."

        writer = tf.summary.FileWriter("./logs/" + log_info)
        writer.add_graph(sess.graph)

        total_parameters = 0
        for variable in tf.trainable_variables():
            name = variable.name
            shape = variable.get_shape()
            print name, shape
            variable_parametes = 1
            for dim in shape:
                variable_parametes *= dim.value
            total_parameters += variable_parametes
        print total_parameters

        file = open("./logs/" + log_info + '/loss_record.csv', 'a')
        file_writer = csv.writer(file, delimiter=',')
        data_path = "./training_testing_data/620x188_images/"
        # print data_seq
        itr = 0
        for epoch in range(no_epochs):
            for seq in data_seq:
                # print "Loading Data", seq
                train_full = np.load(data_path + seq + '.npy')
                print seq, "Loaded.", train_full.shape[0], "Training Examples."
                input_data = train_full[:, 0:2]
                output_data = train_full[:, 2]
                print input_data.shape, output_data.shape
                input_data, output_data = shuffle(input_data, output_data)
                eval_data, eval_labels = shuffle(val_input, val_output)
                for k in xrange(0, len(input_data), batch_size):
                    batch_input = input_data[k:k + batch_size]
                    batch_output = output_data[k:k + batch_size]
                    batch_input = np.asarray([[i[0], i[1]]
                                              for i in batch_input])
                    batch_output = np.asarray([[i[0], i[1], i[2]]
                                               for i in batch_output])
                    # print batch_output[0]
                    # cv2.imshow("Input_Image", batch_input[0,0])
                    # cv2.imshow("Input_Image_Next", batch_input[0,1])
                    # cv2.waitKey(100)
                    train_loss, _, summary = sess.run(
                        [loss, train_op, merged_sum], {
                            x_image: batch_input[:, 0],
                            x_next_image: batch_input[:, 1],
                            y: batch_output
                        })
                    # itr = epoch*len(input_data)/batch_size + k/batch_size
                    itr = itr + 1
                    writer.add_summary(summary)
                    if (itr % 100 == 0):
                        writer.add_summary(summary, itr)  # append summary
                        print "Epoch:-", epoch, "Sequence", seq, "Iteration:-", k / batch_size
                        val_loss_tot = 0
                        for j in xrange(0,
                                        len(val_input) - batch_size,
                                        batch_size):
                            val_batch_input = val_input[j:j + batch_size]
                            val_batch_input = np.asarray(
                                [[i[0], i[1]] for i in val_batch_input])
                            val_batch_output = val_output[j:j + batch_size]
                            val_batch_output = np.asarray(
                                [[i[0], i[1], i[2]] for i in val_batch_output])
                            val_loss_tot = val_loss_tot + sess.run(
                                loss, {
                                    x_image: val_batch_input[:, 0],
                                    x_next_image: val_batch_input[:, 1],
                                    y: val_batch_output
                                })

                        val_loss_avg = val_loss_tot / int(
                            len(val_input) / batch_size)

                        write_string = [
                            'Epoch:- ' + str(epoch),
                            "Validation_Loss:- " + str(val_loss_avg),
                            "Training_Loss:- " + str(train_loss)
                        ]
                        file_writer.writerow(write_string)
                        print "Validation Loss:", val_loss_avg
                        print "Training Loss:", train_loss
                if (epoch % 5 == 0):
                    saver.save(sess, "./logs/" + log_info + "/model")
                    print "Checkpoint saved"
Exemplo n.º 55
0
    def __init__(self, params):
        self.params = params

        # mask
        self.numCandidates = tf.placeholder(
            shape=[None, self.params.NUM_ACTIONS], dtype=tf.float32)
        self.mask = tf.cast(self.numCandidates, dtype=tf.bool)
        self.maskNum = tf.cast(self.mask, dtype=tf.float32)
        self.maskBigNeg = tf.subtract(self.maskNum, 1)
        self.maskBigNeg = tf.multiply(self.maskBigNeg, 999999)

        # graph represention
        self.langsVisited = tf.placeholder(shape=[None, 3], dtype=tf.float32)
        self.numActions = tf.placeholder(shape=[None, 1], dtype=tf.float32)

        # link representation
        self.linkSpecificInput = tf.placeholder(shape=[
            None, self.params.NUM_ACTIONS, self.params.NUM_LINK_FEATURES
        ],
                                                dtype=tf.float32)
        print("self.linkSpecific", self.linkSpecificInput.shape)
        self.numLinkFeatures = int(self.linkSpecificInput.shape[2])
        #print("self.numLinkFeatures", type(self.numLinkFeatures), self.numLinkFeatures)

        # batch size
        self.batchSize = tf.shape(self.mask)[0]

        # network
        self.input = tf.concat([self.langsVisited, self.numActions], 1)
        #print("self.input", self.input.shape)

        self.W1 = tf.Variable(
            tf.random_uniform([3 + 1, params.hiddenDim], minval=0, maxval=0))
        self.b1 = tf.Variable(
            tf.random_uniform([1, params.hiddenDim], minval=0, maxval=0))
        self.hidden1 = tf.matmul(self.input, self.W1)
        self.hidden1 = tf.add(self.hidden1, self.b1)
        self.hidden1 = tf.nn.relu(self.hidden1)
        #self.hidden1 = tf.nn.sigmoid(self.hidden1)
        print("self.hidden1", self.hidden1.shape)

        self.W2 = tf.Variable(
            tf.random_uniform([params.hiddenDim, params.linkDim],
                              minval=0,
                              maxval=0))
        self.b2 = tf.Variable(
            tf.random_uniform([1, params.linkDim], minval=0, maxval=0))
        self.hidden2 = tf.matmul(self.hidden1, self.W2)
        self.hidden2 = tf.add(self.hidden2, self.b2)
        #self.hidden2 = tf.nn.relu(self.hidden3)
        #self.hidden2 = tf.nn.sigmoid(self.hidden3)
        print("self.hidden2", self.hidden2.shape)

        # link-specific
        self.WlinkSpecific = tf.Variable(
            tf.random_uniform([self.numLinkFeatures, params.linkDim], 0, 0.01))
        self.blinkSpecific = tf.Variable(
            tf.random_uniform([1, params.linkDim], 0, 0.01))

        #print("self.linkSpecific1", self.linkSpecific.shape)
        self.linkSpecific = tf.reshape(
            self.linkSpecificInput,
            [self.batchSize * self.params.NUM_ACTIONS, self.numLinkFeatures])
        #print("self.linkSpecific2", self.linkSpecific.shape)

        self.linkSpecific = tf.matmul(self.linkSpecific, self.WlinkSpecific)
        #print("self.linkSpecific3", self.linkSpecific.shape)
        self.linkSpecific = tf.add(self.linkSpecific, self.blinkSpecific)
        #self.linkSpecific = tf.nn.relu(self.linkSpecific)
        #self.linkSpecific = tf.nn.sigmoid(self.linkSpecific)
        #print("self.linkSpecific4", self.linkSpecific.shape)
        self.linkSpecific = tf.reshape(
            self.linkSpecific,
            [self.batchSize, self.params.NUM_ACTIONS, params.linkDim])
        #print("self.linkSpecific5", self.linkSpecific.shape)

        # final q-values
        self.logit = tf.reshape(self.hidden2,
                                [self.batchSize, 1, params.linkDim])
        self.logit = tf.multiply(self.linkSpecific, self.logit)
        self.logit = tf.reduce_sum(self.logit, axis=2)

        # softmax
        self.maxLogit = tf.add(self.logit, self.maskBigNeg)
        self.maxLogit = tf.reduce_max(self.maxLogit, axis=1)
        self.maxLogit = tf.reshape(self.maxLogit, [self.batchSize, 1])

        self.smNumer = tf.subtract(self.logit, self.maxLogit)
        self.smNumer = tf.multiply(self.smNumer, self.maskNum)
        self.smNumer = tf.exp(self.smNumer)
        self.smNumer = tf.multiply(self.smNumer, self.maskNum)

        self.smNumerSum = tf.reduce_sum(self.smNumer, axis=1)
        self.smNumerSum = tf.reshape(self.smNumerSum, [self.batchSize, 1])

        self.probs = tf.divide(self.smNumer, self.smNumerSum)
        self.chosenAction = tf.argmax(self.probs, 0)

        # training
        self.reward_holder = tf.placeholder(shape=[None], dtype=tf.float32)
        self.action_holder = tf.placeholder(shape=[None],
                                            dtype=tf.int32)  #  0 or 1

        self.r1 = tf.range(
            0, self.batchSize)  # 0 1 2 3 4 len=length of trajectory
        self.r2 = self.params.NUM_ACTIONS
        self.r3 = self.r1 * self.r2  # 0 2 4 6 8
        self.indexes = self.r3 + self.action_holder  # r3 + 0/1 offset depending on action

        self.o1 = tf.reshape(self.probs, [-1])  # all action probs in 1-d
        self.responsible_outputs = tf.gather(
            self.o1, self.indexes
        )  # the prob of the action. Should have just stored it!? len=length of trajectory

        self.l1 = tf.log(self.responsible_outputs)
        self.l2 = self.l1 * self.reward_holder  # log prob * reward. len=length of trajectory
        self.loss = -tf.reduce_mean(self.l2)  # 1 number

        # calc grads, but don't actually update weights
        tvars = tf.trainable_variables()
        self.gradient_holders = []
        for idx, var in enumerate(
                tvars
        ):  # idx = contiguous int, var = variable shape (4,8) (8,2)
            #print("idx", idx)
            #print("var", var)
            placeholder = tf.placeholder(tf.float32, name=str(idx) + '_holder')
            self.gradient_holders.append(placeholder)
        self.gradients = tf.gradients(
            self.loss,
            tvars)  # grads same shape as gradient_holder0. (4,8) (8,2)

        #self.trainer = tf.train.GradientDescentOptimizer(learning_rate=params.lrn_rate)
        #self.trainer = tf.train.AdagradOptimizer(learning_rate=params.lrn_rate)
        self.trainer = tf.train.AdamOptimizer(learning_rate=params.lrn_rate)
        #self.updateModel = self.trainer.minimize(self.loss)
        self.update_batch = self.trainer.apply_gradients(
            zip(self.gradient_holders, tvars))
Exemplo n.º 56
0
labels_train_final = np.array(
    [1 if y == k1 else -1 for y in labels_train_final])

x_data = tf.placeholder(shape=[None, K], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
print(x_data, y_target)

W = tf.Variable(tf.random_normal(shape=[K, 1]))
b = tf.Variable(tf.random_normal(shape=[1, 1]))

model_output = tf.add(tf.matmul(x_data, W), b)

l2_norm = tf.norm(W)
hinge = tf.reduce_sum(
    tf.maximum(0., tf.subtract(1., tf.multiply(model_output, y_target))))
loss = hinge + l2_norm

optimizer = tf.train.GradientDescentOptimizer(
    learning_rate=0.001).minimize(loss)

labels_train_final = labels_train_final.reshape(labels_train_final.shape[0], 1)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for i in range(500):
        _, loss_ = sess.run([optimizer, loss],
                            feed_dict={
                                x_data: X_train_final,
                                y_target: labels_train_final
def train(tf_seed, np_seed, train_steps, only_finetune, finetune_train_steps, out_steps, summary_steps, checkpoint_steps, step_size_schedule,
          weight_decay, momentum, train_batch_size, epsilon, replay_m, model_dir, source_model_dir, dataset, 
          beta, gamma, disc_update_steps, adv_update_steps_per_iter, disc_layers, disc_base_channels, steps_before_adv_opt, adv_encoder_type, enc_output_activation, 
          sep_opt_version, grad_image_ratio, final_grad_image_ratio, num_grad_image_ratios, normalize_zero_mean, eval_adv_attack, same_optimizer, only_fully_connected, disc_avg_pool_hw, 
          finetuned_source_model_dir, train_finetune_source_model, finetune_img_random_pert, img_random_pert, model_suffix, source_task, **kwargs):
    tf.set_random_seed(tf_seed)
    np.random.seed(np_seed)

    model_dir = model_dir + 'IGAM-%sto%s_b%dupresize_beta_%.3f_gamma_%.3f_disc_update_steps%d_l%dbc%d' % (source_task, dataset, train_batch_size, beta, gamma, disc_update_steps, disc_layers, disc_base_channels)  # TODO Replace with not defaults

    if disc_avg_pool_hw:
        model_dir = model_dir + 'avgpool'

    if img_random_pert:
        model_dir = model_dir + '_imgpert'

    if steps_before_adv_opt != 0:
        model_dir = model_dir + '_advdelay%d' % (steps_before_adv_opt)

    if train_steps != 80000:
        model_dir = model_dir + '_%dsteps' % (train_steps)
    if same_optimizer == False:
        model_dir = model_dir + '_adamDopt'

    if tf_seed != 451760341:
        model_dir = model_dir + '_tf_seed%d' % (tf_seed)
    if np_seed != 216105420:
        model_dir = model_dir + '_np_seed%d' % (np_seed)

    model_dir = model_dir + model_suffix

    # Setting up the data and the model
    data_path = get_path_dir(dataset=dataset, **kwargs)
    if dataset == 'cifar10':
        raw_data = cifar10_input.CIFAR10Data(data_path)
    else:
        raw_data = cifar100_input.CIFAR100Data(data_path)


    global_step = tf.train.get_or_create_global_step()
    increment_global_step_op = tf.assign(global_step, global_step+1)
    reset_global_step_op = tf.assign(global_step, 0)

    full_source_model_x_input = tf.placeholder(tf.float32, shape = [None, 32, 32, 3])
    upresized_full_source_model_x_input = tf.image.resize_images(full_source_model_x_input, size=[64, 64])
    if dataset == 'cifar10':
        source_model = ModelTinyImagenetSourceExtendedLogits(mode='train', dataset=source_task, target_task_class_num=10, train_batch_size=train_batch_size, input_tensor=upresized_full_source_model_x_input)
    elif dataset == 'cifar100':
        source_model = ModelTinyImagenetSourceExtendedLogits(mode='train', dataset=source_task, target_task_class_num=100, train_batch_size=train_batch_size, input_tensor=upresized_full_source_model_x_input)
    model = Model(mode='train', dataset=dataset, train_batch_size=train_batch_size, normalize_zero_mean=normalize_zero_mean)

    # Setting up the optimizers
    boundaries = [int(sss[0]) for sss in step_size_schedule][1:]
    values = [sss[1] for sss in step_size_schedule]
    learning_rate = tf.train.piecewise_constant(tf.cast(global_step, tf.int32), boundaries, values)
    c_optimizer = tf.train.MomentumOptimizer(learning_rate, momentum)
    finetune_optimizer = tf.train.AdamOptimizer(learning_rate = 0.001)
    
    if same_optimizer:
        d_optimizer = tf.train.MomentumOptimizer(learning_rate, momentum)
    else:
        print("Using ADAM opt for DISC model")
        d_optimizer = tf.train.AdamOptimizer(learning_rate = 0.001)

    # Compute input gradient (saliency map)
    input_grad = tf.gradients(model.target_softmax, model.x_input, name="gradients_ig")[0]
    source_model_input_grad = tf.gradients(source_model.target_softmax, full_source_model_x_input, name="gradients_ig_source_model")[0]

    # lp norm diff between input_grad & source_model_input_grad
    input_grad_l2_norm_diff = tf.reduce_mean(tf.reduce_sum(tf.pow(tf.subtract(input_grad, source_model_input_grad), 2.0), keepdims=True))

    # Setting up the discriminator model
    labels_input_grad = tf.zeros( tf.shape(input_grad)[0] , dtype=tf.int64)
    labels_source_model_input_grad = tf.ones( tf.shape(input_grad)[0] , dtype=tf.int64)
    disc_model = IgamConvDiscriminatorModel(mode='train', dataset=dataset, train_batch_size=train_batch_size, image_size=32, num_conv_layers=disc_layers, base_num_channels=disc_base_channels, normalize_zero_mean=normalize_zero_mean,
        x_modelgrad_input_tensor=input_grad, y_modelgrad_input_tensor=labels_input_grad, x_source_modelgrad_input_tensor=source_model_input_grad, y_source_modelgrad_input_tensor=labels_source_model_input_grad, only_fully_connected=only_fully_connected, avg_pool_hw=disc_avg_pool_hw)

    t_vars = tf.trainable_variables()
    C_vars = [var for var in t_vars if 'classifier' in var.name]
    D_vars = [var for var in t_vars if 'discriminator' in var.name]
    source_model_vars = [var for var in t_vars if ('discriminator' not in var.name and 'classifier' not in var.name and 'target_task_logit' not in var.name)]
    source_model_target_logit_vars = [var for var in t_vars if 'target_task_logit' in var.name]
    
    source_model_saver = tf.train.Saver(var_list=source_model_vars)
    finetuned_source_model_vars = source_model_vars + source_model_target_logit_vars
    finetuned_source_model_saver = tf.train.Saver(var_list=finetuned_source_model_vars)

    # Source model finetune optimization
    source_model_finetune_loss = source_model.target_task_mean_xent + weight_decay * source_model.weight_decay_loss
    # Classifier: Optimizing computation
    # total classifier loss: Add discriminator loss into total classifier loss
    total_loss = model.mean_xent + weight_decay * model.weight_decay_loss - beta * disc_model.mean_xent + gamma * input_grad_l2_norm_diff
    
    classification_c_loss = model.mean_xent + weight_decay * model.weight_decay_loss
    adv_c_loss = - beta * disc_model.mean_xent

    # Discriminator: Optimizating computation
    # discriminator loss
    total_d_loss = disc_model.mean_xent + weight_decay * disc_model.weight_decay_loss

    # Finetune source_model
    source_model_new_weights = source_model_target_logit_vars
    finetune_min_step = finetune_optimizer.minimize(source_model_finetune_loss, var_list=source_model_new_weights)

    # Train classifier
    final_grads = c_optimizer.compute_gradients(total_loss, var_list=C_vars)
    no_pert_grad = [(tf.zeros_like(v), v) if 'perturbation' in v.name else (g, v) for g, v in final_grads]
    c_min_step = c_optimizer.apply_gradients(no_pert_grad)

    classification_final_grads = c_optimizer.compute_gradients(classification_c_loss, var_list=C_vars)
    classification_no_pert_grad = [(tf.zeros_like(v), v) if 'perturbation' in v.name else (g, v) for g, v in classification_final_grads]
    c_classification_min_step = c_optimizer.apply_gradients(classification_no_pert_grad)

    # discriminator opt step
    d_min_step = d_optimizer.minimize(total_d_loss, var_list=D_vars)

    # Loss gradients to the model params
    logit_weights = tf.get_default_graph().get_tensor_by_name('classifier/logit/DW:0')
    last_conv_weights = tf.get_default_graph().get_tensor_by_name('classifier/unit_3_4/sub2/conv2/DW:0')
    first_conv_weights = tf.get_default_graph().get_tensor_by_name('classifier/input/init_conv/DW:0')

    model_xent_logit_grad_norm = tf.norm(tf.gradients(model.mean_xent, logit_weights)[0], ord='euclidean')
    
    disc_xent_logit_grad_norm = tf.norm(tf.gradients(disc_model.mean_xent, logit_weights)[0], ord='euclidean')
    
    input_grad_l2_norm_diff_logit_grad_norm = tf.norm(tf.gradients(input_grad_l2_norm_diff, logit_weights)[0], ord='euclidean')

    model_xent_last_conv_grad_norm = tf.norm(tf.gradients(model.mean_xent, last_conv_weights)[0], ord='euclidean')
    disc_xent_last_conv_grad_norm = tf.norm(tf.gradients(disc_model.mean_xent, last_conv_weights)[0], ord='euclidean')
    input_grad_l2_norm_diff_last_conv_grad_norm = tf.norm(tf.gradients(input_grad_l2_norm_diff, last_conv_weights)[0], ord='euclidean')
    model_xent_first_conv_grad_norm = tf.norm(tf.gradients(model.mean_xent, first_conv_weights)[0], ord='euclidean')
    disc_xent_first_conv_grad_norm = tf.norm(tf.gradients(disc_model.mean_xent, first_conv_weights)[0], ord='euclidean')
    input_grad_l2_norm_diff_first_conv_grad_norm = tf.norm(tf.gradients(input_grad_l2_norm_diff, first_conv_weights)[0], ord='euclidean')

    # Setting up the Tensorboard and checkpoint outputs
    if not os.path.exists(model_dir):
        os.makedirs(model_dir)

    saver = tf.train.Saver(max_to_keep=1)
    tf.summary.scalar('C accuracy', model.accuracy)
    tf.summary.scalar('D accuracy', disc_model.accuracy)
    tf.summary.scalar('C xent', model.xent / train_batch_size)
    tf.summary.scalar('D xent', disc_model.xent / train_batch_size)
    tf.summary.scalar('total C loss', total_loss / train_batch_size)
    tf.summary.scalar('total D loss', total_d_loss / train_batch_size)
    tf.summary.scalar('adv C loss', adv_c_loss / train_batch_size)
    tf.summary.scalar('C cls xent loss', model.mean_xent)
    tf.summary.scalar('D xent loss', disc_model.mean_xent)
    # Loss gradients
    tf.summary.scalar('model_xent_logit_grad_norm', model_xent_logit_grad_norm)
    tf.summary.scalar('disc_xent_logit_grad_norm', disc_xent_logit_grad_norm)
    tf.summary.scalar('input_grad_l2_norm_diff_logit_grad_norm', input_grad_l2_norm_diff_logit_grad_norm)
    tf.summary.scalar('model_xent_last_conv_grad_norm', model_xent_last_conv_grad_norm)
    tf.summary.scalar('disc_xent_last_conv_grad_norm', disc_xent_last_conv_grad_norm)
    tf.summary.scalar('input_grad_l2_norm_diff_last_conv_grad_norm', input_grad_l2_norm_diff_last_conv_grad_norm)
    tf.summary.scalar('model_xent_first_conv_grad_norm', model_xent_first_conv_grad_norm)
    tf.summary.scalar('disc_xent_first_conv_grad_norm', disc_xent_first_conv_grad_norm)
    tf.summary.scalar('input_grad_l2_norm_diff_first_conv_grad_norm', input_grad_l2_norm_diff_first_conv_grad_norm)
    merged_summaries = tf.summary.merge_all()

    with tf.Session() as sess:
    # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0)
    # with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        print('important params >>> \n model dir: %s \n dataset: %s \n training batch size: %d \n' % (model_dir, dataset, train_batch_size))
        # initialize data augmentation\
        if dataset == 'cifar10':
          data = cifar10_input.AugmentedCIFAR10Data(raw_data, sess, model)
        elif dataset == 'cifar100':
          data = cifar100_input.AugmentedCIFAR100Data(raw_data, sess, model)

        # Initialize the summary writer, global variables, and our time counter.
        summary_writer = tf.summary.FileWriter(model_dir + '/train', sess.graph)
        eval_summary_writer = tf.summary.FileWriter(model_dir + '/eval')
        sess.run(tf.global_variables_initializer())

        # Restore source model
        source_model_file = tf.train.latest_checkpoint(source_model_dir)
        source_model_saver.restore(sess, source_model_file)

        # Finetune source model here
        if train_finetune_source_model:
            for ii in tqdm(range(finetune_train_steps)):
                x_batch, y_batch = data.train_data.get_next_batch(train_batch_size, multiple_passes=True)
                if finetune_img_random_pert:
                    x_batch = x_batch + np.random.uniform(-epsilon, epsilon, x_batch.shape)
                    x_batch = np.clip(x_batch, 0, 255) # ensure valid pixel range
                
                nat_dict = {full_source_model_x_input: x_batch, source_model.y_input: y_batch}
                                        
                # Output to stdout
                if ii % summary_steps == 0:
                    train_finetune_acc, train_finetune_loss = sess.run([source_model.target_task_accuracy, source_model_finetune_loss], feed_dict=nat_dict)
                    
                    x_eval_batch, y_eval_batch = data.eval_data.get_next_batch(train_batch_size, multiple_passes=True)
                    if img_random_pert:
                        x_eval_batch = x_eval_batch + np.random.uniform(-epsilon, epsilon, x_eval_batch.shape)
                        x_eval_batch = np.clip(x_eval_batch, 0, 255) # ensure valid pixel range

                    eval_dict = {full_source_model_x_input: x_eval_batch, source_model.y_input: y_eval_batch}
                    val_finetune_acc, val_finetune_loss = sess.run([source_model.target_task_accuracy, source_model_finetune_loss], feed_dict=eval_dict)
                    print('Source Model Finetune Step {}:    ({})'.format(ii, datetime.now()))
                    print('    training nat accuracy {:.4}% -- validation nat accuracy {:.4}%'.format(train_finetune_acc * 100,
                                                                                                    val_finetune_acc * 100))
                    print('    training nat c loss: {}'.format( train_finetune_loss ))
                    print('    validation nat c loss: {}'.format( val_finetune_loss ))

                    sys.stdout.flush()

                sess.run(finetune_min_step, feed_dict=nat_dict)
                sess.run(increment_global_step_op)
            finetuned_source_model_saver.save(sess, os.path.join(finetuned_source_model_dir, 'checkpoint'), global_step=global_step)
            
            if only_finetune:
                # full test evaluation
                if dataset == 'cifar10':
                    raw_data = cifar10_input.CIFAR10Data(data_path, init_shuffle=False)
                else:
                    raw_data = cifar100_input.CIFAR100Data(data_path, init_shuffle=False)
                
                data_size = raw_data.eval_data.n
                if data_size % train_batch_size == 0:
                    eval_steps = data_size // train_batch_size
                else:
                    eval_steps = data_size // train_batch_size 
                total_num_correct = 0
                for ii in tqdm(range(eval_steps)):
                    x_eval_batch, y_eval_batch = raw_data.eval_data.get_next_batch(train_batch_size, multiple_passes=False)
                    eval_dict = {full_source_model_x_input: x_eval_batch, source_model.y_input: y_eval_batch}
                    val_finetune_acc, num_correct = sess.run([source_model.target_task_accuracy, source_model.target_task_num_correct], feed_dict=eval_dict)
                    total_num_correct += num_correct
                eval_acc = total_num_correct / data_size
                
                print('Evaluated finetuned source_model on full eval cifar')
                print("Full clean eval_acc: {}%".format(eval_acc*100))

                # generate input gradients for tinyimagenet train and eval set
                if dataset == 'cifar10':
                    raw_data = cifar10_input.CIFAR10Data(data_path, init_shuffle=False)
                else:
                    raw_data = cifar100_input.CIFAR100Data(data_path, init_shuffle=False)
                # Train set
                all_input_gradients = []
                iter_steps = raw_data.train_data.n // train_batch_size
                if raw_data.train_data.n % train_batch_size != 0:
                    iter_steps += 1
                for ii in tqdm(range(iter_steps)):
                    x_batch, y_batch = raw_data.train_data.get_next_batch(train_batch_size, multiple_passes=False)   
                    nat_dict = {full_source_model_x_input: x_batch, source_model.y_input: y_batch}
                    ig = sess.run(source_model_input_grad, feed_dict=nat_dict)
                    all_input_gradients.append(ig)
                path = os.path.join(finetuned_source_model_dir, "{}_train_ig.npy".format(dataset))
                all_input_gradients = np.concatenate(all_input_gradients, axis=0)
                np.save(path, all_input_gradients[:raw_data.train_data.n])
                
                # Eval set
                if dataset == 'cifar10':
                    raw_data = cifar10_input.CIFAR10Data(data_path, init_shuffle=False)
                else:
                    raw_data = cifar100_input.CIFAR100Data(data_path, init_shuffle=False)
                all_input_gradients = []
                iter_steps = raw_data.eval_data.n // train_batch_size
                for ii in tqdm(range(iter_steps)):
                    x_batch, y_batch = raw_data.eval_data.get_next_batch(train_batch_size, multiple_passes=False)   
                    nat_dict = {full_source_model_x_input: x_batch, source_model.y_input: y_batch}
                    ig = sess.run(source_model_input_grad, feed_dict=nat_dict)
                    all_input_gradients.append(ig)
                path = os.path.join(finetuned_source_model_dir, "{}_eval_ig.npy".format(dataset))
                all_input_gradients = np.concatenate(all_input_gradients, axis=0)
                np.save(path, all_input_gradients[:raw_data.eval_data.n])

                return

        else:
            finetuned_source_model_file = tf.train.latest_checkpoint(finetuned_source_model_dir)
            finetuned_source_model_saver.restore(sess, finetuned_source_model_file)


        # reset global step to 0 before running main training loop
        sess.run(reset_global_step_op)

        # Main training loop
        for ii in tqdm(range(train_steps)):
            x_batch, y_batch = data.train_data.get_next_batch(train_batch_size, multiple_passes=True)            
            if img_random_pert:
                x_batch = x_batch + np.random.uniform(-epsilon, epsilon, x_batch.shape)
                x_batch = np.clip(x_batch, 0, 255) # ensure valid pixel range

            # Sample randinit input grads
            nat_dict = {model.x_input: x_batch, model.y_input: y_batch, full_source_model_x_input: x_batch, source_model.y_input: y_batch}

            # Output to stdout
            if ii % summary_steps == 0:
                train_acc, train_disc_acc, train_c_loss, train_d_loss, train_adv_c_loss, summary = sess.run([model.accuracy, disc_model.accuracy, total_loss, total_d_loss, adv_c_loss, merged_summaries], feed_dict=nat_dict)
                summary_writer.add_summary(summary, global_step.eval(sess))
                
                x_eval_batch, y_eval_batch = data.eval_data.get_next_batch(train_batch_size, multiple_passes=True)
                if img_random_pert:
                    x_eval_batch = x_eval_batch + np.random.uniform(-epsilon, epsilon, x_eval_batch.shape)
                    x_eval_batch = np.clip(x_eval_batch, 0, 255) # ensure valid pixel range

                eval_dict = {model.x_input: x_eval_batch, model.y_input: y_eval_batch, full_source_model_x_input: x_eval_batch, source_model.y_input: y_eval_batch}
                val_acc, val_disc_acc, val_c_loss, val_d_loss, val_adv_c_loss, summary = sess.run([model.accuracy, disc_model.accuracy, total_loss, total_d_loss, adv_c_loss, merged_summaries], feed_dict=eval_dict)
                eval_summary_writer.add_summary(summary, global_step.eval(sess))
                print('Step {}:    ({})'.format(ii, datetime.now()))
                print('    training nat accuracy {:.4}% -- validation nat accuracy {:.4}%'.format(train_acc * 100,
                                                                                                  val_acc * 100))
                print('    training nat disc accuracy {:.4}% -- validation nat disc accuracy {:.4}%'.format(train_disc_acc * 100,
                                                                                                  val_disc_acc * 100))
                print('    training nat c loss: {},     d loss: {},     adv c loss: {}'.format( train_c_loss, train_d_loss, train_adv_c_loss))
                print('    validation nat c loss: {},     d loss: {},     adv c loss: {}'.format( val_c_loss, val_d_loss, val_adv_c_loss))

                sys.stdout.flush()
            # Tensorboard summaries
            elif ii % out_steps == 0:
                nat_acc, nat_disc_acc, nat_c_loss, nat_d_loss, nat_adv_c_loss = sess.run([model.accuracy, disc_model.accuracy, total_loss, total_d_loss, adv_c_loss], feed_dict=nat_dict)
                print('Step {}:    ({})'.format(ii, datetime.now()))
                print('    training nat accuracy {:.4}%'.format(nat_acc * 100))
                print('    training nat disc accuracy {:.4}%'.format(nat_disc_acc * 100))
                print('    training nat c loss: {},     d loss: {},      adv c loss: {}'.format( nat_c_loss, nat_d_loss, nat_adv_c_loss))

            # Write a checkpoint
            if (ii+1) % checkpoint_steps == 0:
                saver.save(sess, os.path.join(model_dir, 'checkpoint'), global_step=global_step)
            
            if sep_opt_version == 1:
                if ii >= steps_before_adv_opt:
                    # Actual training step for Classifier
                    sess.run(c_min_step, feed_dict=nat_dict)
                    sess.run(increment_global_step_op)
                    
                    if ii % disc_update_steps == 0:
                        # Actual training step for Discriminator
                        sess.run(d_min_step, feed_dict=nat_dict)
                else:
                    # only train on classification loss
                    sess.run(c_classification_min_step, feed_dict=nat_dict)
                    sess.run(increment_global_step_op)

                # # Use this to optimize classifier and discriminator at the same step
            elif sep_opt_version == 2:
                # Actual training step for Classifier
                if ii >= steps_before_adv_opt:
                    if adv_update_steps_per_iter > 1:
                        sess.run(c_classification_min_step, feed_dict=nat_dict)
                        sess.run(increment_global_step_op)
                        for i in range(adv_update_steps_per_iter):
                            x_batch, y_batch = data.train_data.get_next_batch(train_batch_size, multiple_passes=True)
                            if img_random_pert:
                                x_batch = x_batch + np.random.uniform(-epsilon, epsilon, x_batch.shape)
                                x_batch = np.clip(x_batch, 0, 255) # ensure valid pixel range
                                
                            nat_dict = {model.x_input: x_batch, model.y_input: y_batch, full_source_model_x_input: x_batch, source_model.y_input: y_batch}

                            sess.run(c_adv_min_step, feed_dict=nat_dict)
                    else:
                        sess.run(c_min_step, feed_dict=nat_dict)
                        sess.run(increment_global_step_op)
                    
                    if ii % disc_update_steps == 0:
                        # Actual training step for Discriminator
                        sess.run(d_min_step, feed_dict=nat_dict)
                else:
                    # only train on classification loss
                    sess.run(c_classification_min_step, feed_dict=nat_dict)
                    sess.run(increment_global_step_op)
            elif sep_opt_version == 0:            
                if ii >= steps_before_adv_opt:
                    if ii % disc_update_steps == 0:
                        sess.run([c_min_step, d_min_step], feed_dict=nat_dict)
                        sess.run(increment_global_step_op)
                    else:
                        sess.run(c_min_step, feed_dict=nat_dict)
                        sess.run(increment_global_step_op)
                else:
                    sess.run(c_classification_min_step, feed_dict=nat_dict)
                    sess.run(increment_global_step_op)

        # full test evaluation
        if dataset == 'cifar10':
            raw_data = cifar10_input.CIFAR10Data(data_path, init_shuffle=False)
        else:
            raw_data = cifar100_input.CIFAR100Data(data_path, init_shuffle=False)
        
        data_size = raw_data.eval_data.n
        if data_size % train_batch_size == 0:
            eval_steps = data_size // train_batch_size
        else:
            eval_steps = data_size // train_batch_size 
        total_num_correct = 0
        for ii in tqdm(range(eval_steps)):
            x_eval_batch, y_eval_batch = raw_data.eval_data.get_next_batch(train_batch_size, multiple_passes=False)
            eval_dict = {model.x_input: x_eval_batch, model.y_input: y_eval_batch}
            num_correct = sess.run(model.num_correct, feed_dict=eval_dict)
            total_num_correct += num_correct
        eval_acc = total_num_correct / data_size
        
        clean_eval_file_path = os.path.join(model_dir, 'full_clean_eval_acc.txt')
        with open(clean_eval_file_path, "a+") as f:
            f.write("Full clean eval_acc: {}%".format(eval_acc*100))
        print("Full clean eval_acc: {}%".format(eval_acc*100))


        # generate input gradients for tinyimagenet train and eval set
        # Train set
        all_input_gradients = []
        iter_steps = raw_data.train_data.n // train_batch_size
        if raw_data.train_data.n % train_batch_size != 0:
            iter_steps += 1
        for ii in tqdm(range(iter_steps)):
            x_batch, y_batch = raw_data.train_data.get_next_batch(train_batch_size, multiple_passes=False)   
            nat_dict = {model.x_input: x_batch, model.y_input: y_batch}
            ig = sess.run(input_grad, feed_dict=nat_dict)
            all_input_gradients.append(ig)
        path = os.path.join(model_dir, "{}_train_ig.npy".format(dataset))
        all_input_gradients = np.concatenate(all_input_gradients, axis=0)
        np.save(path, all_input_gradients[:raw_data.train_data.n])
        
        # Eval set
        all_input_gradients = []
        if dataset == 'cifar10':
            raw_data = cifar10_input.CIFAR10Data(data_path, init_shuffle=False)
        else:
            raw_data = cifar100_input.CIFAR100Data(data_path, init_shuffle=False)
        iter_steps = raw_data.eval_data.n // train_batch_size
        for ii in tqdm(range(iter_steps)):
            x_batch, y_batch = raw_data.eval_data.get_next_batch(train_batch_size, multiple_passes=False)   
            nat_dict = {model.x_input: x_batch, model.y_input: y_batch}
            ig = sess.run(input_grad, feed_dict=nat_dict)
            all_input_gradients.append(ig)
        path = os.path.join(model_dir, "{}_eval_ig.npy".format(dataset))
        all_input_gradients = np.concatenate(all_input_gradients, axis=0)
        np.save(path, all_input_gradients[:raw_data.eval_data.n])


        devices = sess.list_devices()
        for d in devices:
            print("sess' device names:")
            print(d.name)

    return model_dir
Exemplo n.º 58
0
    def __init__(self, I_size, O_size, n_control):
        #The network recieves a frame from the game, flattened into an array.
        #It then resizes it and processes it through four convolutional layers.
        self.scalarInput = tf.placeholder(shape=[None, I_size],
                                          dtype=tf.float32)

        self.f_connect1 = tf.contrib.layers.fully_connected(
            inputs=self.scalarInput,
            num_outputs=64,
            activation_fn=tf.nn.relu,
            weights_initializer=tf.random_normal_initializer(),
            biases_initializer=tf.random_normal_initializer())
        self.f_connect2 = tf.contrib.layers.fully_connected(
            inputs=self.f_connect1,
            num_outputs=64,
            activation_fn=tf.nn.relu,
            weights_initializer=tf.random_normal_initializer(),
            biases_initializer=tf.random_normal_initializer())
        self.f_connect3 = tf.contrib.layers.fully_connected(
            inputs=self.f_connect2,
            num_outputs=64,
            activation_fn=tf.nn.relu,
            weights_initializer=tf.random_normal_initializer(),
            biases_initializer=tf.random_normal_initializer())
        self.f_connect4 = tf.contrib.layers.fully_connected(
            inputs=self.f_connect3,
            num_outputs=O_size,
            activation_fn=tf.nn.relu,
            weights_initializer=tf.random_normal_initializer(),
            biases_initializer=tf.random_normal_initializer())

        #We take the output from the final convolutional layer and split it into separate advantage and value streams.
        self.streamAC, self.streamVC = tf.split(self.f_connect4,
                                                num_or_size_splits=2,
                                                axis=1)

        #self.streamA = slim.flatten(self.streamAC)
        #self.streamV = slim.flatten(self.streamVC)

        self.streamA = self.streamAC
        self.streamV = self.streamVC

        xavier_init = tf.contrib.layers.xavier_initializer()
        self.AW = tf.Variable(xavier_init([O_size // 2, 5 * n_control]))
        self.VW = tf.Variable(xavier_init([O_size // 2, 1]))
        self.Advantage = tf.matmul(self.streamA, self.AW)
        self.Value = tf.matmul(self.streamV, self.VW)

        #Then combine them together to get our final Q-values.
        self.Qout = self.Value + tf.subtract(
            self.Advantage, tf.reduce_mean(self.Advantage, keep_dims=True))

        sizeQ = tf.shape(self.Qout)

        self.Qout_reshape = tf.reshape(self.Qout, [
            tf.to_int32(sizeQ[0] * n_control),
            tf.to_int32(sizeQ[1] / n_control)
        ])

        self.predict = tf.argmax(self.Qout_reshape, 1)

        #network generate all the action-value pair for the input state, we sample some action-value pair from memory, we just need to min the different between o and out

        #Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
        self.targetQ = tf.placeholder(shape=[None, n_control],
                                      dtype=tf.float32)
        self.actions = tf.placeholder(shape=[None, n_control], dtype=tf.int32)

        self.actions_onehot = tf.one_hot(self.actions, 5, dtype=tf.float32)

        hotsize = tf.shape(self.actions_onehot)

        self.reshape_hot = tf.reshape(self.actions_onehot,
                                      [hotsize[0] * n_control, 5])

        self.sum = tf.reduce_sum(tf.multiply(self.Qout_reshape,
                                             self.reshape_hot),
                                 axis=1)
        self.Q = tf.reshape(self.sum, [hotsize[0], n_control])
        self.td_error = tf.square(self.targetQ - self.Q)
        self.loss = tf.reduce_mean(self.td_error)
        self.trainer = tf.train.AdamOptimizer(learning_rate=0.0001)
        self.updateModel = self.trainer.minimize(self.loss)
Exemplo n.º 59
0
batch_size = 150

# Initialize placeholders
x_data = tf.placeholder(shape=[None, 2], dtype=tf.float32)
y_target = tf.placeholder(shape=[None, 1], dtype=tf.float32)
prediction_grid = tf.placeholder(shape=[None, 2], dtype=tf.float32)

# Create variables for svm
b = tf.Variable(tf.random_normal(shape=[1, batch_size]))

# Gaussian (RBF) kernel
gamma = tf.constant(-25.0)
dist = tf.reduce_sum(tf.square(x_data), 1)
dist = tf.reshape(dist, [-1, 1])
sq_dists = tf.add(
    tf.subtract(dist, tf.multiply(2., tf.matmul(x_data,
                                                tf.transpose(x_data)))),
    tf.transpose(dist))
my_kernel = tf.exp(tf.multiply(gamma, tf.abs(sq_dists)))

# Compute SVM Model
model_output = tf.matmul(b, my_kernel)
first_term = tf.reduce_sum(b)
b_vec_cross = tf.matmul(tf.transpose(b), b)
y_target_cross = tf.matmul(y_target, tf.transpose(y_target))
second_term = tf.reduce_sum(
    tf.multiply(my_kernel, tf.multiply(b_vec_cross, y_target_cross)))
loss = tf.negative(tf.subtract(first_term, second_term))

# Gaussian (RBF) prediction kernel
rA = tf.reshape(tf.reduce_sum(tf.square(x_data), 1), [-1, 1])
rB = tf.reshape(tf.reduce_sum(tf.square(prediction_grid), 1), [-1, 1])
Exemplo n.º 60
0
 def add_loss_op(self, preds):
     Diff = (tf.subtract(self.labels_placeholder, preds))
     batch_loss = tf.sqrt(tf.reduce_sum(tf.square(Diff), axis=1))
     mean_loss = tf.reduce_mean(batch_loss)
     return mean_loss