我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用tensorflow.reverse_v2()。
def reverse(x, axes): """Reverse a tensor along the the specified axes # Returns A tensor. """ if isinstance(axes, int): axes = [axes] try: return tf.reverse_v2(x, axes) except AttributeError: # Older TF versions. dims = [True if i in axes else False for i in range(len(x.get_shape()._dims))] return tf.reverse(x, dims) # VALUE MANIPULATION
def ndlstm_base_dynamic(inputs, noutput, scope=None, reverse=False): """Run an LSTM, either forward or backward. This is a 1D LSTM implementation using dynamic_rnn and the TensorFlow LSTM op. Args: inputs: input sequence (length, batch_size, ninput) noutput: depth of output scope: optional scope name reverse: run LSTM in reverse Returns: Output sequence (length, batch_size, noutput) """ with tf.variable_scope(scope, "SeqLstm", [inputs]): # TODO(tmb) make batch size, sequence_length dynamic # example: sequence_length = tf.shape(inputs)[0] _, batch_size, _ = _shape(inputs) lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(noutput, state_is_tuple=False) state = tf.zeros([batch_size, lstm_cell.state_size]) sequence_length = int(inputs.get_shape()[0]) sequence_lengths = tf.to_int64(tf.fill([batch_size], sequence_length)) if reverse: inputs = tf.reverse_v2(inputs, [0]) outputs, _ = tf.nn.dynamic_rnn(lstm_cell, inputs, sequence_lengths, state, time_major=True) if reverse: outputs = tf.reverse_v2(outputs, [0]) return outputs
def reverse(x, axes): '''Reverse a tensor along the the specified axes ''' if isinstance(axes, int): axes = [axes] try: return tf.reverse_v2(x, axes) except AttributeError: # Older TF versions. dims = [True if i in axes else False for i in range(len(x.get_shape()._dims))] return tf.reverse(x, dims) # VALUE MANIPULATION
def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None): """Sort boxes and associated fields according to a scalar field. A common use case is reordering the boxes according to descending scores. Args: boxlist: BoxList holding N boxes. field: A BoxList field for sorting and reordering the BoxList. order: (Optional) descend or ascend. Default is descend. scope: name scope. Returns: sorted_boxlist: A sorted BoxList with the field in the specified order. Raises: ValueError: if specified field does not exist ValueError: if the order is not either descend or ascend """ with tf.name_scope(scope, 'SortByField'): if order != SortOrder.descend and order != SortOrder.ascend: raise ValueError('Invalid sort order') field_to_sort = boxlist.get_field(field) if len(field_to_sort.shape.as_list()) != 1: raise ValueError('Field should have rank 1') num_boxes = boxlist.num_boxes() num_entries = tf.size(field_to_sort) length_assert = tf.Assert( tf.equal(num_boxes, num_entries), ['Incorrect field size: actual vs expected.', num_entries, num_boxes]) with tf.control_dependencies([length_assert]): # TODO: Remove with tf.device when top_k operation runs correctly on GPU. with tf.device('/cpu:0'): _, sorted_indices = tf.nn.top_k(field_to_sort, num_boxes, sorted=True) if order == SortOrder.ascend: sorted_indices = tf.reverse_v2(sorted_indices, [0]) return gather(boxlist, sorted_indices)