python类greater_equal()的实例源码

segment.py 文件源码 项目:jack 作者: uclmr 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def segment_sample_select(probs, segment_ids):
    num_segments = tf.reduce_max(segment_ids) + 1
    sampled = tf.random_uniform([num_segments])

    def scan_fn(acc, x):
        p, i = x[0], x[1]
        prev_v = tf.gather(acc[0], i)
        new_probs = acc[0] + tf.one_hot(i, num_segments, p)
        select = tf.logical_and(tf.less(prev_v, 0.0), tf.greater_equal(prev_v + p, 0.0))
        return new_probs, select

    _, selection = tf.scan(scan_fn, (probs, segment_ids), initializer=(-sampled, False))

    return selection
tensorflow_backend.py 文件源码 项目:keras 作者: GeekLiB 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def greater_equal(x, y):
    '''Element-wise truth value of (x >= y).
    Returns a bool tensor.
    '''
    return tf.greater_equal(x, y)
kfac.py 文件源码 项目:baselines 作者: openai 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def apply_stats(self, statsUpdates):
        """ compute stats and update/apply the new stats to the running average
        """

        def updateAccumStats():
            if self._full_stats_init:
                return tf.cond(tf.greater(self.sgd_step, self._cold_iter), lambda: tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter)), tf.no_op)
            else:
                return tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter))

        def updateRunningAvgStats(statsUpdates, fac_iter=1):
            # return tf.cond(tf.greater_equal(self.factor_step,
            # tf.convert_to_tensor(fac_iter)), lambda:
            # tf.group(*self._apply_stats(stats_list, varlist)), tf.no_op)
            return tf.group(*self._apply_stats(statsUpdates))

        if self._async_stats:
            # asynchronous stats update
            update_stats = self._apply_stats(statsUpdates)

            queue = tf.FIFOQueue(1, [item.dtype for item in update_stats], shapes=[
                                 item.get_shape() for item in update_stats])
            enqueue_op = queue.enqueue(update_stats)

            def dequeue_stats_op():
                return queue.dequeue()
            self.qr_stats = tf.train.QueueRunner(queue, [enqueue_op])
            update_stats_op = tf.cond(tf.equal(queue.size(), tf.convert_to_tensor(
                0)), tf.no_op, lambda: tf.group(*[dequeue_stats_op(), ]))
        else:
            # synchronous stats update
            update_stats_op = tf.cond(tf.greater_equal(
                self.stats_step, self._stats_accum_iter), lambda: updateRunningAvgStats(statsUpdates), updateAccumStats)
        self._update_stats_op = update_stats_op
        return update_stats_op
utils.py 文件源码 项目:zhusuan 作者: thu-ml 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __ge__(self, other):
        return tf.greater_equal(self, other)

    # slicing and indexing
metrics.py 文件源码 项目:kaggle_redefining_cancer_treatment 作者: jorgemf 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def multi_label(prediction_batch, labels_batch, threshold=0.5, moving_average=True):
    with tf.variable_scope('metrics'):
        threshold_graph = tf.constant(threshold, name='threshold')
        zero_point_five = tf.constant(0.5)
        predicted_bool = tf.greater_equal(prediction_batch, threshold_graph)
        real_bool = tf.greater_equal(labels_batch, zero_point_five)
        return _metrics(predicted_bool, real_bool, moving_average)
text_classification_model_simple.py 文件源码 项目:kaggle_redefining_cancer_treatment 作者: jorgemf 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def remove_padding(self, input_text):
        # calculate max length of the input_text
        mask = tf.greater_equal(input_text, 0)  # true for words false for padding
        sequence_length = tf.reduce_sum(tf.cast(mask, tf.int32), 1)

        # truncate the input text to max length
        max_sequence_length = tf.reduce_max(sequence_length)
        input_text_length = tf.shape(input_text)[1]
        empty_padding_lenght = input_text_length - max_sequence_length
        input_text, _ = tf.split(input_text, [max_sequence_length, empty_padding_lenght], axis=1)

        return input_text, sequence_length
