Builds the model based on input shapes received. This is to be used for subclassed models, which do not know at instantiation time what their inputs look like. This method only exists for users who want to call `model.build()` in a standalone way (as a substitute for calling the mo
(self, input_shape)
| 575 | |
| 576 | @base_layer_utils.default |
| 577 | def build(self, input_shape): |
| 578 | """Builds the model based on input shapes received. |
| 579 | |
| 580 | This is to be used for subclassed models, which do not know at instantiation |
| 581 | time what their inputs look like. |
| 582 | |
| 583 | This method only exists for users who want to call `model.build()` in a |
| 584 | standalone way (as a substitute for calling the model on real data to |
| 585 | build it). It will never be called by the framework (and thus it will |
| 586 | never throw unexpected errors in an unrelated workflow). |
| 587 | |
| 588 | Args: |
| 589 | input_shape: Single tuple, TensorShape, or list of shapes, where shapes |
| 590 | are tuples, integers, or TensorShapes. |
| 591 | |
| 592 | Raises: |
| 593 | ValueError: |
| 594 | 1. In case of invalid user-provided data (not of type tuple, |
| 595 | list, or TensorShape). |
| 596 | 2. If the model requires call arguments that are agnostic |
| 597 | to the input shapes (positional or kwarg in call signature). |
| 598 | 3. If not all layers were properly built. |
| 599 | 4. If float type inputs are not supported within the layers. |
| 600 | |
| 601 | In each of these cases, the user should build their model by calling it |
| 602 | on real tensor data. |
| 603 | """ |
| 604 | if self._is_graph_network: |
| 605 | self.built = True |
| 606 | return |
| 607 | |
| 608 | # If subclass network |
| 609 | if input_shape is None: |
| 610 | raise ValueError('Input shape must be defined when calling build on a ' |
| 611 | 'model subclass network.') |
| 612 | valid_types = (tuple, list, tensor_shape.TensorShape) |
| 613 | if not isinstance(input_shape, valid_types): |
| 614 | raise ValueError('Specified input shape is not one of the valid types. ' |
| 615 | 'Please specify a batch input shape of type tuple or ' |
| 616 | 'list of input shapes. User provided ' |
| 617 | 'input type: {}'.format(type(input_shape))) |
| 618 | |
| 619 | if input_shape and not self.inputs: |
| 620 | # We create placeholders for the `None`s in the shape and build the model |
| 621 | # in a Graph. Since tf.Variable is compatible with both eager execution |
| 622 | # and graph building, the variables created after building the model in |
| 623 | # a Graph are still valid when executing eagerly. |
| 624 | if context.executing_eagerly(): |
| 625 | graph = func_graph.FuncGraph('build_graph') |
| 626 | else: |
| 627 | graph = backend.get_graph() |
| 628 | with graph.as_default(): |
| 629 | if isinstance(input_shape, list): |
| 630 | x = [base_layer_utils.generate_placeholders_from_shape(shape) |
| 631 | for shape in input_shape] |
| 632 | else: |
| 633 | x = base_layer_utils.generate_placeholders_from_shape(input_shape) |
| 634 |
nothing calls this directly
no test coverage detected