當前位置: 首頁>>代碼示例>>Python>>正文


Python nest.pack_sequence_as方法代碼示例

本文整理匯總了Python中tensorflow.python.util.nest.pack_sequence_as方法的典型用法代碼示例。如果您正苦於以下問題:Python nest.pack_sequence_as方法的具體用法?Python nest.pack_sequence_as怎麽用?Python nest.pack_sequence_as使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tensorflow.python.util.nest的用法示例。


在下文中一共展示了nest.pack_sequence_as方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: transpose_batch_time

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def transpose_batch_time(inputs):
    """Transposes inputs between time-major and batch-major.

    Args:
        inputs: A Tensor of shape `[batch_size, max_time, ...]` (batch-major)
            or `[max_time, batch_size, ...]` (time-major), or a (possibly
            nested) tuple of such elements.

    Returns:
        A (possibly nested tuple of) Tensor with transposed batch and
        time dimensions of inputs.
    """
    flat_input = nest.flatten(inputs)
    flat_input = [ops.convert_to_tensor(input_) for input_ in flat_input]
    # pylint: disable=protected-access
    flat_input = [rnn._transpose_batch_time(input_) for input_ in flat_input]
    return nest.pack_sequence_as(structure=inputs, flat_sequence=flat_input) 
開發者ID:qkaren,項目名稱:Counterfactual-StoryRW,代碼行數:19,代碼來源:shapes.py

示例2: _create

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def _create(self):
        # Concat bridge inputs on the depth dimensions
        bridge_input = nest.map_structure(
            lambda x: tf.reshape(x, [self.batch_size, _total_tensor_depth(x)]),
            self._bridge_input)
        bridge_input_flat = nest.flatten([bridge_input])
        bridge_input_concat = tf.concat(bridge_input_flat, axis=1)

        state_size_splits = nest.flatten(self.decoder_state_size)
        total_decoder_state_size = sum(state_size_splits)

        # Pass bridge inputs through a fully connected layer layer
        initial_state_flat = tf.contrib.layers.fully_connected(
            bridge_input_concat,
            num_outputs=total_decoder_state_size,
            activation_fn=self._activation_fn,
            weights_initializer=tf.truncated_normal_initializer(
                stddev=self.parameter_init),
            biases_initializer=tf.zeros_initializer(),
            scope=None)

        # Shape back into required state size
        initial_state = tf.split(initial_state_flat, state_size_splits, axis=1)
        return nest.pack_sequence_as(self.decoder_state_size, initial_state) 
開發者ID:hirofumi0810,項目名稱:tensorflow_end2end_speech_recognition,代碼行數:26,代碼來源:bridge.py

示例3: get_next

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def get_next(self, name=None):
    """Returns a nested structure of `tf.Tensor`s containing the next element.

    Args:
      name: (Optional.) A name for the created operation.

    Returns:
      A nested structure of `tf.Tensor` objects.
    """
    return nest.pack_sequence_as(
        self._output_types,
        gen_dataset_ops.iterator_get_next(
            self._iterator_resource,
            output_types=nest.flatten(self._output_types),
            output_shapes=nest.flatten(self._output_shapes),
            name=name)) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:18,代碼來源:dataset_ops.py

示例4: _zero_state

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def _zero_state(self, img, att, presence, state, transform_features, transform_state=False):

        with tf.variable_scope(self.__class__.__name__) as vs:
            features = self.extract_features(img, att)[1]

            if transform_features:
                features_flat = tf.reshape(features, (-1, self.n_units))
                features_flat = AffineLayer(features_flat, self.n_units, name='init_feature_transform').output
                features = tf.reshape(features_flat, tf.shape(features))

            rnn_outputs, hidden_state = self._propagate(features, state)

            hidden_state = nest.flatten(hidden_state)

            if transform_state:
                for i, hs in enumerate(hidden_state):
                    name = 'init_state_transform_{}'.format(i)
                    hidden_state[i] = AffineLayer(hs, self.n_units, name=name).output

            state = nest.pack_sequence_as(structure=state, flat_sequence=hidden_state)
        self.rnn_vs = vs
        return state, rnn_outputs 
開發者ID:akosiorek,項目名稱:hart,代碼行數:24,代碼來源:attention_ops.py

示例5: unflatten_features_and_labels

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def unflatten_features_and_labels(self, flattened_inputs):
      """Restores the flattened inputs to original features and labels form.

      Args:
        flattened_inputs: Flattened inputs for each shard.

      Returns:
        A tuple of (`features`, `labels`), where `labels` could be None.
        Each one, if present, should have identical structure (single tensor vs
        dict) as the one returned by input_fn.

      Raises:
        ValueError: If the number of expected tensors from `flattened_inputs`
          mismatches the recorded structure.
      """

      unflattened_inputs = data_nest.pack_sequence_as(self._feature_structure,
                                                      flattened_inputs)
      return _Inputs(
          unflattened_inputs['features'],
          unflattened_inputs.get('labels'),
          signals=unflattened_inputs.get('signals')) 
開發者ID:ymcui,項目名稱:Chinese-XLNet,代碼行數:24,代碼來源:tpu_estimator.py

示例6: map_nested

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def map_nested(map_fn, nested):
  """Executes map_fn on every element in a (potentially) nested structure.

  Args:
    map_fn: A callable to execute on each element in 'nested'.
    nested: A potentially nested combination of sequence objects. Sequence
      objects include tuples, lists, namedtuples, and all subclasses of
      collections.Sequence except strings. See nest.is_sequence for details.
      For example [1, ('hello', 4.3)] is a nested structure containing elements
      1, 'hello', and 4.3.
  Returns:
    out_structure: A potentially nested combination of sequence objects with the
      same structure as the 'nested' input argument. out_structure
      contains the result of applying map_fn to each element in 'nested'. For
      example map_nested(lambda x: x+1, [1, (3, 4.3)]) returns [2, (4, 5.3)].
  """
  out = map(map_fn, nest.flatten(nested))
  return nest.pack_sequence_as(nested, out) 
開發者ID:rky0930,項目名稱:yolo_v2,代碼行數:20,代碼來源:nested_utils.py

示例7: map_nested

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def map_nested(map_fn, nested):
  """Executes map_fn on every element in a (potentially) nested structure.

  Args:
    map_fn: A callable to execute on each element in 'nested'.
    nested: A potentially nested combination of sequence objects. Sequence
      objects include tuples, lists, namedtuples, and all subclasses of
      collections.Sequence except strings. See nest.is_sequence for details.
      For example [1, ('hello', 4.3)] is a nested structure containing elements
      1, 'hello', and 4.3.
  Returns:
    out_structure: A potentially nested combination of sequence objects with the
      same structure as the 'nested' input argument. out_structure
      contains the result of applying map_fn to each element in 'nested'. For
      example map_nested(lambda x: x+1, [1, (3, 4.3)]) returns [2, (4, 5.3)].
  """
  out = list(map(map_fn, nest.flatten(nested)))
  return nest.pack_sequence_as(nested, out) 
開發者ID:dnguyengithub,項目名稱:MultitaskAIS,代碼行數:20,代碼來源:nested_utils.py

示例8: trainable_initial_state

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def trainable_initial_state(batch_size, state_size,
                            initializer=None, name="initial_state"):
  flat_state_size = nest.flatten(state_size)

  if not initializer:
    flat_initializer = tuple(tf.zeros_initializer for _ in flat_state_size)
  else:
    flat_initializer = tuple(tf.zeros_initializer for initializer in flat_state_size)

  names = ["{}_{}".format(name, i) for i in xrange(len(flat_state_size))]
  tiled_states = []

  for name, size, init in zip(names, flat_state_size, flat_initializer):
    shape_with_batch_dim = [1, size]
    initial_state_variable = tf.get_variable(
        name, shape=shape_with_batch_dim, initializer=init())

    tiled_state = tf.tile(initial_state_variable,
                          [batch_size, 1], name=(name + "_tiled"))
    tiled_states.append(tiled_state)

  return nest.pack_sequence_as(structure=state_size,
                               flat_sequence=tiled_states) 
開發者ID:devsisters,項目名稱:neural-combinatorial-rl-tensorflow,代碼行數:25,代碼來源:layers.py

示例9: static_rnn

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def static_rnn(cell, inputs, scope=None):
    """Simple version of static_rnn."""
    with tf.variable_scope(scope or "rnn") as varscope:
        batch_size = dimension(inputs, axis=1)
        state = cell.zero_state(batch_size, tf.float32)
        flat_inputs = nest.flatten(inputs)
        flat_inputs = list(zip(*[tf.unstack(flat_input, axis=0) for flat_input in flat_inputs]))
        flat_outputs = []
        for time, flat_input in enumerate(flat_inputs):
            if time > 0:
                varscope.reuse_variables()
            input_ = nest.pack_sequence_as(inputs, flat_input)
            output, state = cell(input_, state)
            flat_output = nest.flatten(output)
            flat_outputs.append(flat_output)
        flat_outputs = [tf.stack(flat_output, axis=0) for flat_output in zip(*flat_outputs)]
        outputs = nest.pack_sequence_as(output, flat_outputs)
        return outputs, state 
開發者ID:alexlee-gk,項目名稱:video_prediction,代碼行數:20,代碼來源:tf_utils.py

示例10: gather_states

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def gather_states(states, beam_ids):
    """ Gathers states according to beam ids.

    Args:
        states: A Tensor of a list/tuple/dict of Tensors. For each Tensor, the first
          dimension must be batch_size, otherwise, unknow errors may occur.
        beam_ids: A tensor with shape [batch_size, ] that used to gather states.

    Returns: A Tensor or a list/tuple of Tensors with the same structure
      as `states`.
    """

    def _gather(x):
        assert isinstance(x, tf.Tensor)
        return tf.gather(x, beam_ids)

    return nest.pack_sequence_as(
        states,
        nest.map_structure(
            _gather, nest.flatten(states))) 
開發者ID:zhaocq-nlp,項目名稱:NJUNMT-tf,代碼行數:22,代碼來源:beam_search.py