layers.py 文件源码 项目:LiTeFlow 作者: petrux 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def finished(self, time, output):
        """Check which sentences are finished.

        Arguments:
          time: a `Tensor` of rank `0D` (i.e. a scalar) with the 0-based value of the
            current step in the loop.
          output: a `Tensor` of rank `2D` and shape `[batch_size, num_classes]` representing
            the current output of the model, i.e. abatch of probability distribution estimations
            over the output classes.

        Returns:
          a `Tensor` of shape `[batch_size]` of `tf.bool` elements, indicating for each
          position if the corresponding sequence has terminated or not. A sequence is
          has terminated if the current step is greater or equal the number of steps allowed
          (defined in the `lengths` input argument) and if the `argmax` over the output
          probability distribution ends up in the class that has id equal to the `EOS` symbol
          (if provided).
        """
        length = time + 1
        finished = tf.greater_equal(length, self._lengths)
        if finished.get_shape().ndims == 0:
            batch = [utils.get_dimension(output, 0)]
            finished = tf.tile([finished], batch)
        if self._EOS is not None:
            ids = tf.cast(tf.argmax(output, axis=-1), tf.int32)
            eos = tf.equal(ids, self._EOS)
            finished = tf.logical_or(finished, eos)
        return finished
test_layers.py 文件源码 项目:LiTeFlow 作者: petrux 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_iterations(self):
        """Test the number of iterations."""

        lengths = tf.constant([1, 2, 3], dtype=tf.int32)
        def _helper_finished(time, _):
            return tf.greater_equal(time + 1, lengths)
        helper = mock.Mock()
        helper.finished.side_effect = _helper_finished

        batch_size = utils.get_dimension(lengths, 0)
        inp_size, state_size, output_size = 2, 5, 2

        decoder = mock.Mock()
        decoder.init_input.side_effect = lambda: tf.zeros([batch_size, inp_size])
        decoder.init_state.side_effect = lambda: tf.ones([batch_size, state_size])
        decoder.zero_output.side_effect = lambda: tf.zeros([batch_size, output_size])
        decoder.step.side_effect = lambda t, i, s:\
            ((i + 1), 3 * (i + 1), (s + 2), tf.tile([False], [batch_size]))

        output_exp = np.asarray(
            [[[1, 1], [0, 0], [0, 0]],
             [[1, 1], [4, 4], [0, 0]],
             [[1, 1], [4, 4], [13, 13]]],
            dtype=np.float32)  # pylint: disable=E1101,I0011
        state_exp = np.asarray(
            [[7, 7, 7, 7, 7],
             [7, 7, 7, 7, 7],
             [7, 7, 7, 7, 7]],
            dtype=np.float32) # pylint: disable=E1101,I0011

        dyndec = layers.DynamicDecoder(decoder, helper)
        output_t, state_t = dyndec.decode()

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            output_act, state_act = sess.run([output_t, state_t])
            self.assertAllEqual(output_exp, output_act)
            self.assertAllEqual(state_exp, state_act)
model.py 文件源码 项目:CycleGAN-TensorFlow 作者: vanhuyz 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def optimize(self, G_loss, D_Y_loss, F_loss, D_X_loss):
    def make_optimizer(loss, variables, name='Adam'):
      """ Adam optimizer with learning rate 0.0002 for the first 100k steps (~100 epochs)
          and a linearly decaying rate that goes to zero over the next 100k steps
      """
      global_step = tf.Variable(0, trainable=False)
      starter_learning_rate = self.learning_rate
      end_learning_rate = 0.0
      start_decay_step = 100000
      decay_steps = 100000
      beta1 = self.beta1
      learning_rate = (
          tf.where(
                  tf.greater_equal(global_step, start_decay_step),
                  tf.train.polynomial_decay(starter_learning_rate, global_step-start_decay_step,
                                            decay_steps, end_learning_rate,
                                            power=1.0),
                  starter_learning_rate
          )

      )
      tf.summary.scalar('learning_rate/{}'.format(name), learning_rate)

      learning_step = (
          tf.train.AdamOptimizer(learning_rate, beta1=beta1, name=name)
                  .minimize(loss, global_step=global_step, var_list=variables)
      )
      return learning_step

    G_optimizer = make_optimizer(G_loss, self.G.variables, name='Adam_G')
    D_Y_optimizer = make_optimizer(D_Y_loss, self.D_Y.variables, name='Adam_D_Y')
    F_optimizer =  make_optimizer(F_loss, self.F.variables, name='Adam_F')
    D_X_optimizer = make_optimizer(D_X_loss, self.D_X.variables, name='Adam_D_X')

    with tf.control_dependencies([G_optimizer, D_Y_optimizer, F_optimizer, D_X_optimizer]):
      return tf.no_op(name='optimizers')
