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


Python random_ops.random_shuffle方法代码示例

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


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

示例1: __init__

# 需要导入模块: from tensorflow.python.ops import random_ops [as 别名]
# 或者: from tensorflow.python.ops.random_ops import random_shuffle [as 别名]
def __init__(self,
               shuffle_fn=None,
               random_fn=None,
               random_next_sentence_threshold=0.5):
    """Creates an instance of the `NextSentencePredictionExtractor`.

    Args:
      shuffle_fn: An op that shuffles the sentences in a random order. Default
        uses tf.random.shuffle. The output of `shuffle_fn` are the candidates
        used for the random next sentence.
      random_fn: An op that returns a random float from [0, 1]. If the results
        of this function passes `random_next_sentence_threshold` then a random
        sentence is swapped in as it's accompanying segment.  Default uses
        tf.random.uniform.
      random_next_sentence_threshold: A float threshold that determines whether
        or not a random sentence is injected instead of the next sentence. The
        higher the threshold, the higher the likelihood of inserting a random
        sentence.
    """
    self._shuffle_fn = shuffle_fn or random_ops.random_shuffle
    self._random_fn = random_fn or random_ops.random_uniform

    self._random_next_sentence_threshold = random_next_sentence_threshold 
开发者ID:tensorflow,项目名称:text,代码行数:25,代码来源:segment_extractor_ops.py

示例2: get_grow_tensor

# 需要导入模块: from tensorflow.python.ops import random_ops [as 别名]
# 或者: from tensorflow.python.ops.random_ops import random_shuffle [as 别名]
def get_grow_tensor(self, weights, method):
    """Different ways to initialize new connections.

    Args:
      weights: tf.Tensor or Variable.
      method: str, available options: 'zeros', 'random_normal', 'random_uniform'
        and 'initial_value'

    Returns:
      tf.Tensor same shape and type as weights.

    Raises:
      ValueError, when the method is not valid.
    """
    if not isinstance(method, six.string_types):
      raise ValueError('Grow-Init: %s is not a string' % method)

    if method == 'zeros':
      grow_tensor = array_ops.zeros_like(weights, dtype=weights.dtype)
    elif method.startswith('initial_dist'):
      original_shape = weights.initial_value.shape
      divisor = extract_number(method)
      grow_tensor = array_ops.reshape(
          random_ops.random_shuffle(array_ops.reshape(
              weights.initial_value, [-1])),
          original_shape) / divisor
    elif method.startswith('random_normal'):
      stddev = math_ops.reduce_std(weights)
      divisor = extract_number(method)
      grow_tensor = self._random_normal(
          weights.shape, stddev=stddev, dtype=weights.dtype,
          seed=hash(weights.name + 'grow_init_n')) / divisor
    elif method.startswith('random_uniform'):
      mean = math_ops.reduce_mean(math_ops.abs(weights))
      divisor = extract_number(method)
      grow_tensor = self._random_uniform(
          weights.shape, minval=-mean, maxval=mean, dtype=weights.dtype,
          seed=hash(weights.name + 'grow_init_u')) / divisor
    else:
      raise ValueError('Grow-Init: %s is not a valid option.' % method)
    return grow_tensor 
开发者ID:google-research,项目名称:rigl,代码行数:43,代码来源:sparse_optimizers.py

示例3: call

