Should tf.keras.models.clone_model preserve the hash?

Issue

In the context of training some deep Q network, I have to generate clone of a tensorflow model (containing dense neural networks and some activations). This is the syntax I am using.

target_model=tf.keras.models.clone_model(model=model)

My question is, why does not it preserve the hash, calculated using hash(model)? If these are two exact clones of each other, just at two different memory locations, should the hash change? Or is the memory location itself an input to the hash, rather than the objects themselves?

Solution

The hash of a model is dependent on the __hash__ function which isn’t overridden in the keras Model class. Therefore when the deepcopy is made with clone_model, the id value of the object changes and which means that the value of hash() also changes.

The answer below explains how the default hash function works.

What is the default __hash__ in python?

Answered By – peytoncas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published