def concat(input_layer, concat_dim, other_tensors=None): """Concatenates input PrettyTensor with other_tensors along the specified dim. This adds the Pretty Tensor passed via input_layer to the front of the list of tensors to concat. Args: input_layer: The input layer. concat_dim: The dimension along which to concat. other_tensors: The tensors to concatenate with as an iterable or None if this is called on a sequence. Returns: A new PrettyTensor. Raises: ValueError: If other_tensors is None and this is not a sequence. """ if input_layer.is_sequence(): all_tensors = input_layer.sequence all_tensors.extend(other_tensors or []) else: all_tensors = [input_layer] if other_tensors is None: raise ValueError('Other Tensors must be supplied.') all_tensors.extend(other_tensors) # Edge cases really only apply when this is a sequence with 0 or 1 element. if not all_tensors: return prettytensor.wrap_sequence([]) else: return tf.concat(all_tensors, concat_dim)
def concat(input_layer, concat_dim, other_tensors=None): """Concatenates input PrettyTensor with other_tensors along the specified dim. This adds the Pretty Tensor passed via input_layer to the front of the list of tensors to concat. Args: input_layer: The input layer. concat_dim: The dimension along which to concat. other_tensors: The tensors to concatenate with as an iterable or None if this is called on a sequence. Returns: A new PrettyTensor. Raises: ValueError: If other_tensors is None and this is not a sequence. """ if input_layer.is_sequence(): all_tensors = input_layer.sequence all_tensors.extend(other_tensors or []) else: all_tensors = [input_layer] if other_tensors is None: raise ValueError("Other Tensors must be supplied.") all_tensors.extend(other_tensors) # Edge cases really only apply when this is a sequence with 0 or 1 element. if not all_tensors: return prettytensor.wrap_sequence([]) else: return tf.concat_v2(all_tensors, concat_dim)
def create_sequence_pretty_tensor(sequence_input, shape=None, save_state=True): """Creates a PrettyTensor object for the given sequence. The first dimension is treated as a time-dimension * batch and a default is set for `unroll` and `state_saver`. TODO(eiderman): Remove shape. Args: sequence_input: A SequenceInput or StateSavingSequenceInput shape: The shape of each item in the sequence (including batch). save_state: If true, use the sequence_input's state and save_state methods. Returns: 2 Layers: inputs, targets """ inputs = prettytensor.wrap_sequence(sequence_input.inputs, tensor_shape=shape) targets = prettytensor.wrap_sequence(sequence_input.targets) if save_state: bookkeeper.set_recurrent_state_saver(sequence_input) return inputs, targets
def create_sequence_pretty_tensor(sequence_input, shape=None, save_state=True): """Creates a PrettyTensor object for the given sequence. The first dimension is treated as a time-dimension * batch and a default is set for `unroll` and `state_saver`. TODO(eiderman): Remove shape. Args: sequence_input: A SequenceInput or StateSavingSequenceInput shape: The shape of each item in the sequence (including batch). save_state: If true, use the sequence_input's state and save_state methods. Returns: 2 Layers: inputs, targets """ inputs = prettytensor.wrap_sequence(sequence_input.inputs, tensor_shape=shape) targets = prettytensor.wrap_sequence(sequence_input.targets) if save_state: bookkeeper.set_recurrent_state_saver(sequence_input) return inputs, targets
def map_(input_layer, fn): """Maps the given function across this sequence. To map an entire template across the sequence, use the `as_fn` method on the template. Args: input_layer: The input tensor. fn: A function of 1 argument that is applied to each item in the sequence. Returns: A new sequence Pretty Tensor. """ return prettytensor.wrap_sequence([fn(x) for x in input_layer])
def map_(input_layer, fn): """Maps the given function across this sequence. To map an entire template across the sequence, use the `as_fn` method on the template. Args: input_layer: The input tensor. fn: A function of 1 argument that is applied to each item in the sequence. Returns: A new sequence Pretty Tensor. """ return prettytensor.wrap_sequence([fn(x) for x in input_layer])