Keras 3 API 文件 / 模型 API / 儲存 & 序列化 / Keras 權重檔案編輯器

Keras 權重檔案編輯器

[source]

KerasFileEditor 類別

keras.saving.KerasFileEditor(filepath)

用於檢查、編輯和重新儲存 Keras 權重檔案的工具。

當您在對模型架構進行變更後,需要調整舊的已儲存權重檔案時,您會發現這個類別非常有用。

參數

  • filepath:要檢查和編輯的本地檔案路徑。

範例

editor = KerasFileEditor("my_model.weights.h5")

# Displays current contents
editor.summary()

# Remove the weights of an existing layer
editor.delete_object("layers/dense_2")

# Add the weights of a new layer
editor.add_object("layers/einsum_dense", weights={"0": ..., "1": ...})

# Save the weights of the edited model
editor.resave_weights("edited_model.weights.h5")

[source]

summary 方法

KerasFileEditor.summary()

印出已開啟檔案的權重結構。


[source]

compare 方法

KerasFileEditor.compare(reference_model)

將已開啟的檔案與參考模型進行比較。

這個方法會列出目前已開啟的檔案與提供的參考模型之間的所有不匹配之處。

參數

  • reference_model:要比較的模型實例。

回傳

  • 具有以下鍵的字典'status''error_count''match_count'。狀態可以是 'success''error''error_count' 是找到的不匹配數量。'match_count' 是找到的匹配權重數量。

[source]

save 方法

KerasFileEditor.save(filepath)

儲存已編輯的權重檔案。

參數

  • filepath:儲存檔案的路徑。必須是 .weights.h5 檔案。

[source]

rename_object 方法

KerasFileEditor.rename_object(object_name, new_name)

重新命名檔案中的物件 (例如:層)。

參數

  • object_name:字串,要重新命名的物件名稱或路徑 (例如:"dense_2""layers/dense_2")。
  • new_name:字串,物件的新名稱。

[source]

delete_object 方法

KerasFileEditor.delete_object(object_name)

從檔案中移除物件 (例如:層)。

參數

  • object_name:字串,要刪除的物件名稱或路徑 (例如:"dense_2""layers/dense_2")。

[source]

add_object 方法

KerasFileEditor.add_object(object_path, weights)

在檔案中新增一個新物件 (例如:層)。

參數

  • object_path:字串,要新增物件的完整路徑 (例如:"layers/dense_2")。
  • weights:字典,將權重名稱對應到權重值 (陣列),例如:{"0": kernel_value, "1": bias_value}

[source]

delete_weight 方法

KerasFileEditor.delete_weight(object_name, weight_name)

從現有物件中移除權重。

參數

  • object_name:字串,要從中移除權重的物件名稱或路徑 (例如:"dense_2""layers/dense_2")。
  • weight_name:字串,要刪除的權重名稱 (例如:"0")。

[source]

add_weights 方法

KerasFileEditor.add_weights(object_name, weights)

在現有物件中新增一個或多個新權重。

參數

  • object_name:字串,要新增權重的物件名稱或路徑 (例如:"dense_2""layers/dense_2")。
  • weights:字典,將權重名稱對應到權重值 (陣列),例如:{"0": kernel_value, "1": bias_value}