def testWeightedMovingAverage(self):
        with self.test_session() as sess:
            decay = 0.5
            weight = tf.placeholder(tf.float32, [])
            val = tf.placeholder(tf.float32, [])

            wma = moving_averages.weighted_moving_average(val, decay, weight)
            tf.initialize_all_variables().run()

            # Get the first weighted moving average.
            val_1 = 3.0
            weight_1 = 4.0
            wma_array = sess.run(wma, feed_dict={val: val_1, weight: weight_1})
            numerator_1 = val_1 * weight_1 * (1.0 - decay)
            denominator_1 = weight_1 * (1.0 - decay)
            self.assertAllClose(numerator_1 / denominator_1, wma_array)

            # Get the second weighted moving average.
            val_2 = 11.0
            weight_2 = 22.0
            wma_array = sess.run(wma, feed_dict={val: val_2, weight: weight_2})
            numerator_2 = numerator_1 * decay + val_2 * weight_2 * (1.0 -
                                                                    decay)
            denominator_2 = denominator_1 * decay + weight_2 * (1.0 - decay)
            self.assertAllClose(numerator_2 / denominator_2, wma_array)
Example #2
0
  def testWeightedMovingAverageBfloat16(self):
    bfloat16 = pywrap_tensorflow.TF_bfloat16_type()
    with self.cached_session() as sess:
      decay = 0.5
      weight = array_ops.placeholder(dtypes.bfloat16, [])
      val = array_ops.placeholder(dtypes.bfloat16, [])

      wma = moving_averages.weighted_moving_average(val, decay, weight)
      variables.global_variables_initializer().run()

      # Get the first weighted moving average.
      val_1 = 3.0
      weight_1 = 4.0
      wma_array = sess.run(wma, feed_dict={val: val_1, weight: weight_1})
      numerator_1 = val_1 * weight_1 * (1.0 - decay)
      denominator_1 = weight_1 * (1.0 - decay)
      self.assertAllClose(numerator_1 / denominator_1, wma_array)

      # Get the second weighted moving average.
      val_2 = 11.0
      weight_2 = 22.0
      wma_array = sess.run(wma, feed_dict={val: val_2, weight: weight_2})
      numerator_2 = numerator_1 * decay + val_2 * weight_2 * (1.0 - decay)
      denominator_2 = denominator_1 * decay + weight_2 * (1.0 - decay)
      self.assertAllClose(bfloat16(numerator_2 / denominator_2), wma_array)
  def testWeightedMovingAverage(self):
    with self.test_session() as sess:
      decay = 0.5
      weight = tf.placeholder(tf.float32, [])
      val = tf.placeholder(tf.float32, [])

      wma = moving_averages.weighted_moving_average(val, decay, weight)
      tf.initialize_all_variables().run()

      # Get the first weighted moving average.
      val_1 = 3.0
      weight_1 = 4.0
      wma_array = sess.run(
          wma, feed_dict={val: val_1, weight: weight_1})
      numerator_1 = val_1 * weight_1 * (1.0 - decay)
      denominator_1 = weight_1 * (1.0 - decay)
      self.assertAllClose(numerator_1 / denominator_1, wma_array)

      # Get the second weighted moving average.
      val_2 = 11.0
      weight_2 = 22.0
      wma_array = sess.run(
          wma, feed_dict={val: val_2, weight: weight_2})
      numerator_2 = numerator_1 * decay + val_2 * weight_2 * (1.0 - decay)
      denominator_2 = denominator_1 * decay + weight_2 * (1.0 - decay)
      self.assertAllClose(numerator_2 / denominator_2, wma_array)
Example #4
0
def compute_losses_multi_or(logits,
                            actions_one_hot,
                            if_reach_goal,
                            weights=None,
                            num_actions=-1,
                            data_loss_wt=1.,
                            reg_loss_wt=1.,
                            ewma_decay=0.99,
                            reg_loss_op=None,
                            batch_size=4):
    assert (num_actions > 0), 'num_actions must be specified and must be > 0.'

    with tf.name_scope('loss'):
        if_reach_goal = tf.squeeze(if_reach_goal, [-1])
        batch_total = tf.reduce_sum(if_reach_goal, reduction_indices=1)
        total = tf.reduce_sum(if_reach_goal)

        if weights is None:
            weights = tf.ones_like(actions_one_hot,
                                   dtype=tf.float32,
                                   name='weight')

        action_prob = tf.nn.softmax(logits)  # (Bx40) x 6
        action_prob = tf.reshape(action_prob,
                                 shape=[batch_size, -1,
                                        num_actions])  # B x 40 x 6
        multip = tf.multiply(action_prob, actions_one_hot)  # B x 40 x 6
        action_prob = tf.reduce_sum(multip, reduction_indices=2)  # B x 40

        example_loss = -tf.math.log(tf.maximum(tf.constant(1e-4),
                                               action_prob))  # B x 40
        example_loss = tf.multiply(example_loss, if_reach_goal)  # B x 40
        batch_loss = tf.reduce_sum(example_loss, reduction_indices=1) / \
                     tf.maximum(batch_total, tf.constant(1.))  # B. .If batch_total is zero all example_loss in that batch is all zeros.
        data_loss_op = tf.reduce_sum(batch_loss) / batch_size
        if reg_loss_op is None:
            if reg_loss_wt > 0:
                reg_loss_op = tf.add_n(tf.losses.get_regularization_losses())
            else:
                reg_loss_op = tf.constant(0.)

        if reg_loss_wt > 0:
            total_loss_op = data_loss_wt * data_loss_op + reg_loss_wt * reg_loss_op
        else:
            total_loss_op = data_loss_wt * data_loss_op

        is_correct = tf.cast(tf.greater(action_prob, 0.5, name='pred_class'),
                             tf.float32)
        is_correct = tf.multiply(is_correct, if_reach_goal)
        acc_op = tf.reduce_sum(is_correct) / total

        ewma_acc_op = moving_averages.weighted_moving_average(acc_op,
                                                              ewma_decay,
                                                              weight=total,
                                                              name='ewma_acc')

        acc_ops = [ewma_acc_op]

    return reg_loss_op, data_loss_op, total_loss_op, acc_ops
