Home > Mobile >  Should tf.keras.models.clone_model preserve the hash?
Should tf.keras.models.clone_model preserve the hash?

Time:06-27

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?

CodePudding user response:

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?

  • Related