MCPcopy Create free account
hub / github.com/alibaba/bigcomputing / _reverse_seq

Function _reverse_seq

DIEN/rnn.py:268–307  ·  view source on GitHub ↗

Reverse a list of Tensors up to specified lengths. Args: input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features) or nested tuples of tensors. lengths: A `Tensor` of dimension batch_size, containing lengths for each sequence in the batch

(input_seq, lengths)

Source from the content-addressed store, hash-verified

266
267
268def _reverse_seq(input_seq, lengths):
269 """Reverse a list of Tensors up to specified lengths.
270
271 Args:
272 input_seq: Sequence of seq_len tensors of dimension (batch_size, n_features)
273 or nested tuples of tensors.
274 lengths: A `Tensor` of dimension batch_size, containing lengths for each
275 sequence in the batch. If "None" is specified, simply reverses
276 the list.
277
278 Returns:
279 time-reversed sequence
280 """
281 if lengths is None:
282 return list(reversed(input_seq))
283
284 flat_input_seq = tuple(nest.flatten(input_) for input_ in input_seq)
285
286 flat_results = [[] for _ in range(len(input_seq))]
287 for sequence in zip(*flat_input_seq):
288 input_shape = tensor_shape.unknown_shape(
289 ndims=sequence[0].get_shape().ndims)
290 for input_ in sequence:
291 input_shape.merge_with(input_.get_shape())
292 input_.set_shape(input_shape)
293
294 # Join into (time, batch_size, depth)
295 s_joined = array_ops.stack(sequence)
296
297 # Reverse along dimension 0
298 s_reversed = array_ops.reverse_sequence(s_joined, lengths, 0, 1)
299 # Split again into list
300 result = array_ops.unstack(s_reversed)
301 for r, flat_result in zip(result, flat_results):
302 r.set_shape(input_shape)
303 flat_result.append(r)
304
305 results = [nest.pack_sequence_as(structure=input_, flat_sequence=flat_result)
306 for input_, flat_result in zip(input_seq, flat_results)]
307 return results
308
309
310def bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None,

Callers 1

static_bidirectional_rnnFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected