Skip to content

fix: Fix the problem where the model.save_weights() can't save multi-layer weight #1137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/TensorFlowNET.Keras/Saving/hdf5_format.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
using System.Linq;
using System.Text.RegularExpressions;

namespace Tensorflow.Keras.Saving
{
public class hdf5_format
Expand Down Expand Up @@ -132,7 +134,9 @@ public static void load_weights_from_hdf5_group(long f, List<ILayer> layers)
var weight_names = load_attributes_from_hdf5_group(g, "weight_names");
foreach (var i_ in weight_names)
{
(success, Array result) = Hdf5.ReadDataset<float>(g, i_);
var vm = Regex.Replace(i_, "/", "$");
vm = i_.Split('/')[0] + "/$" + vm.Substring(i_.Split('/')[0].Length + 1, i_.Length - i_.Split('/')[0].Length - 1);
(success, Array result) = Hdf5.ReadDataset<float>(g, vm);
if (success)
weight_values.Add(np.array(result));
}
Expand Down Expand Up @@ -193,7 +197,8 @@ public static void save_weights_to_hdf5_group(long f, List<ILayer> layers)
if (name.IndexOf("/") > 1)
{
var crDataGroup = Hdf5.CreateOrOpenGroup(g, Hdf5Utils.NormalizedName(name.Split('/')[0]));
WriteDataset(crDataGroup, name.Split('/')[1], tensor);
var _name = Regex.Replace(name.Substring(name.Split('/')[0].Length, name.Length - name.Split('/')[0].Length), "/", "$");
WriteDataset(crDataGroup, _name, tensor);
Hdf5.CloseGroup(crDataGroup);
}
else
Expand Down