tensorflow_backend.py 文件源码 项目:deep-learning-keras-projects 作者: jasmeetsb 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def greater_equal(x, y):
    """Element-wise truth value of (x >= y).

    # Returns
        A bool tensor.
    """
    return tf.greater_equal(x, y)
metrics.py 文件源码 项目:tensorport-template 作者: tensorport 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def multi_label(prediction_batch, labels_batch, threshold=0.5, moving_average=True):
    with tf.variable_scope('metrics'):
        threshold_graph = tf.constant(threshold, name='threshold')
        zero_point_five = tf.constant(0.5)
        predicted_bool = tf.greater_equal(prediction_batch, threshold_graph)
        real_bool = tf.greater_equal(labels_batch, zero_point_five)
        return _metrics(predicted_bool, real_bool, moving_average)
pyramid_network.py 文件源码 项目:Master-R-CNN 作者: Mark110 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _get_valid_sample_fraction(labels, p=0):
    """return fraction of non-negative examples, the ignored examples have been marked as negative"""
    num_valid = tf.reduce_sum(tf.cast(tf.greater_equal(labels, p), tf.float32))
    num_example = tf.cast(tf.size(labels), tf.float32)
    frac = tf.cond(tf.greater(num_example, 0), lambda:num_valid / num_example,  
            lambda: tf.cast(0, tf.float32))
    frac_ = tf.cond(tf.greater(num_valid, 0), lambda:num_example / num_valid, 
            lambda: tf.cast(0, tf.float32))
    return frac, frac_
vgg_preprocessing.py 文件源码 项目:Classification_Nets 作者: BobLiu20 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _crop(image, offset_height, offset_width, crop_height, crop_width):
  """Crops the given image using the provided offsets and sizes.

  Note that the method doesn't assume we know the input image size but it does
  assume we know the input image rank.

  Args:
    image: an image of shape [height, width, channels].
    offset_height: a scalar tensor indicating the height offset.
    offset_width: a scalar tensor indicating the width offset.
    crop_height: the height of the cropped image.
    crop_width: the width of the cropped image.

  Returns:
    the cropped (and resized) image.

  Raises:
    InvalidArgumentError: if the rank is not 3 or if the image dimensions are
      less than the crop size.
  """
  original_shape = tf.shape(image)

  rank_assertion = tf.Assert(
      tf.equal(tf.rank(image), 3),
      ['Rank of image must be equal to 3.'])
  with tf.control_dependencies([rank_assertion]):
    cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]])

  size_assertion = tf.Assert(
      tf.logical_and(
          tf.greater_equal(original_shape[0], crop_height),
          tf.greater_equal(original_shape[1], crop_width)),
      ['Crop size greater than the image size.'])

  offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))

  # Use tf.slice instead of crop_to_bounding box as it accepts tensors to
  # define the crop size.
  with tf.control_dependencies([size_assertion]):
    image = tf.slice(image, offsets, cropped_shape)
  return tf.reshape(image, cropped_shape)
preprocessor.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _crop(self, image, offset_height, offset_width, crop_height, crop_width):
        """Crops the given image using the provided offsets and sizes.
        Note that the method doesn't assume we know the input image size but it does
        assume we know the input image rank.

        Args:
          image: an image of shape [height, width, channels].
          offset_height: a scalar tensor indicating the height offset.
          offset_width: a scalar tensor indicating the width offset.
          crop_height: the height of the cropped image.
          crop_width: the width of the cropped image.

        Returns:
          the cropped (and resized) image.

        Raises:
          InvalidArgumentError: if the rank is not 3 or if the image dimensions are
            less than the crop size.
        """
        original_shape = tf.shape(image)

        rank_assertion = tf.Assert(
            tf.equal(tf.rank(image), 3),
            ['Rank of image must be equal to 3.'])
        with tf.control_dependencies([rank_assertion]):
            cropped_shape = tf.stack(
                [crop_height, crop_width, original_shape[2]])

        size_assertion = tf.Assert(
            tf.logical_and(
                tf.greater_equal(original_shape[0], crop_height),
                tf.greater_equal(original_shape[1], crop_width)),
            ['Crop size greater than the image size.'])

        offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))
        with tf.control_dependencies([size_assertion]):
            image = tf.slice(image, offsets, cropped_shape)
        return tf.reshape(image, cropped_shape)
