Python tensorflow.python.training.moving_averages.weighted_moving_average() Examples

The following are 10 code examples of tensorflow.python.training.moving_averages.weighted_moving_average(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow.python.training.moving_averages , or try the search function .
Example #1
Source File: nav_utils.py    From DOTA_models with Apache License 2.0 5 votes vote down vote up
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 #2
Source File: nav_utils.py    From yolo_v2 with Apache License 2.0 5 votes vote down vote up
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 #3
Source File: nav_utils.py    From Gun-Detector with Apache License 2.0 5 votes vote down vote up
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 #4
Source File: moving_averages_test.py    From deep_image_model with Apache License 2.0 5 votes vote down vote up
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.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(numerator_2 / denominator_2, wma_array) 
Example #5
Source File: nav_utils.py    From hands-detection with MIT License 5 votes vote down vote up
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
Source File: nav_utils.py    From object_detection_kitti with Apache License 2.0 5 votes vote down vote up
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 #7
Source File: nav_utils.py    From object_detection_with_tensorflow with MIT License 5 votes vote down vote up
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 #8
Source File: nav_utils.py    From g-tensorflow-models with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: nav_utils.py    From models with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: nav_utils.py    From multilabel-image-classification-tensorflow with MIT License 5 votes vote down vote up
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