# 需要导入模块: from tensorflow.python.ops import random_ops [as 别名]
# 或者: from tensorflow.python.ops.random_ops import random_shuffle [as 别名]
def call(self, inputs, training=None, **kwargs):
        inputs, memory = inputs
        batch_size = K.shape(inputs)[0]
        seq_len = K.shape(inputs)[1]
        mem_mask = K.tile(K.ones_like(memory[:, :, :1], dtype=K.floatx()), [1, 1, seq_len])

        # Build content mask with random permutation
        ranges = K.tile(K.expand_dims(K.arange(0, seq_len), axis=-1), [1, batch_size])
        if self.enabled:
            shuffle = random_shuffle(ranges)
        else:
            shuffle = ranges
        if self.directional:
            shuffled = K.in_train_phase(shuffle, ranges, training)
        else:
            if self.enabled:
                shuffled = K.in_train_phase(shuffle, ranges + seq_len, training)
            else:
                shuffled = ranges + seq_len
        ranges = K.expand_dims(K.permute_dimensions(ranges, [1, 0]), axis=-1)
        shuffled = K.expand_dims(K.permute_dimensions(shuffled, [1, 0]), axis=1)
        content_mask = K.cast(ranges <= shuffled, dtype=K.floatx())

        # Build query mask based on content mask
        ranges = K.arange(0, seq_len)
        eye = K.equal(K.expand_dims(ranges, axis=0), K.expand_dims(ranges, axis=-1))
        eye = K.expand_dims(K.cast(eye, dtype=K.floatx()), axis=0)
        query_mask = content_mask * (1.0 - eye)

        content_mask = K.concatenate([mem_mask, content_mask], axis=1)
        query_mask = K.concatenate([mem_mask, query_mask], axis=1)
        return [
            K.permute_dimensions(content_mask, [0, 2, 1]),
            K.permute_dimensions(query_mask, [0, 2, 1]),
        ] 
开发者ID:CyberZHG,项目名称:keras-xlnet,代码行数:37,代码来源:permutation.py

示例4: input_producer

# 需要导入模块: from tensorflow.python.ops import random_ops [as 别名]
# 或者: from tensorflow.python.ops.random_ops import random_shuffle [as 别名]
def input_producer(input_tensor, element_shape=None, num_epochs=None,
                   shuffle=True, seed=None, capacity=32, shared_name=None,
                   summary_name=None, name=None):
  """Output the rows of `input_tensor` to a queue for an input pipeline.

  Args:
    input_tensor: A tensor with the rows to produce. Must be at least
      one-dimensional. Must either have a fully-defined shape, or
      `element_shape` must be defined.
    element_shape: (Optional.) A `TensorShape` representing the shape of a
      row of `input_tensor`, if it cannot be inferred.
    num_epochs: (Optional.) An integer. If specified `input_producer` produces
      each row of `input_tensor` `num_epochs` times before generating an
      `OutOfRange` error. If not specified, `input_producer` can cycle through
      the rows of `input_tensor` an unlimited number of times.
    shuffle: (Optional.) A boolean. If true, the rows are randomly shuffled
      within each epoch.
    seed: (Optional.) An integer. The seed to use if `shuffle` is true.
    capacity: (Optional.) The capacity of the queue to be used for buffering
      the input.
    shared_name: (Optional.) If set, this queue will be shared under the given
      name across multiple sessions.
    summary_name: (Optional.) If set, a scalar summary for the current queue
      size will be generated, using this name as part of the tag.
    name: (Optional.) A name for queue.

  Returns:
    A queue with the output rows.  A `QueueRunner` for the queue is
    added to the current `QUEUE_RUNNER` collection of the current
    graph.

  Raises:
    ValueError: If the shape of the input cannot be inferred from the arguments.
  """
  with ops.name_scope(name, "input_producer", [input_tensor]):
    input_tensor = ops.convert_to_tensor(input_tensor, name="input_tensor")
    element_shape = input_tensor.get_shape()[1:].merge_with(element_shape)
    if not element_shape.is_fully_defined():
      raise ValueError("Either `input_tensor` must have a fully defined shape "
                       "or `element_shape` must be specified")

    if shuffle:
      input_tensor = random_ops.random_shuffle(input_tensor, seed=seed)

    input_tensor = limit_epochs(input_tensor, num_epochs)

    q = data_flow_ops.FIFOQueue(capacity=capacity,
                                dtypes=[input_tensor.dtype.base_dtype],
                                shapes=[element_shape],
                                shared_name=shared_name, name=name)
    enq = q.enqueue_many([input_tensor])
    queue_runner.add_queue_runner(queue_runner.QueueRunner(q, [enq]))
    if summary_name is not None:
      summary.scalar("queue/%s/%s" % (q.name, summary_name),
                     math_ops.cast(q.size(), dtypes.float32) * (1. / capacity))
    return q 
开发者ID:tobegit3hub,项目名称:deep_image_model,代码行数:58,代码来源:input.py


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