preprocessor.py 文件源码 项目:tefla 作者: openAGI 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _crop(self, image, offset_height, offset_width, crop_height, crop_width):
        """Crops the given image using the provided offsets and sizes.
        Note that the method doesn't assume we know the input image size but it does
        assume we know the input image rank.

        Args:
          image: an image of shape [height, width, channels].
          offset_height: a scalar tensor indicating the height offset.
          offset_width: a scalar tensor indicating the width offset.
          crop_height: the height of the cropped image.
          crop_width: the width of the cropped image.

        Returns:
          the cropped (and resized) image.

        Raises:
          InvalidArgumentError: if the rank is not 3 or if the image dimensions are
            less than the crop size.
        """
        original_shape = tf.shape(image)

        rank_assertion = tf.Assert(
            tf.equal(tf.rank(image), 3),
            ['Rank of image must be equal to 3.'])
        with tf.control_dependencies([rank_assertion]):
            cropped_shape = tf.stack(
                [crop_height, crop_width, original_shape[2]])

        size_assertion = tf.Assert(
            tf.logical_and(
                tf.greater_equal(original_shape[0], crop_height),
                tf.greater_equal(original_shape[1], crop_width)),
            ['Crop size greater than the image size.'])

        offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))
        with tf.control_dependencies([size_assertion]):
            image = tf.slice(image, offsets, cropped_shape)
        return tf.reshape(image, cropped_shape)
augmentation.py 文件源码 项目:fcn 作者: ilovin 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def crop_to_fixed_size(img_tensor,annotation_tensor,output_shape):
    """
    the output_shape must be smaller than the input_shape
    :param img_tensor: [w,h,depth]
    :param annotation_tensor: [w,h,1]
    :param output_shape:
    :param mask_out_num:
    :return: (output_shape,output_shape,3) (output_shape,output_shape,1)
    """
    original_shape = tf.shape(img_tensor)
    crop_width, crop_height = output_shape[0],output_shape[1]
    image_width, image_height = original_shape[0],original_shape[1]
    img_cropped_shape = tf.stack([output_shape[0], output_shape[1], 3])
    annotate_cropped_shape = tf.stack([output_shape[0], output_shape[1], 1])

    size_assertion = tf.Assert(
        tf.logical_and(
            tf.greater_equal(original_shape[0], crop_width),
            tf.greater_equal(original_shape[1], crop_height)),
        ['Crop size greater than the image size.'])
    max_offset_height = tf.reshape(image_height - crop_height + 1, [])
    max_offset_width = tf.reshape(image_width - crop_width + 1, [])

    offset_height = tf.random_uniform(
        [], maxval=max_offset_height, dtype=tf.int32)
    offset_width = tf.random_uniform(
        [], maxval=max_offset_width, dtype=tf.int32)
    offsets = tf.to_int32(tf.stack([offset_width, offset_height, 0]))

    annotation_tensor = tf.to_int32(annotation_tensor)
    # Use tf.slice instead of crop_to_bounding box as it accepts tensors to
    # define the crop size.
    with tf.control_dependencies([size_assertion]):
        image = tf.slice(img_tensor, offsets, img_cropped_shape)
        annotate = tf.slice(annotation_tensor,offsets,annotate_cropped_shape)
    return tf.reshape(image, img_cropped_shape),tf.reshape(annotate,annotate_cropped_shape)
augmentation.py 文件源码 项目:fcn 作者: ilovin 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def crop_or_resize_to_fixed_size_and_rotate_output(img_tensor,
                                        annotation_tensor,
                                        output_shape,
                                        mask_out_num=None):
    """Returns tensor of a size (output_shape, output_shape, depth) and (output_shape, output_shape, 1).
    The function returns tensor that is of a size (output_shape, output_shape, depth)
    which is randomly cropped and rotate

    Parameters
    ----------
    img_tensor : Tensor of size (width, height, depth)
        Tensor with image
    annotation_tensor : Tensor of size (width, height, 1)
        Tensor with respective annotation
    output_shape : Tensor or list [int, int]
        Tensor of list representing desired output shape
    mask_out_number : int
        Number representing the mask out value.

    Returns
    -------
    cropped_padded_img : Tensor of size (output_shape[0], output_shape[1], 3).
        Image Tensor that was randomly scaled
    cropped_padded_annotation : Tensor of size (output_shape[0], output_shape[1], 1)
        Respective annotation Tensor that was randomly scaled with the same parameters
    """
    input_shape = tf.shape(img_tensor)[0:2]
    image_width, image_height = input_shape[0],input_shape[1]
    crop_width, crop_height = output_shape[0],output_shape[1]

    cropped_padded_img,cropped_padded_annotaion =  control_flow_ops.cond(
        tf.logical_and(
            tf.greater_equal(image_height, crop_height),
            tf.greater_equal(image_width, crop_width)),
        fn1=lambda:crop_to_fixed_size(img_tensor,annotation_tensor,output_shape),
        fn2=lambda:resize_to_fixed_size(img_tensor,annotation_tensor,output_shape,mask_out_num=mask_out_num))
    return cropped_padded_img,cropped_padded_annotaion
