Instantiates a Model from its config (output of `get_config()`). Arguments: config: Model config dictionary. custom_objects: Optional dictionary mapping names (strings) to custom classes or functions to be considered during deserialization. Returns:
(cls, config, custom_objects=None)
| 964 | |
| 965 | @classmethod |
| 966 | def from_config(cls, config, custom_objects=None): |
| 967 | """Instantiates a Model from its config (output of `get_config()`). |
| 968 | |
| 969 | Arguments: |
| 970 | config: Model config dictionary. |
| 971 | custom_objects: Optional dictionary mapping names |
| 972 | (strings) to custom classes or functions to be |
| 973 | considered during deserialization. |
| 974 | |
| 975 | Returns: |
| 976 | A model instance. |
| 977 | |
| 978 | Raises: |
| 979 | ValueError: In case of improperly formatted config dict. |
| 980 | """ |
| 981 | # Layer instances created during the graph reconstruction process. |
| 982 | created_layers = collections.OrderedDict() |
| 983 | |
| 984 | # Dictionary mapping layer instances to |
| 985 | # node data that specifies a layer call. |
| 986 | # It acts as a queue that maintains any unprocessed |
| 987 | # layer call until it becomes possible to process it |
| 988 | # (i.e. until the input tensors to the call all exist). |
| 989 | unprocessed_nodes = {} |
| 990 | |
| 991 | def add_unprocessed_node(layer, node_data): |
| 992 | if layer not in unprocessed_nodes: |
| 993 | unprocessed_nodes[layer] = [node_data] |
| 994 | else: |
| 995 | unprocessed_nodes[layer].append(node_data) |
| 996 | |
| 997 | def process_node(layer, node_data): |
| 998 | """Deserialize a node. |
| 999 | |
| 1000 | Arguments: |
| 1001 | layer: layer instance. |
| 1002 | node_data: Nested structure of `ListWrapper`. |
| 1003 | |
| 1004 | Raises: |
| 1005 | ValueError: In case of improperly formatted `node_data`. |
| 1006 | """ |
| 1007 | input_tensors = [] |
| 1008 | for input_data in nest.flatten(node_data): |
| 1009 | input_data = input_data.as_list() |
| 1010 | inbound_layer_name = input_data[0] |
| 1011 | inbound_node_index = input_data[1] |
| 1012 | inbound_tensor_index = input_data[2] |
| 1013 | if len(input_data) == 3: |
| 1014 | kwargs = {} |
| 1015 | elif len(input_data) == 4: |
| 1016 | kwargs = input_data[3] |
| 1017 | kwargs = _deserialize_keras_tensors(kwargs, created_layers) |
| 1018 | else: |
| 1019 | raise ValueError('Improperly formatted model config.') |
| 1020 | |
| 1021 | inbound_layer = created_layers[inbound_layer_name] |
| 1022 | if len(inbound_layer._inbound_nodes) <= inbound_node_index: |
| 1023 | add_unprocessed_node(layer, node_data) |