Skip to content

Weight loading: skip_mismatch is not respected for H5 format #21404

Open
@DLumi

Description

@DLumi

The skip_mismatch flag when loading H5 weights is not respected unless by_name is also specified.
Otherwise, it just fails with an error:

ValueError                                Traceback (most recent call last)
[/tmp/ipython-input-7-1152335395.py](https://localhost:8080/#) in <cell line: 0>()
----> 1 m2.load_weights('test_save.h5', skip_mismatch=True)

1 frames
[/usr/local/lib/python3.11/dist-packages/keras/src/legacy/saving/legacy_h5_format.py](https://localhost:8080/#) in _set_weights(instance, symbolic_weights, weight_values, name, skip_mismatch)
    438                 )
    439                 continue
--> 440             raise ValueError(
    441                 f"Shape mismatch in {name}"
    442                 f"for weight {symbolic_weights[i].path}. "

ValueError: Shape mismatch in layer #2 (named dense_5)for weight dense_5/kernel. Weight expects shape (256, 25). Received saved weight with shape (256, 10)

I think it either needs to be fixed, or documented as a feature the same way it's documented that by_name is unsupported with .keras format.
By the way, despite being documented, I think loading .keras with by_name provided could use a better error message than this:
ValueError: Invalid keyword arguments: {'by_name': True}

Colab notebook:
https://colab.research.google.com/drive/14uH8nQkhKH0KSwozFosCJSiOnGm7I7A-

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions