Saving h5 weights in numpy format

Kavya
3 min readJun 8, 2023

--

Hdf5 or h5 is an abbreviated form of ‘hierarchichal data fromat version 5’. The main purpose of hdf5 is to store large amount of data in the form of multi-dimensional array. Primary goal of this format is the quick retrieval and modification well-structured data. Almost all programming languages such as python, c, c++ etc. supports this format, which makes it more popular in the field of computer science.

In the context of machine learning, the h5 file format is often used to save and load neural network weights. During model training the weight parametrs are constantly updated to reduce the loss function and improve model accuracy. Once the training is over, we can save these model weights for future use. The h5 file format provides efficient way for saving neural network weights. By saving the weights in an .h5 file, we can easily load and reuse them later without having to retrain the model from scratch.

Let’s see an example. Consider a small model,

i = tf.keras.Input(2,)
x = tf.keras.layers.Dense(8, activation='relu')(i)
x = tf.keras.layers.Dense(16, activation='relu')(x)
x = tf.keras.layers.Dense(32, activation='tanh')(x)
x = tf.keras.layers.Dense(8, activation='tanh')(x)
x = tf.keras.layers.Dense(1, activation='sigmoid')(x)
model = tf.keras.models.Model(i,x)

I’ve trained this model using GPU from kaggle., and if you’re intersted you can find it here. After training I saved the model weights using given line of code

model.save("and_logic_gate.h5")

we can view this model using netron, and clicking on each layer will give the information about layer parameters.

Netron view of the model and_logic_gate.h5

In above sanp i’ve selected first dense layer, so the kernel weight associated with this layer is shown. They are just numbers stored in arrays and we need to save this weight using numpy.save()

First we need to load the saved model (or we can directly save weights in numpy format from the model). we can load keras model using

model = tf.keras.models.load_model("and_logic_gate.h5")

We need to save kernel weight of first dense layer. We can access model parameters using model.variables and it’ll give a list.

we can access first dense layer kernel weights using list indexing

we can convert it into a numpy array using .numpy()

You can compare this weights with our netron model to confirm its authenticity.

Next use numpy.save() to save kernel weights

to load this ‘kernel_weight.npy’ we can use numpy.load()

What if we need to save whole model weights in numpy format? use the given block of code for this purpose

from pathlib import Path
for variable in model.variables:
path_list = variable.name.replace(":0","").split("/")
dir_path = "weights/"+ ("/").join(path_list[:-1])
file_name = path_list[-1]
Path(dir_path).mkdir(parents=True, exist_ok=True)
np.save(dir_path+"/"+file_name+".npy",variable.numpy())

This will create a directory named ‘weights’ with subdirectories for each model layer.

--

--