MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / compute_output_shape

Method compute_output_shape

tensorflow/python/keras/layers/recurrent.py:428–477  ·  view source on GitHub ↗
(self, input_shape)

Source from the content-addressed store, hash-verified

426 self._states = states
427
428 def compute_output_shape(self, input_shape):
429 if isinstance(input_shape, list):
430 input_shape = input_shape[0]
431 # Check whether the input shape contains any nested shapes. It could be
432 # (tensor_shape(1, 2), tensor_shape(3, 4)) or (1, 2, 3) which is from numpy
433 # inputs.
434 try:
435 input_shape = tensor_shape.as_shape(input_shape)
436 except (ValueError, TypeError):
437 # A nested tensor input
438 input_shape = nest.flatten(input_shape)[0]
439
440 batch = input_shape[0]
441 time_step = input_shape[1]
442 if self.time_major:
443 batch, time_step = time_step, batch
444
445 if _is_multiple_state(self.cell.state_size):
446 state_size = self.cell.state_size
447 else:
448 state_size = [self.cell.state_size]
449
450 def _get_output_shape(flat_output_size):
451 output_dim = tensor_shape.as_shape(flat_output_size).as_list()
452 if self.return_sequences:
453 if self.time_major:
454 output_shape = tensor_shape.as_shape([time_step, batch] + output_dim)
455 else:
456 output_shape = tensor_shape.as_shape([batch, time_step] + output_dim)
457 else:
458 output_shape = tensor_shape.as_shape([batch] + output_dim)
459 return output_shape
460
461 if getattr(self.cell, 'output_size', None) is not None:
462 # cell.output_size could be nested structure.
463 output_shape = nest.flatten(nest.map_structure(
464 _get_output_shape, self.cell.output_size))
465 output_shape = output_shape[0] if len(output_shape) == 1 else output_shape
466 else:
467 # Note that state_size[0] could be a tensor_shape or int.
468 output_shape = _get_output_shape(state_size[0])
469
470 if self.return_state:
471 def _get_state_shape(flat_state):
472 state_shape = [batch] + tensor_shape.as_shape(flat_state).as_list()
473 return tensor_shape.as_shape(state_shape)
474 state_shape = nest.map_structure(_get_state_shape, state_size)
475 return generic_utils.to_list(output_shape) + nest.flatten(state_shape)
476 else:
477 return output_shape
478
479 def compute_mask(self, inputs, mask):
480 # Time step masks must be the same for each input.

Calls 3

_is_multiple_stateFunction · 0.85
flattenMethod · 0.45
to_listMethod · 0.45