Reading H5 Groups and Attributes

37 views (last 30 days)
Karl Haebler
Karl Haebler on 22 Oct 2019
Commented: Jacek Dmochowski on 13 Oct 2022
Someone has ostensibly provided me with an h5 file (generated in python) that contains the weights of a neural network. These I need to open in matlab.
However, when I used the h5info and h5disp functions in MATLAB, the datasets part of the h5 file info is empty. However, there seems to be dataset information within the group category of the file, as you can see below.
How should I properly be accessing the weights with this h5 file?
Using data = hdf5read(filename,datasetname) seems to not work for no matter what I provide as the datasetname. I cannot provide the .h5 file since it is an unsupported file type with this forum.
Thank you very much
hinfo =
Filename: '/home/sem19h2/Documents/Karl/Semester_Thesis/Scripts/New_matlab_model/matlab_karl_v2/_weights.h5'
Name: '/'
Groups: [3x1 struct]
Datasets: []
Datatypes: []
Links: []
Attributes: [3x1 struct]
HDF5 _weights.h5
Group '/'
Attributes:
'layer_names': 'lstm_55 ', 'dense_29', 'lstm_56 '
'backend': 'tensorflow'
'keras_version': '2.2.4'
Group '/dense_29'
Attributes:
'weight_names': 'dense_29/kernel:0', 'dense_29/bias:0 '
Group '/dense_29/dense_29'
Dataset 'bias:0'
Size: 70
MaxSize: 70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 70x70
MaxSize: 70x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Group '/lstm_55'
Attributes:
'weight_names': 'lstm_55/kernel:0 ', 'lstm_55/recurrent_kernel:0', 'lstm_55/bias:0 '
Group '/lstm_55/lstm_55'
Dataset 'bias:0'
Size: 280
MaxSize: 280
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 280x10
MaxSize: 280x10
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'recurrent_kernel:0'
Size: 280x70
MaxSize: 280x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Group '/lstm_56'
Attributes:
'weight_names': 'lstm_56/kernel:0 ', 'lstm_56/recurrent_kernel:0', 'lstm_56/bias:0 '
Group '/lstm_56/lstm_56'
Dataset 'bias:0'
Size: 16
MaxSize: 16
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'kernel:0'
Size: 16x70
MaxSize: 16x70
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
Dataset 'recurrent_kernel:0'
Size: 16x4
MaxSize: 16x4
Datatype: H5T_IEEE_F32LE (single)
ChunkSize: []
Filters: none
FillValue: 0.000000
  5 Comments
Said Murtaza Jallali
Said Murtaza Jallali on 4 Feb 2021
hey i have the same problem, have you solved it yet???
Jacek Dmochowski
Jacek Dmochowski on 13 Oct 2022
You need to provide the name of the group(s) to the second argument of h5read().
For example, for grabbing the weights of the "dense_29" layer, you could do:
dense_29_weights = h5read(h5_filename,'/dense_29/dense_29/kernel:0');
Hope this helps.

Sign in to comment.

Answers (1)

Sivylla Paraskevopoulou
Sivylla Paraskevopoulou on 12 Feb 2021
You might find useful the following two examples on how to import weights from a separate file.
1. Import Keras Network and Weights

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!