当前位置: 首页>>代码示例>>Python>>正文


Python tensorflow.rank方法代码示例

本文整理汇总了Python中tensorflow.rank方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.rank方法的具体用法?Python tensorflow.rank怎么用?Python tensorflow.rank使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tensorflow的用法示例。


在下文中一共展示了tensorflow.rank方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: expanded_shape

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def expanded_shape(orig_shape, start_dim, num_dims):
  """Inserts multiple ones into a shape vector.

  Inserts an all-1 vector of length num_dims at position start_dim into a shape.
  Can be combined with tf.reshape to generalize tf.expand_dims.

  Args:
    orig_shape: the shape into which the all-1 vector is added (int32 vector)
    start_dim: insertion position (int scalar)
    num_dims: length of the inserted all-1 vector (int scalar)
  Returns:
    An int32 vector of length tf.size(orig_shape) + num_dims.
  """
  with tf.name_scope('ExpandedShape'):
    start_dim = tf.expand_dims(start_dim, 0)  # scalar to rank-1
    before = tf.slice(orig_shape, [0], start_dim)
    add_shape = tf.ones(tf.reshape(num_dims, [1]), dtype=tf.int32)
    after = tf.slice(orig_shape, start_dim, [-1])
    new_shape = tf.concat([before, add_shape, after], 0)
    return new_shape 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:22,代码来源:ops.py

示例2: pad_tensor

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def pad_tensor(t, length):
  """Pads the input tensor with 0s along the first dimension up to the length.

  Args:
    t: the input tensor, assuming the rank is at least 1.
    length: a tensor of shape [1]  or an integer, indicating the first dimension
      of the input tensor t after padding, assuming length <= t.shape[0].

  Returns:
    padded_t: the padded tensor, whose first dimension is length. If the length
      is an integer, the first dimension of padded_t is set to length
      statically.
  """
  t_rank = tf.rank(t)
  t_shape = tf.shape(t)
  t_d0 = t_shape[0]
  pad_d0 = tf.expand_dims(length - t_d0, 0)
  pad_shape = tf.cond(
      tf.greater(t_rank, 1), lambda: tf.concat([pad_d0, t_shape[1:]], 0),
      lambda: tf.expand_dims(length - t_d0, 0))
  padded_t = tf.concat([t, tf.zeros(pad_shape, dtype=t.dtype)], 0)
  if not _is_tensor(length):
    padded_t = _set_dim_0(padded_t, length)
  return padded_t 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:26,代码来源:shape_utils.py

示例3: clip_tensor

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def clip_tensor(t, length):
  """Clips the input tensor along the first dimension up to the length.

  Args:
    t: the input tensor, assuming the rank is at least 1.
    length: a tensor of shape [1]  or an integer, indicating the first dimension
      of the input tensor t after clipping, assuming length <= t.shape[0].

  Returns:
    clipped_t: the clipped tensor, whose first dimension is length. If the
      length is an integer, the first dimension of clipped_t is set to length
      statically.
  """
  clipped_t = tf.gather(t, tf.range(length))
  if not _is_tensor(length):
    clipped_t = _set_dim_0(clipped_t, length)
  return clipped_t 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:19,代码来源:shape_utils.py

示例4: pad_or_clip_tensor

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def pad_or_clip_tensor(t, length):
  """Pad or clip the input tensor along the first dimension.

  Args:
    t: the input tensor, assuming the rank is at least 1.
    length: a tensor of shape [1]  or an integer, indicating the first dimension
      of the input tensor t after processing.

  Returns:
    processed_t: the processed tensor, whose first dimension is length. If the
      length is an integer, the first dimension of the processed tensor is set
      to length statically.
  """
  processed_t = tf.cond(
      tf.greater(tf.shape(t)[0], length),
      lambda: clip_tensor(t, length),
      lambda: pad_tensor(t, length))
  if not _is_tensor(length):
    processed_t = _set_dim_0(processed_t, length)
  return processed_t 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:22,代码来源:shape_utils.py

