Prints a summary of a model. Arguments: model: Keras model instance. line_length: Total length of printed lines (e.g. set this to adapt the display to different terminal window sizes). positions: Relative or absolute positions of log elements in each line.
(model, line_length=None, positions=None, print_fn=None)
| 83 | |
| 84 | |
| 85 | def print_summary(model, line_length=None, positions=None, print_fn=None): |
| 86 | """Prints a summary of a model. |
| 87 | |
| 88 | Arguments: |
| 89 | model: Keras model instance. |
| 90 | line_length: Total length of printed lines |
| 91 | (e.g. set this to adapt the display to different |
| 92 | terminal window sizes). |
| 93 | positions: Relative or absolute positions of log elements in each line. |
| 94 | If not provided, defaults to `[.33, .55, .67, 1.]`. |
| 95 | print_fn: Print function to use. |
| 96 | It will be called on each line of the summary. |
| 97 | You can set it to a custom function |
| 98 | in order to capture the string summary. |
| 99 | It defaults to `print` (prints to stdout). |
| 100 | """ |
| 101 | if print_fn is None: |
| 102 | print_fn = print |
| 103 | |
| 104 | if model.__class__.__name__ == 'Sequential': |
| 105 | sequential_like = True |
| 106 | elif not model._is_graph_network: |
| 107 | # We treat subclassed models as a simple sequence of layers, for logging |
| 108 | # purposes. |
| 109 | sequential_like = True |
| 110 | else: |
| 111 | sequential_like = True |
| 112 | nodes_by_depth = model._nodes_by_depth.values() |
| 113 | nodes = [] |
| 114 | for v in nodes_by_depth: |
| 115 | if (len(v) > 1) or (len(v) == 1 and |
| 116 | len(nest.flatten(v[0].inbound_layers)) > 1): |
| 117 | # if the model has multiple nodes |
| 118 | # or if the nodes have multiple inbound_layers |
| 119 | # the model is no longer sequential |
| 120 | sequential_like = False |
| 121 | break |
| 122 | nodes += v |
| 123 | if sequential_like: |
| 124 | # search for shared layers |
| 125 | for layer in model.layers: |
| 126 | flag = False |
| 127 | for node in layer._inbound_nodes: |
| 128 | if node in nodes: |
| 129 | if flag: |
| 130 | sequential_like = False |
| 131 | break |
| 132 | else: |
| 133 | flag = True |
| 134 | if not sequential_like: |
| 135 | break |
| 136 | |
| 137 | if sequential_like: |
| 138 | line_length = line_length or 65 |
| 139 | positions = positions or [.45, .85, 1.] |
| 140 | if positions[-1] <= 1: |
| 141 | positions = [int(line_length * p) for p in positions] |
| 142 | # header names for the different log elements |
nothing calls this directly
no test coverage detected