示例11: batch

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def batch(self, batch_size=None):
    """Get a batch of tensors."""
    if self.produces_batches:
      assert batch_size is None, 'Cannot enforce a batch size if `func()` returns batches!'
      flat_batch = self._queue.dequeue()
      for name, pl in self.flat_placeholders.items():
        flat_batch[name].set_shape(pl.shape)

    else:
      flat_batch = self._queue.dequeue_many(batch_size)

    batch = Struct()
    for name, pl in self.placeholders.items():
      flat_vals = sorted((k, v)
                         for k, v in flat_batch.items() if k.startswith(name))
      vals = [v for k, v in flat_vals]
      batch[name] = vals[0] if len(
          vals) == 0 else nest.pack_sequence_as(pl, vals)

    return batch 
開發者ID:neocxi,項目名稱:pixelsnail-public,代碼行數:22,代碼來源:tf_utils.py

示例12: where_tensors

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def where_tensors(condition, x_tensors, y_tensors):
  """Performs a tf.where operation on a two sets of Tensors.

  Args:
    condition: The condition tensor to use for the where operation.
    x_tensors: A potentially nested tuple or list of Tensors.
    y_tensors: A potentially nested tuple or list of Tensors. Must have the
    same structure as x_tensors.
  Returns:
    whered_tensors: A potentially nested tuple or list of Tensors with the
      same structure as the 'tensors' input argument. Contains the result of
      applying tf.where(condition, x, y) on each pair of elements in x_tensors
      and y_tensors.
  """
  flat_x = nest.flatten(x_tensors)
  flat_y = nest.flatten(y_tensors)
  result = [tf.where(condition, x, y) for x, y in
            itertools.izip(flat_x, flat_y)]

  return nest.pack_sequence_as(x_tensors, result) 
開發者ID:generalized-iou,項目名稱:g-tensorflow-models,代碼行數:22,代碼來源:nested_utils.py

示例13: _forward

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def _forward(self, x1, x2):
    """Run forward through the reversible layers."""

    side_inputs = [self.f_side_input, self.g_side_input]
    flat_side_inputs = nest.flatten(side_inputs)

    def _forward_wrap(x1_, x2_, *flat_side_inputs):
      f_side, g_side = nest.pack_sequence_as(side_inputs, flat_side_inputs)
      return _rev_block_forward(
          x1_,
          x2_,
          self.f,
          self.g,
          num_layers=self.num_layers,
          f_side_input=f_side,
          g_side_input=g_side,
          gate_outputs=self._use_efficient_backprop)

    @custom_gradient.custom_gradient
    def _forward_with_custom_grad(*args):
      out = _forward_wrap(*args)  # pylint: disable=no-value-for-parameter
      grad_fn = self._make_efficient_grad_fn(args, out)
      return out, grad_fn

    if self._use_efficient_backprop:
      return _forward_with_custom_grad(x1, x2, *flat_side_inputs)
    else:
      return _forward_wrap(x1, x2, *flat_side_inputs) 
開發者ID:taehoonlee,項目名稱:tensornets,代碼行數:30,代碼來源:rev_block_lib.py

示例14: BuildLoop

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def BuildLoop(self, pred, body, loop_vars, shape_invariants):
    """Add the loop termination condition and body to the graph."""

    # Keep original_loop_vars to identify which are TensorArrays
    original_loop_vars = loop_vars
    # Convert TensorArrays to their flow variables
    loop_vars = nest.map_structure(_convert_tensorarray_to_flow,
                                   nest.flatten(loop_vars))
    loop_vars = ops.convert_n_to_tensor_or_indexed_slices(loop_vars)
    try:
      self.Enter()
      original_body_result, exit_vars = self._BuildLoop(
          pred, body, original_loop_vars, loop_vars, shape_invariants)
    finally:
      self.Exit()

    flat_result = nest.flatten(original_body_result)
    # Convert TensorArray flow variables outside the context back into
    # their associated TensorArrays for returning to caller.
    exit_vars_with_tensor_arrays = (
        _convert_flows_to_tensorarrays(flat_result, exit_vars))
    packed_exit_vars = nest.pack_sequence_as(
        structure=original_body_result,
        flat_sequence=exit_vars_with_tensor_arrays)
    return (packed_exit_vars[0] if len(exit_vars) == 1
            else packed_exit_vars) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:28,代碼來源:control_flow_ops.py

示例15: __init__

# 需要導入模塊: from tensorflow.python.util import nest [as 別名]
# 或者: from tensorflow.python.util.nest import pack_sequence_as [as 別名]
def __init__(self, tensors):
    """See `Dataset.from_tensors()` for details."""
    super(TensorDataset, self).__init__()
    with ops.name_scope("tensors"):
      self._tensors = nest.pack_sequence_as(tensors, [
          ops.convert_to_tensor(t, name="component_%d" % i)
          for i, t in enumerate(nest.flatten(tensors))
      ]) 
開發者ID:ryfeus,項目名稱:lambda-packs,代碼行數:10,代碼來源:dataset_ops.py


注:本文中的tensorflow.python.util.nest.pack_sequence_as方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。