| 491 | return output_mask |
| 492 | |
| 493 | def build(self, input_shape): |
| 494 | if isinstance(input_shape, list): |
| 495 | input_shape = input_shape[0] |
| 496 | # The input_shape here could be a nest structure. |
| 497 | |
| 498 | # do the tensor_shape to shapes here. The input could be single tensor, or a |
| 499 | # nested structure of tensors. |
| 500 | def get_input_spec(shape): |
| 501 | if isinstance(shape, tensor_shape.TensorShape): |
| 502 | input_spec_shape = shape.as_list() |
| 503 | else: |
| 504 | input_spec_shape = list(shape) |
| 505 | batch_index, time_step_index = (1, 0) if self.time_major else (0, 1) |
| 506 | if not self.stateful: |
| 507 | input_spec_shape[batch_index] = None |
| 508 | input_spec_shape[time_step_index] = None |
| 509 | return InputSpec(shape=tuple(input_spec_shape)) |
| 510 | |
| 511 | def get_step_input_shape(shape): |
| 512 | if isinstance(shape, tensor_shape.TensorShape): |
| 513 | shape = tuple(shape.as_list()) |
| 514 | # remove the timestep from the input_shape |
| 515 | return shape[1:] if self.time_major else (shape[0],) + shape[2:] |
| 516 | |
| 517 | # Check whether the input shape contains any nested shapes. It could be |
| 518 | # (tensor_shape(1, 2), tensor_shape(3, 4)) or (1, 2, 3) which is from numpy |
| 519 | # inputs. |
| 520 | try: |
| 521 | input_shape = tensor_shape.as_shape(input_shape) |
| 522 | except (ValueError, TypeError): |
| 523 | # A nested tensor input |
| 524 | pass |
| 525 | |
| 526 | if not nest.is_sequence(input_shape): |
| 527 | # This indicates the there is only one input. |
| 528 | if self.input_spec is not None: |
| 529 | self.input_spec[0] = get_input_spec(input_shape) |
| 530 | else: |
| 531 | self.input_spec = [get_input_spec(input_shape)] |
| 532 | step_input_shape = get_step_input_shape(input_shape) |
| 533 | else: |
| 534 | if self.input_spec is not None: |
| 535 | self.input_spec[0] = nest.map_structure(get_input_spec, input_shape) |
| 536 | else: |
| 537 | self.input_spec = generic_utils.to_list( |
| 538 | nest.map_structure(get_input_spec, input_shape)) |
| 539 | step_input_shape = nest.map_structure(get_step_input_shape, input_shape) |
| 540 | |
| 541 | # allow cell (if layer) to build before we set or validate state_spec |
| 542 | if isinstance(self.cell, Layer): |
| 543 | if not self.cell.built: |
| 544 | self.cell.build(step_input_shape) |
| 545 | |
| 546 | # set or validate state_spec |
| 547 | if _is_multiple_state(self.cell.state_size): |
| 548 | state_size = list(self.cell.state_size) |
| 549 | else: |
| 550 | state_size = [self.cell.state_size] |