示例5: pool

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def pool(inputs, window_size, pooling_type, padding, strides=(1, 1)):
  """Pooling (supports "LEFT")."""
  with tf.name_scope("pool", values=[inputs]):
    static_shape = inputs.get_shape()
    if not static_shape or len(static_shape) != 4:
      raise ValueError("Inputs to conv must have statically known rank 4.")
    # Add support for left padding.
    if padding == "LEFT":
      assert window_size[0] % 2 == 1 and window_size[1] % 2 == 1
      if len(static_shape) == 3:
        width_padding = 2 * (window_size[1] // 2)
        padding_ = [[0, 0], [width_padding, 0], [0, 0]]
      else:
        height_padding = 2 * (window_size[0] // 2)
        cond_padding = tf.cond(
            tf.equal(shape_list(inputs)[2], 1), lambda: tf.constant(0),
            lambda: tf.constant(2 * (window_size[1] // 2)))
        width_padding = 0 if static_shape[2] == 1 else cond_padding
        padding_ = [[0, 0], [height_padding, 0], [width_padding, 0], [0, 0]]
      inputs = tf.pad(inputs, padding_)
      inputs.set_shape([static_shape[0], None, None, static_shape[3]])
      padding = "VALID"

  return tf.nn.pool(inputs, window_size, pooling_type, padding, strides=strides) 
开发者ID:akzaidi,项目名称:fine-lm,代码行数:26,代码来源:common_layers.py

示例6: shape_list

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def shape_list(x):
  """Return list of dims, statically where possible."""
  x = tf.convert_to_tensor(x)

  # If unknown rank, return dynamic shape
  if x.get_shape().dims is None:
    return tf.shape(x)

  static = x.get_shape().as_list()
  shape = tf.shape(x)

  ret = []
  for i in range(len(static)):
    dim = static[i]
    if dim is None:
      dim = shape[i]
    ret.append(dim)
  return ret 
开发者ID:akzaidi,项目名称:fine-lm,代码行数:20,代码来源:common_layers.py

示例7: matmul_gather_on_zeroth_axis

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def matmul_gather_on_zeroth_axis(params, indices, scope=None):
  """Matrix multiplication based implementation of tf.gather on zeroth axis.

  TODO(rathodv, jonathanhuang): enable sparse matmul option.

  Args:
    params: A float32 Tensor. The tensor from which to gather values.
      Must be at least rank 1.
    indices: A Tensor. Must be one of the following types: int32, int64.
      Must be in range [0, params.shape[0])
    scope: A name for the operation (optional).

  Returns:
    A Tensor. Has the same type as params. Values from params gathered
    from indices given by indices, with shape indices.shape + params.shape[1:].
  """
  with tf.name_scope(scope, 'MatMulGather'):
    params_shape = shape_utils.combined_static_and_dynamic_shape(params)
    indices_shape = shape_utils.combined_static_and_dynamic_shape(indices)
    params2d = tf.reshape(params, [params_shape[0], -1])
    indicator_matrix = tf.one_hot(indices, params_shape[0])
    gathered_result_flattened = tf.matmul(indicator_matrix, params2d)
    return tf.reshape(gathered_result_flattened,
                      tf.stack(indices_shape + params_shape[1:])) 
开发者ID:ahmetozlu,项目名称:vehicle_counting_tensorflow,代码行数:26,代码来源:ops.py

示例8: test_sequence_sigmoid_cross_entropy

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def test_sequence_sigmoid_cross_entropy(self):
        """Tests `texar.losses.test_sequence_sigmoid_cross_entropy`.
        """
        self._test_sequence_loss(
            tx.losses.sequence_sigmoid_cross_entropy,
            self._one_hot_labels, self._logits, self._sequence_length)

        self._test_sequence_loss(
            tx.losses.sequence_sigmoid_cross_entropy,
            self._one_hot_labels[:, :, 0],
            self._logits[:, :, 0],
            self._sequence_length)

        labels = tf.placeholder(dtype=tf.int32, shape=None)
        loss = tx.losses.sequence_sigmoid_cross_entropy(
            logits=self._logits[:, :, 0],
            labels=tf.to_float(labels),
            sequence_length=self._sequence_length)
        with self.test_session() as sess:
            rank = sess.run(
                tf.rank(loss),
                feed_dict={labels: np.ones([self._batch_size, self._max_time])})
            self.assertEqual(rank, 0) 
开发者ID:qkaren,项目名称:Counterfactual-StoryRW,代码行数:25,代码来源:mle_losses_test.py

示例9: is_same_dynamic_shape

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def is_same_dynamic_shape(x, y):
    """
    Whether `x` and `y` has the same dynamic shape.

    :param x: A Tensor.
    :param y: A Tensor.

    :return: A scalar Tensor of `bool`.
    """
    # There is a BUG of Tensorflow for not doing static shape inference
    # right in nested tf.cond()'s, so we are not comparing x and y's
    # shape directly but working with their concatenations.
    return tf.cond(
        tf.equal(tf.rank(x), tf.rank(y)),
        lambda: tf.reduce_all(tf.equal(
            tf.concat([tf.shape(x), tf.shape(y)], 0),
            tf.concat([tf.shape(y), tf.shape(x)], 0))),
        lambda: tf.convert_to_tensor(False, tf.bool)) 
开发者ID:thu-ml,项目名称:zhusuan,代码行数:20,代码来源:utils.py

示例10: get_shape_list

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def get_shape_list(tensor):
    """
    When the rank of `tensor` is known from the static shape, return a list
    where each item is either an `int` (known from the static shape) or a
    scalar `int32` Tensor (picked from the dynamic shape).

    When the rank of `tensor` is unknown, return `None`.

    :param tensor: A `tf.Tensor`.
    :return: A list or `None`.
    """
    static_shape = tensor.get_shape()
    if not static_shape:
        return None
    dynamic_shape = tf.shape(tensor)
    ret = [(val or dynamic_shape[i])
           for i, val in enumerate(static_shape.as_list())]
    return ret 
开发者ID:thu-ml,项目名称:zhusuan,代码行数:20,代码来源:utils.py

示例11: assert_rank_at_least

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def assert_rank_at_least(tensor, k, name):
    """
    Whether the rank of `tensor` is at least k.

    :param tensor: A Tensor to be checked.
    :param k: The least rank allowed.
    :param name: The name of `tensor` for error message.

    :return: The checked tensor.
    """
    static_shape = tensor.get_shape()
    shape_err_msg = '{} should have rank >= {}.'.format(name, k)
    if static_shape and (static_shape.ndims < k):
        raise ValueError(shape_err_msg)
    if not static_shape:
        _assert_shape_op = tf.assert_rank_at_least(
            tensor, k, message=shape_err_msg)
        with tf.control_dependencies([_assert_shape_op]):
            tensor = tf.identity(tensor)
    return tensor 
开发者ID:thu-ml,项目名称:zhusuan,代码行数:22,代码来源:utils.py

示例12: _crop

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def _crop(image, offset_height, offset_width, crop_height, crop_width):
  original_shape = tf.shape(image)

  rank_assertion = tf.Assert(
      tf.equal(tf.rank(image), 3),
      ['Rank of image must be equal to 3.'])
  cropped_shape = control_flow_ops.with_dependencies(
      [rank_assertion],
      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.
  image = control_flow_ops.with_dependencies(
      [size_assertion],
      tf.slice(image, offsets, cropped_shape))
  return tf.reshape(image, cropped_shape) 
开发者ID:CharlesShang,项目名称:FastMaskRCNN,代码行数:26,代码来源:utils.py

示例13: calculate_latent_loss

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def calculate_latent_loss(self, latent_weights):
        """ Calculate the latent loss in the form of KL divergence """
        for posterior in self.posteriors:
            # Minimize the chi squared divergence of the posterior 'f' from the prior 'g' (a
            # standard normal distribution), this amounts to minimizing the square of the difference
            # of the first moment of f from the first moment of g divided by the squared variance of
            # g (NOTE: mt_f is the t-th moment of the distribution f):
            #    min(chisq) = (m1_f - m1_g)^2 / sigma_g^2
            #
            # The idea behind using the chi squared divergence is that it is an upper bound for the
            # Kullback-Leibler divergence. The following inequality holds:
            #    KL(f||g) <= log(1 + Chi^2(f||g))
            #
            # So minimize this bound rather than the chi squared divergence directly
            mean, _ = self.compute_moments(posterior)

            axes = tf.range(1, tf.rank(mean))
            chisq = tf.log1p(tf.square(mean - self.prior.mean()) / self.prior.variance())
            chisq = tf.reduce_sum(latent_weights * chisq, axes)
            tf.losses.add_loss(tf.reduce_mean(chisq, name='chisq')) 
开发者ID:dojoteef,项目名称:glas,代码行数:22,代码来源:sample.py

示例14: compute_log_probs

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
def compute_log_probs(self, states, actions):
        """Compute log probabilities of inputted actions

        :param states (tf.Tensor): Tensors of inputs to NN
        :param actions (tf.Tensor): Tensors of NOT one-hot vector.
            They will be converted to one-hot vector inside this function.
        """
        param = self._compute_dist(states)
        actions = tf.one_hot(
            indices=tf.squeeze(actions),
            depth=self.action_dim)
        param["prob"] = tf.cond(
            tf.math.greater(tf.rank(actions), tf.rank(param["prob"])),
            lambda: tf.expand_dims(param["prob"], axis=0),
            lambda: param["prob"])
        actions = tf.cond(
            tf.math.greater(tf.rank(param["prob"]), tf.rank(actions)),
            lambda: tf.expand_dims(actions, axis=0),
            lambda: actions)
        log_prob = self.dist.log_likelihood(actions, param)
        return log_prob 
开发者ID:keiohta,项目名称:tf2rl,代码行数:23,代码来源:categorical_actor.py

示例15: _crop

# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import rank [as 别名]
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) 
开发者ID:ringringyi,项目名称:DOTA_models,代码行数:43,代码来源:vgg_preprocessing.py


注:本文中的tensorflow.rank方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。