Verify that the required weights are set. Raises: ValueError: If a required weight is not set in the specification.
(self)
| 99 | """A layer specification declares the weights that should be set by the converters.""" |
| 100 | |
| 101 | def validate(self) -> None: |
| 102 | """Verify that the required weights are set. |
| 103 | |
| 104 | Raises: |
| 105 | ValueError: If a required weight is not set in the specification. |
| 106 | """ |
| 107 | unset_attributes = [] |
| 108 | |
| 109 | def _check(spec, name, value): |
| 110 | if value is None: |
| 111 | unset_attributes.append(name) |
| 112 | return |
| 113 | |
| 114 | if isinstance(value, np.ndarray): |
| 115 | # float64 is not a supported type. |
| 116 | if value.dtype == np.float64: |
| 117 | value = value.astype(np.float32) |
| 118 | elif isinstance(value, float): |
| 119 | value = np.dtype("float32").type(value) |
| 120 | elif isinstance(value, bool): |
| 121 | # Convert bool to an integer type. |
| 122 | value = np.dtype("int8").type(value) |
| 123 | elif isinstance(value, str): |
| 124 | if value != OPTIONAL: |
| 125 | value = np.frombuffer(value.encode("utf-8"), dtype=np.int8) |
| 126 | |
| 127 | if isinstance(value, np.ndarray) or isinstance(value, np.generic): |
| 128 | value = NumpyVariable(value) |
| 129 | elif torch_is_available and isinstance(value, torch.Tensor): |
| 130 | value = PyTorchVariable(value) |
| 131 | |
| 132 | attr_name = _split_scope(name)[-1] |
| 133 | setattr(spec, attr_name, value) |
| 134 | |
| 135 | self._visit(_check) |
| 136 | |
| 137 | if unset_attributes: |
| 138 | raise ValueError( |
| 139 | "Some required model attributes are not set:\n\n%s" |
| 140 | % "\n".join(unset_attributes) |
| 141 | ) |
| 142 | |
| 143 | def variables( |
| 144 | self, |