Saves all layer weights. Either saves in HDF5 or in TensorFlow format based on the `save_format` argument. When saving in HDF5 format, the weight file has: - `layer_names` (attribute), a list of strings (ordered names of model layers). - For every layer, a `group`
(self, filepath, overwrite=True, save_format=None)
| 1171 | signatures) |
| 1172 | |
| 1173 | def save_weights(self, filepath, overwrite=True, save_format=None): |
| 1174 | """Saves all layer weights. |
| 1175 | |
| 1176 | Either saves in HDF5 or in TensorFlow format based on the `save_format` |
| 1177 | argument. |
| 1178 | |
| 1179 | When saving in HDF5 format, the weight file has: |
| 1180 | - `layer_names` (attribute), a list of strings |
| 1181 | (ordered names of model layers). |
| 1182 | - For every layer, a `group` named `layer.name` |
| 1183 | - For every such layer group, a group attribute `weight_names`, |
| 1184 | a list of strings |
| 1185 | (ordered names of weights tensor of the layer). |
| 1186 | - For every weight in the layer, a dataset |
| 1187 | storing the weight value, named after the weight tensor. |
| 1188 | |
| 1189 | When saving in TensorFlow format, all objects referenced by the network are |
| 1190 | saved in the same format as `tf.train.Checkpoint`, including any `Layer` |
| 1191 | instances or `Optimizer` instances assigned to object attributes. For |
| 1192 | networks constructed from inputs and outputs using `tf.keras.Model(inputs, |
| 1193 | outputs)`, `Layer` instances used by the network are tracked/saved |
| 1194 | automatically. For user-defined classes which inherit from `tf.keras.Model`, |
| 1195 | `Layer` instances must be assigned to object attributes, typically in the |
| 1196 | constructor. See the documentation of `tf.train.Checkpoint` and |
| 1197 | `tf.keras.Model` for details. |
| 1198 | |
| 1199 | While the formats are the same, do not mix `save_weights` and |
| 1200 | `tf.train.Checkpoint`. Checkpoints saved by `Model.save_weights` should be |
| 1201 | loaded using `Model.load_weights`. Checkpoints saved using |
| 1202 | `tf.train.Checkpoint.save` should be restored using the corresponding |
| 1203 | `tf.train.Checkpoint.restore`. Prefer `tf.train.Checkpoint` over |
| 1204 | `save_weights` for training checkpoints. |
| 1205 | |
| 1206 | The TensorFlow format matches objects and variables by starting at a root |
| 1207 | object, `self` for `save_weights`, and greedily matching attribute |
| 1208 | names. For `Model.save` this is the `Model`, and for `Checkpoint.save` this |
| 1209 | is the `Checkpoint` even if the `Checkpoint` has a model attached. This |
| 1210 | means saving a `tf.keras.Model` using `save_weights` and loading into a |
| 1211 | `tf.train.Checkpoint` with a `Model` attached (or vice versa) will not match |
| 1212 | the `Model`'s variables. See the [guide to training |
| 1213 | checkpoints](https://www.tensorflow.org/alpha/guide/checkpoints) for details |
| 1214 | on the TensorFlow format. |
| 1215 | |
| 1216 | Arguments: |
| 1217 | filepath: String, path to the file to save the weights to. When saving |
| 1218 | in TensorFlow format, this is the prefix used for checkpoint files |
| 1219 | (multiple files are generated). Note that the '.h5' suffix causes |
| 1220 | weights to be saved in HDF5 format. |
| 1221 | overwrite: Whether to silently overwrite any existing file at the |
| 1222 | target location, or provide the user with a manual prompt. |
| 1223 | save_format: Either 'tf' or 'h5'. A `filepath` ending in '.h5' or |
| 1224 | '.keras' will default to HDF5 if `save_format` is `None`. Otherwise |
| 1225 | `None` defaults to 'tf'. |
| 1226 | |
| 1227 | Raises: |
| 1228 | ImportError: If h5py is not available when attempting to save in HDF5 |
| 1229 | format. |
| 1230 | ValueError: For invalid/unknown format arguments. |