Specifies the ndim, dtype and shape of every input to a layer. Every layer should expose (if appropriate) an `input_spec` attribute: a list of instances of InputSpec (one per input tensor). A None entry in a shape is compatible with any dimension, a None shape is compatible with any shape.
| 32 | @keras_export('keras.layers.InputSpec') |
| 33 | @tf_export(v1=['layers.InputSpec']) |
| 34 | class InputSpec(object): |
| 35 | """Specifies the ndim, dtype and shape of every input to a layer. |
| 36 | |
| 37 | Every layer should expose (if appropriate) an `input_spec` attribute: |
| 38 | a list of instances of InputSpec (one per input tensor). |
| 39 | |
| 40 | A None entry in a shape is compatible with any dimension, |
| 41 | a None shape is compatible with any shape. |
| 42 | |
| 43 | Arguments: |
| 44 | dtype: Expected DataType of the input. |
| 45 | shape: Shape tuple, expected shape of the input |
| 46 | (may include None for unchecked axes). |
| 47 | ndim: Integer, expected rank of the input. |
| 48 | max_ndim: Integer, maximum rank of the input. |
| 49 | min_ndim: Integer, minimum rank of the input. |
| 50 | axes: Dictionary mapping integer axes to |
| 51 | a specific dimension value. |
| 52 | """ |
| 53 | |
| 54 | def __init__(self, |
| 55 | dtype=None, |
| 56 | shape=None, |
| 57 | ndim=None, |
| 58 | max_ndim=None, |
| 59 | min_ndim=None, |
| 60 | axes=None): |
| 61 | self.dtype = dtypes.as_dtype(dtype).name if dtype is not None else None |
| 62 | if shape is not None: |
| 63 | self.ndim = len(shape) |
| 64 | self.shape = shape |
| 65 | else: |
| 66 | self.ndim = ndim |
| 67 | self.shape = None |
| 68 | self.max_ndim = max_ndim |
| 69 | self.min_ndim = min_ndim |
| 70 | try: |
| 71 | axes = axes or {} |
| 72 | self.axes = {int(k): axes[k] for k in axes} |
| 73 | except (ValueError, TypeError): |
| 74 | raise TypeError('The keys in axes must be integers.') |
| 75 | |
| 76 | if self.axes and (self.ndim is not None or self.max_ndim is not None): |
| 77 | max_dim = (self.ndim if self.ndim else self.max_ndim) - 1 |
| 78 | max_axis = max(self.axes) |
| 79 | if max_axis > max_dim: |
| 80 | raise ValueError('Axis {} is greater than the maximum allowed value: {}' |
| 81 | .format(max_axis, max_dim)) |
| 82 | |
| 83 | def __repr__(self): |
| 84 | spec = [('dtype=' + str(self.dtype)) if self.dtype else '', |
| 85 | ('shape=' + str(self.shape)) if self.shape else '', |
| 86 | ('ndim=' + str(self.ndim)) if self.ndim else '', |
| 87 | ('max_ndim=' + str(self.max_ndim)) if self.max_ndim else '', |
| 88 | ('min_ndim=' + str(self.min_ndim)) if self.min_ndim else '', |
| 89 | ('axes=' + str(self.axes)) if self.axes else ''] |
| 90 | return 'InputSpec(%s)' % ', '.join(x for x in spec if x) |
| 91 |
no outgoing calls
no test coverage detected