Example #5
0
def compute_losses_multi_or(logits,
                            actions_one_hot,
                            weights=None,
                            num_actions=-1,
                            data_loss_wt=1.,
                            reg_loss_wt=1.,
                            ewma_decay=0.99,
                            reg_loss_op=None):
    assert (num_actions > 0), 'num_actions must be specified and must be > 0.'

    with tf.name_scope('loss'):
        if weights is None:
            weight = tf.ones_like(actions_one_hot,
                                  dtype=tf.float32,
                                  name='weight')

        actions_one_hot = tf.cast(
            tf.reshape(actions_one_hot, [-1, num_actions],
                       're_actions_one_hot'), tf.float32)
        weights = tf.reduce_sum(tf.reshape(weights, [-1, num_actions],
                                           're_weight'),
                                reduction_indices=1)
        total = tf.reduce_sum(weights)

        action_prob = tf.nn.softmax(logits)
        action_prob = tf.reduce_sum(tf.multiply(action_prob, actions_one_hot),
                                    reduction_indices=1)
        example_loss = -tf.log(tf.maximum(tf.constant(1e-4), action_prob))

        data_loss_op = tf.reduce_sum(example_loss * weights) / total
        if reg_loss_op is None:
            if reg_loss_wt > 0:
                reg_loss_op = tf.add_n(tf.losses.get_regularization_losses())
            else:
                reg_loss_op = tf.constant(0.)

        if reg_loss_wt > 0:
            total_loss_op = data_loss_wt * data_loss_op + reg_loss_wt * reg_loss_op
        else:
            total_loss_op = data_loss_wt * data_loss_op

        is_correct = tf.cast(tf.greater(action_prob, 0.5, name='pred_class'),
                             tf.float32)
        acc_op = tf.reduce_sum(is_correct * weights) / total

        ewma_acc_op = moving_averages.weighted_moving_average(acc_op,
                                                              ewma_decay,
                                                              weight=total,
                                                              name='ewma_acc')

        acc_ops = [ewma_acc_op]

    return reg_loss_op, data_loss_op, total_loss_op, acc_ops
Example #6
0
def compute_losses_multi_or(logits, actions_one_hot, weights=None,
                            num_actions=-1, data_loss_wt=1., reg_loss_wt=1.,
                            ewma_decay=0.99, reg_loss_op=None):
  assert(num_actions > 0), 'num_actions must be specified and must be > 0.'
  
  with tf.name_scope('loss'):
    if weights is None:
      weight = tf.ones_like(actions_one_hot, dtype=tf.float32, name='weight')
    
    actions_one_hot = tf.cast(tf.reshape(actions_one_hot, [-1, num_actions],
                                         're_actions_one_hot'), tf.float32)
    weights = tf.reduce_sum(tf.reshape(weights, [-1, num_actions], 're_weight'),
                            reduction_indices=1)
    total = tf.reduce_sum(weights)

    action_prob = tf.nn.softmax(logits)
    action_prob = tf.reduce_sum(tf.multiply(action_prob, actions_one_hot),
                                reduction_indices=1)
    example_loss = -tf.log(tf.maximum(tf.constant(1e-4), action_prob))

    data_loss_op = tf.reduce_sum(example_loss * weights) / total
    if reg_loss_op is None:
      if reg_loss_wt > 0:
        reg_loss_op = tf.add_n(tf.losses.get_regularization_losses())
      else:
        reg_loss_op = tf.constant(0.)
    
    if reg_loss_wt > 0:
      total_loss_op = data_loss_wt*data_loss_op + reg_loss_wt*reg_loss_op 
    else:
      total_loss_op = data_loss_wt*data_loss_op

    is_correct = tf.cast(tf.greater(action_prob, 0.5, name='pred_class'), tf.float32)
    acc_op = tf.reduce_sum(is_correct*weights) / total

    ewma_acc_op = moving_averages.weighted_moving_average(
        acc_op, ewma_decay, weight=total, name='ewma_acc')

    acc_ops = [ewma_acc_op]

  return reg_loss_op, data_loss_op, total_loss_op, acc_ops