vgg_preprocessing.py 文件源码 项目:fcn 作者: ilovin 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _crop(image, offset_height, offset_width, crop_height, crop_width):
  """Crops the given image using the provided offsets and sizes.

  Note that the method doesn't assume we know the input image size but it does
  assume we know the input image rank.

  Args:
    image: an image of shape [height, width, channels].
    offset_height: a scalar tensor indicating the height offset.
    offset_width: a scalar tensor indicating the width offset.
    crop_height: the height of the cropped image.
    crop_width: the width of the cropped image.

  Returns:
    the cropped (and resized) image.

  Raises:
    InvalidArgumentError: if the rank is not 3 or if the image dimensions are
      less than the crop size.
  """
  original_shape = tf.shape(image)

  rank_assertion = tf.Assert(
      tf.equal(tf.rank(image), 3),
      ['Rank of image must be equal to 3.'])
  with tf.control_dependencies([rank_assertion]):
    cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]])

  size_assertion = tf.Assert(
      tf.logical_and(
          tf.greater_equal(original_shape[0], crop_height),
          tf.greater_equal(original_shape[1], crop_width)),
      ['Crop size greater than the image size.'])

  offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))

  # Use tf.slice instead of crop_to_bounding box as it accepts tensors to
  # define the crop size.
  with tf.control_dependencies([size_assertion]):
    image = tf.slice(image, offsets, cropped_shape)
  return tf.reshape(image, cropped_shape)
model.py 文件源码 项目:mean-teacher 作者: CuriousAI 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def training_control(global_step, print_span, evaluation_span, max_step, name=None):
    with tf.name_scope(name, "training_control"):
        return {
            "step": global_step,
            "time_to_print": tf.equal(tf.mod(global_step, print_span), 0),
            "time_to_evaluate": tf.equal(tf.mod(global_step, evaluation_span), 0),
            "time_to_stop": tf.greater_equal(global_step, max_step),
        }
kfac.py 文件源码 项目:pysc2-examples 作者: chris-chris 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def apply_stats(self, statsUpdates):
    """ compute stats and update/apply the new stats to the running average
    """

    def updateAccumStats():
      if self._full_stats_init:
        return tf.cond(tf.greater(self.sgd_step, self._cold_iter), lambda: tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter)), tf.no_op)
      else:
        return tf.group(*self._apply_stats(statsUpdates, accumulate=True, accumulateCoeff=1. / self._stats_accum_iter))

    def updateRunningAvgStats(statsUpdates, fac_iter=1):
      # return tf.cond(tf.greater_equal(self.factor_step,
      # tf.convert_to_tensor(fac_iter)), lambda:
      # tf.group(*self._apply_stats(stats_list, varlist)), tf.no_op)
      return tf.group(*self._apply_stats(statsUpdates))

    if self._async_stats:
      # asynchronous stats update
      update_stats = self._apply_stats(statsUpdates)

      queue = tf.FIFOQueue(1, [item.dtype for item in update_stats], shapes=[
        item.get_shape() for item in update_stats])
      enqueue_op = queue.enqueue(update_stats)

      def dequeue_stats_op():
        return queue.dequeue()
      self.qr_stats = tf.train.QueueRunner(queue, [enqueue_op])
      update_stats_op = tf.cond(tf.equal(queue.size(), tf.convert_to_tensor(
        0)), tf.no_op, lambda: tf.group(*[dequeue_stats_op(), ]))
    else:
      # synchronous stats update
      update_stats_op = tf.cond(tf.greater_equal(
        self.stats_step, self._stats_accum_iter), lambda: updateRunningAvgStats(statsUpdates), updateAccumStats)
    self._update_stats_op = update_stats_op
    return update_stats_op


问题


面经


文章

微信
公众号

扫码关注公众号