Loads all layer weights, either from a TensorFlow or an HDF5 weight file. If `by_name` is False weights are loaded based on the network's topology. This means the architecture should be the same as when the weights were saved. Note that layers that don't have weights are not taken into
(self, filepath, by_name=False)
| 1292 | all_model_checkpoint_paths=[filepath]) |
| 1293 | |
| 1294 | def load_weights(self, filepath, by_name=False): |
| 1295 | """Loads all layer weights, either from a TensorFlow or an HDF5 weight file. |
| 1296 | |
| 1297 | If `by_name` is False weights are loaded based on the network's |
| 1298 | topology. This means the architecture should be the same as when the weights |
| 1299 | were saved. Note that layers that don't have weights are not taken into |
| 1300 | account in the topological ordering, so adding or removing layers is fine as |
| 1301 | long as they don't have weights. |
| 1302 | |
| 1303 | If `by_name` is True, weights are loaded into layers only if they share the |
| 1304 | same name. This is useful for fine-tuning or transfer-learning models where |
| 1305 | some of the layers have changed. |
| 1306 | |
| 1307 | Only topological loading (`by_name=False`) is supported when loading weights |
| 1308 | from the TensorFlow format. Note that topological loading differs slightly |
| 1309 | between TensorFlow and HDF5 formats for user-defined classes inheriting from |
| 1310 | `tf.keras.Model`: HDF5 loads based on a flattened list of weights, while the |
| 1311 | TensorFlow format loads based on the object-local names of attributes to |
| 1312 | which layers are assigned in the `Model`'s constructor. |
| 1313 | |
| 1314 | Arguments: |
| 1315 | filepath: String, path to the weights file to load. For weight files in |
| 1316 | TensorFlow format, this is the file prefix (the same as was passed |
| 1317 | to `save_weights`). |
| 1318 | by_name: Boolean, whether to load weights by name or by topological |
| 1319 | order. Only topological loading is supported for weight files in |
| 1320 | TensorFlow format. |
| 1321 | |
| 1322 | Returns: |
| 1323 | When loading a weight file in TensorFlow format, returns the same status |
| 1324 | object as `tf.train.Checkpoint.restore`. When graph building, restore |
| 1325 | ops are run automatically as soon as the network is built (on first call |
| 1326 | for user-defined classes inheriting from `Model`, immediately if it is |
| 1327 | already built). |
| 1328 | |
| 1329 | When loading weights in HDF5 format, returns `None`. |
| 1330 | |
| 1331 | Raises: |
| 1332 | ImportError: If h5py is not available and the weight file is in HDF5 |
| 1333 | format. |
| 1334 | """ |
| 1335 | if _is_hdf5_filepath(filepath): |
| 1336 | save_format = 'h5' |
| 1337 | else: |
| 1338 | try: |
| 1339 | pywrap_tensorflow.NewCheckpointReader(filepath) |
| 1340 | save_format = 'tf' |
| 1341 | except errors_impl.DataLossError: |
| 1342 | # The checkpoint is not readable in TensorFlow format. Try HDF5. |
| 1343 | save_format = 'h5' |
| 1344 | if save_format == 'tf': |
| 1345 | status = self._trackable_saver.restore(filepath) |
| 1346 | if by_name: |
| 1347 | raise NotImplementedError( |
| 1348 | 'Weights may only be loaded based on topology into Models when ' |
| 1349 | 'loading TensorFlow-formatted weights (got by_name=True to ' |
| 1350 | 'load_weights).') |
| 1351 | if not context.executing_eagerly(): |
nothing calls this directly
no test coverage detected