如何将Keras各轮次训练权重合并保存为单个文件?
Hey there! Let's work through this problem together. I’ve run into similar scenarios when managing Keras model checkpoints, so here’s how you can merge those 50 weight files into a single one, depending on your needs:
Scenario 1: You Need a Single Final Weight File (Most Common Use Case)
If your goal is to get a single weight file for converting to a TensorFlow checkpoint (which is typically all you need for deployment or further training), you don’t actually need to "merge" all files—you just need to pick the best/final epoch’s weights and save them as a single file. Here’s how:
First, recreate your exact Keras model architecture (this has to match the model you used during training perfectly, down to layer types, shapes, and configurations):
# Example model architecture (replace with your actual model code) from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D, Flatten, Dense def build_your_model(): model = Sequential([ Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(28,28,1)), Flatten(), Dense(10, activation='softmax') ]) return model # Initialize the model model = build_your_model()Next, load the weights from your desired epoch (usually the last one, i.e.,
checkpoint_epoch_49.hdf5if you started counting from 0):model.load_weights('checkpoint_epoch_49.hdf5')Finally, save this single set of weights as your target
checkpoint.h5file:model.save_weights('checkpoint.h5')
That’s it! This checkpoint.h5 file is ready to use with TensorFlow just like any standard Keras weight file.
Scenario 2: You Want to Store All 50 Epochs’ Weights in One File
If you need to keep all epoch weights for analysis (like tracking weight changes over training) instead of just one, you can bundle them into a single HDF5 file using the h5py library. Here’s how:
First, install
h5pyif you haven’t already:pip install h5pyThen run this code to load and merge all weights:
import h5py # Use the same model architecture function from Scenario 1 from your_model_script import build_your_model model = build_your_model() # Create a new HDF5 file to hold all epoch weights with h5py.File('all_epochs_merged_weights.h5', 'w') as merged_file: for epoch in range(50): # Load weights for the current epoch weight_path = f'checkpoint_epoch_{epoch}.hdf5' model.load_weights(weight_path) # Get the model's weight tensors layer_weights = model.get_weights() # Create a group in the HDF5 file for this epoch epoch_group = merged_file.create_group(f'epoch_{epoch}') # Save each layer's weights as a dataset in the group for layer_idx, weights in enumerate(layer_weights): epoch_group.create_dataset(f'layer_{layer_idx}', data=weights)
To retrieve weights from this merged file later (e.g., load epoch 10’s weights into your model):
import h5py import numpy as np from your_model_script import build_your_model model = build_your_model() with h5py.File('all_epochs_merged_weights.h5', 'r') as merged_file: # Fetch weights for epoch 10 target_epoch_weights = [] for layer_key in merged_file['epoch_10'].keys(): target_epoch_weights.append(np.array(merged_file['epoch_10'][layer_key])) # Load weights into the model model.set_weights(target_epoch_weights)
内容的提问来源于stack exchange,提问作者Black Snow

