|
| 1 | +Help on class KerasFileEditor in module keras.src.saving.file_editor: |
| 2 | + |
| 3 | +class KerasFileEditor(builtins.object) |
| 4 | + | KerasFileEditor(filepath) |
| 5 | + | |
| 6 | + | Utility to inspect, edit, and resave Keras weights files. |
| 7 | + | |
| 8 | + | You will find this class useful when adapting |
| 9 | + | an old saved weights file after having made |
| 10 | + | architecture changes to a model. |
| 11 | + | |
| 12 | + | Args: |
| 13 | + | filepath: The path to a local file to inspect and edit. |
| 14 | + | |
| 15 | + | Examples: |
| 16 | + | |
| 17 | + | ```python |
| 18 | + | editor = KerasFileEditor("my_model.weights.h5") |
| 19 | + | |
| 20 | + | # Displays current contents |
| 21 | + | editor.summary() |
| 22 | + | |
| 23 | + | # Remove the weights of an existing layer |
| 24 | + | editor.delete_object("layers/dense_2") |
| 25 | + | |
| 26 | + | # Add the weights of a new layer |
| 27 | + | editor.add_object("layers/einsum_dense", weights={"0": ..., "1": ...}) |
| 28 | + | |
| 29 | + | # Save the weights of the edited model |
| 30 | + | editor.resave_weights("edited_model.weights.h5") |
| 31 | + | ``` |
| 32 | + | |
| 33 | + | Methods defined here: |
| 34 | + | |
| 35 | + | __init__(self, filepath) |
| 36 | + | Initialize self. See help(type(self)) for accurate signature. |
| 37 | + | |
| 38 | + | add_object( |
| 39 | + | self, |
| 40 | + | object_path, |
| 41 | + | weights |
| 42 | + | ) |
| 43 | + | Add a new object to the file (e.g. a layer). |
| 44 | + | |
| 45 | + | Args: |
| 46 | + | object_path: String, full path of the |
| 47 | + | object to add (e.g. `"layers/dense_2"`). |
| 48 | + | weights: Dict mapping weight names to weight |
| 49 | + | values (arrays), |
| 50 | + | e.g. `{"0": kernel_value, "1": bias_value}`. |
| 51 | + | |
| 52 | + | add_weights( |
| 53 | + | self, |
| 54 | + | object_name, |
| 55 | + | weights |
| 56 | + | ) |
| 57 | + | Add one or more new weights to an existing object. |
| 58 | + | |
| 59 | + | Args: |
| 60 | + | object_name: String, name or path of the |
| 61 | + | object to add the weights to |
| 62 | + | (e.g. `"dense_2"` or `"layers/dense_2"`). |
| 63 | + | weights: Dict mapping weight names to weight |
| 64 | + | values (arrays), |
| 65 | + | e.g. `{"0": kernel_value, "1": bias_value}`. |
| 66 | + | |
| 67 | + | compare(self, reference_model) |
| 68 | + | Compares the opened file to a reference model. |
| 69 | + | |
| 70 | + | This method will list all mismatches between the |
| 71 | + | currently opened file and the provided reference model. |
| 72 | + | |
| 73 | + | Args: |
| 74 | + | reference_model: Model instance to compare to. |
| 75 | + | |
| 76 | + | Returns: |
| 77 | + | Dict with the following keys: |
| 78 | + | `'status'`, `'error_count'`, `'match_count'`. |
| 79 | + | Status can be `'success'` or `'error'`. |
| 80 | + | `'error_count'` is the number of mismatches found. |
| 81 | + | `'match_count'` is the number of matching weights found. |
| 82 | + | |
| 83 | + | delete_object(self, object_name) |
| 84 | + | Removes an object from the file (e.g. a layer). |
| 85 | + | |
| 86 | + | Args: |
| 87 | + | object_name: String, name or path of the |
| 88 | + | object to delete (e.g. `"dense_2"` or |
| 89 | + | `"layers/dense_2"`). |
| 90 | + | |
| 91 | + | delete_weight( |
| 92 | + | self, |
| 93 | + | object_name, |
| 94 | + | weight_name |
| 95 | + | ) |
| 96 | + | Removes a weight from an existing object. |
| 97 | + | |
| 98 | + | Args: |
| 99 | + | object_name: String, name or path of the |
| 100 | + | object from which to remove the weight |
| 101 | + | (e.g. `"dense_2"` or `"layers/dense_2"`). |
| 102 | + | weight_name: String, name of the weight to |
| 103 | + | delete (e.g. `"0"`). |
| 104 | + | |
| 105 | + | rename_object( |
| 106 | + | self, |
| 107 | + | object_name, |
| 108 | + | new_name |
| 109 | + | ) |
| 110 | + | Rename an object in the file (e.g. a layer). |
| 111 | + | |
| 112 | + | Args: |
| 113 | + | object_name: String, name or path of the |
| 114 | + | object to rename (e.g. `"dense_2"` or |
| 115 | + | `"layers/dense_2"`). |
| 116 | + | new_name: String, new name of the object. |
| 117 | + | |
| 118 | + | resave_weights(self, filepath) |
| 119 | + | |
| 120 | + | save(self, filepath) |
| 121 | + | Save the edited weights file. |
| 122 | + | |
| 123 | + | Args: |
| 124 | + | filepath: Path to save the file to. |
| 125 | + | Must be a `.weights.h5` file. |
| 126 | + | |
| 127 | + | summary(self) |
| 128 | + | Prints the weight structure of the opened file. |
| 129 | + | |
| 130 | + | ---------------------------------------------------------------------- |
| 131 | + | Data descriptors defined here: |
| 132 | + | |
| 133 | + | __dict__ |
| 134 | + | dictionary for instance variables |
| 135 | + | |
| 136 | + | __weakref__ |
| 137 | + | list of weak references to the object |
| 138 | + |
0 commit comments