Validate that all layers in `model` have constant shape.
(model)
| 2146 | |
| 2147 | # pylint: disable=bad-continuation |
| 2148 | def _validate_shapes(model): |
| 2149 | """Validate that all layers in `model` have constant shape.""" |
| 2150 | for layer in model.layers: |
| 2151 | if isinstance(layer.input_shape, tuple): |
| 2152 | input_shapes = [layer.input_shape] |
| 2153 | else: |
| 2154 | input_shapes = layer.input_shape |
| 2155 | |
| 2156 | if isinstance(layer.output_shape, tuple): |
| 2157 | output_shapes = [layer.output_shape] |
| 2158 | else: |
| 2159 | output_shapes = layer.output_shape |
| 2160 | |
| 2161 | for shape in input_shapes + output_shapes: |
| 2162 | for dim in shape[1:]: |
| 2163 | if dim is None: |
| 2164 | raise ValueError( |
| 2165 | """ |
| 2166 | Layer %(layer)s has a variable shape in a non-batch dimension. TPU models must |
| 2167 | have constant shapes for all operations. |
| 2168 | |
| 2169 | You may have to specify `input_length` for RNN/TimeDistributed layers. |
| 2170 | |
| 2171 | Layer: %(layer)s |
| 2172 | Input shape: %(input_shape)s |
| 2173 | Output shape: %(output_shape)s |
| 2174 | """ % { |
| 2175 | 'layer': layer, |
| 2176 | 'input_shape': layer.input_shape, |
| 2177 | 'output_shape': layer.output_shape |
| 2178 | }) |
| 2179 | |
| 2180 | |
| 2181 | # pylint: enable=bad-continuation |