Skip to content
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

The problem has not been completely solved. #15

Open
ChineseDictionary opened this issue Sep 20, 2024 · 4 comments
Open

The problem has not been completely solved. #15

ChineseDictionary opened this issue Sep 20, 2024 · 4 comments

Comments

@ChineseDictionary
Copy link

          The problem has not been completely solved. When I try to load the model in another file in the same environment, the problem still occurs. How do we fix this? Thank you.

The save and load steps are the same as above. I have updated TKAN and keras_efficient_kan to the latest version. The error message is
ValueError: A total of 2 objects could not be loaded. Example error message for object :
The shape of the target variable and the shape of the target value in variable.assign(value) must match. variable.shape=(1, 10), Received: value.shape=(1, 10). Target variable: <KerasVariable shape=(1, 65, 10), dtype=float32, path=tkan/tkan_cell/kan_linear/grid>
List of objects that could not be loaded:
[<KANLinear name=kan_linear, built=True>, <KANLinear name=kan_linear_1, built=True>]
Originally posted by @ChineseDictionary in #12 (comment)

@ducanbk13
Copy link

Hi, I have the same problem. It shows something like: The shape of the target variable and the shape of the target value in variable.assign(value)` must match. variable.shape=(1, 10), Received: value.shape=(1, 10). Target variable: <KerasVariable shape=(1, 2, 10), dtype=float32, path=tkan_6/tkan_cell_6/kan_linear_6/grid>'

List of objects that could not be loaded:
[, , , ]
I tested in many ways from loading .h5 file to using default keras. All of them don't work at all.
Has anyone fixed that?

@remigenet
Copy link
Owner

Hi ! Back from holidays only now, sorry for the delay !
I found the origin of the issue, in facts it was linked to the GridInitialize from KANLinear, and was a tricky issue as the problem didn't occurs at all in jax (that's why I though it was working) but only in tensorflow and torch ! Fix involved just relying on numpy in the initializer instead of keras backend, as it seems either tile or another operation creates the saved weights to not have the same shape (I would guess something like tile in tensorflow and torch are just creating a form of view while jax really create the matrix of values, still unsure pure guess..)
I have added tests in all backend for this now and pushed a new version on pypi, so please just update to 0.4.3 and tell me if it's working now !

@ducanbk13
Copy link

Hi, I think the problem hasn't been resolved completely. I continue to get the problem with:
{
"name": "TypeError",
"message": "<class 'tKanModel.vaeTKAN'> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the model's from_config() method.

config={'module': 'tKanModel', 'class_name': 'vaeTKAN', 'config': {'name': 'vae_tkan', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}, 'registered_name': 'vaeTKAN', 'compile_config': {'optimizer': {'module': 'keras.optimizers', 'class_name': 'Adam', 'config': {'name': 'adam', 'learning_rate': 0.0010000000474974513, 'weight_decay': None, 'clipnorm': None, 'global_clipnorm': None, 'clipvalue': None, 'use_ema': False, 'ema_momentum': 0.99, 'ema_overwrite_frequency': None, 'loss_scale_factor': None, 'gradient_accumulation_steps': None, 'beta_1': 0.9, 'beta_2': 0.999, 'epsilon': 1e-07, 'amsgrad': False}, 'registered_name': None}, 'loss': None, 'loss_weights': None, 'metrics': None, 'weighted_metrics': None, 'run_eagerly': False, 'steps_per_execution': 1, 'jit_compile': True}}.

Exception encountered: Unable to revive model from config. When overriding the get_config() method, make sure that the returned config contains all items used as arguments in the constructor to <class 'tKanModel.vaeTKAN'>, which is the default behavior. You can override this default behavior by defining a from_config(cls, config) class method to specify how to create an instance of vaeTKAN from its config.

Received config={'name': 'vae_tkan', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}

Error encountered during deserialization: 'NoneType' object is not subscriptable",
"stack": "---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/models/model.py:538, in Model.from_config(cls, config, custom_objects)
537 try:
--> 538 return cls(**config)
539 except TypeError as e:

File ~/Documents/2ndrealdatajournalchile/tKanModel.py:31, in vaeTKAN.init(self, config, **kwargs)
30 super(vaeTKAN, self).init(**kwargs)
---> 31 self.seq_len = config['seqLength']
32 self.feat_dim = config['inFeatureDim']

TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py:718, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
717 try:
--> 718 instance = cls.from_config(inner_config)
719 except TypeError as e:

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/models/model.py:540, in Model.from_config(cls, config, custom_objects)
539 except TypeError as e:
--> 540 raise TypeError(
541 "Unable to revive model from config. When overriding "
542 "the get_config() method, make sure that the "
543 "returned config contains all items used as arguments "
544 f"in the constructor to {cls}, "
545 "which is the default behavior. "
546 "You can override this default behavior by defining a "
547 "from_config(cls, config) class method to specify "
548 "how to create an "
549 f"instance of {cls.name} from its config.

"
550 f"Received config={config}

"
551 f"Error encountered during deserialization: {e}"
552 )

TypeError: Unable to revive model from config. When overriding the get_config() method, make sure that the returned config contains all items used as arguments in the constructor to <class 'tKanModel.vaeTKAN'>, which is the default behavior. You can override this default behavior by defining a from_config(cls, config) class method to specify how to create an instance of vaeTKAN from its config.

Received config={'name': 'vae_tkan', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}

Error encountered during deserialization: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
Cell In[4], line 4
1 print("Saving the model ...............")
2 aiModel.save('ckpt/VAEConstrains/TKAN_my_model.keras') # Save as an HDF5 file
----> 4 aiModel = keras.models.load_model('ckpt/VAEConstrains/TKAN_my_model.keras')
5 # model.load_weights
6 # model = keras.models.load_model('best_model.h5', custom_objects={'C_constraint': C_constraint})
7
8 # Show the model architecture
9 aiModel.summary()

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/saving_api.py:189, in load_model(filepath, custom_objects, compile, safe_mode)
186 is_keras_zip = True
188 if is_keras_zip or is_keras_dir or is_hf:
--> 189 return saving_lib.load_model(
190 filepath,
191 custom_objects=custom_objects,
192 compile=compile,
193 safe_mode=safe_mode,
194 )
195 if str(filepath).endswith((".h5", ".hdf5")):
196 return legacy_h5_format.load_model_from_hdf5(
197 filepath, custom_objects=custom_objects, compile=compile
198 )

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/saving_lib.py:365, in load_model(filepath, custom_objects, compile, safe_mode)
360 raise ValueError(
361 "Invalid filename: expected a .keras extension. "
362 f"Received: filepath={filepath}"
363 )
364 with open(filepath, "rb") as f:
--> 365 return _load_model_from_fileobj(
366 f, custom_objects, compile, safe_mode
367 )

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/saving_lib.py:442, in _load_model_from_fileobj(fileobj, custom_objects, compile, safe_mode)
439 with zf.open(_CONFIG_FILENAME, "r") as f:
440 config_json = f.read()
--> 442 model = _model_from_config(
443 config_json, custom_objects, compile, safe_mode
444 )
446 all_filenames = zf.namelist()
447 extract_dir = None

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/saving_lib.py:431, in _model_from_config(config_json, custom_objects, compile, safe_mode)
429 # Construct the model from the configuration file in the archive.
430 with ObjectSharingScope():
--> 431 model = deserialize_keras_object(
432 config_dict, custom_objects, safe_mode=safe_mode
433 )
434 return model

File ~/anaconda3/envs/tensorflowNewest/lib/python3.12/site-packages/keras/src/saving/serialization_lib.py:720, in deserialize_keras_object(config, custom_objects, safe_mode, **kwargs)
718 instance = cls.from_config(inner_config)
719 except TypeError as e:
--> 720 raise TypeError(
721 f"{cls} could not be deserialized properly. Please"
722 " ensure that components that are Python object"
723 " instances (layers, models, etc.) returned by"
724 " get_config() are explicitly deserialized in the"
725 " model's from_config() method."
726 f"

config={config}.

Exception encountered: {e}"
727 )
728 build_config = config.get("build_config", None)
729 if build_config and not instance.built:

TypeError: <class 'tKanModel.vaeTKAN'> could not be deserialized properly. Please ensure that components that are Python object instances (layers, models, etc.) returned by get_config() are explicitly deserialized in the model's from_config() method.

config={'module': 'tKanModel', 'class_name': 'vaeTKAN', 'config': {'name': 'vae_tkan', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}, 'registered_name': 'vaeTKAN', 'compile_config': {'optimizer': {'module': 'keras.optimizers', 'class_name': 'Adam', 'config': {'name': 'adam', 'learning_rate': 0.0010000000474974513, 'weight_decay': None, 'clipnorm': None, 'global_clipnorm': None, 'clipvalue': None, 'use_ema': False, 'ema_momentum': 0.99, 'ema_overwrite_frequency': None, 'loss_scale_factor': None, 'gradient_accumulation_steps': None, 'beta_1': 0.9, 'beta_2': 0.999, 'epsilon': 1e-07, 'amsgrad': False}, 'registered_name': None}, 'loss': None, 'loss_weights': None, 'metrics': None, 'weighted_metrics': None, 'run_eagerly': False, 'steps_per_execution': 1, 'jit_compile': True}}.

Exception encountered: Unable to revive model from config. When overriding the get_config() method, make sure that the returned config contains all items used as arguments in the constructor to <class 'tKanModel.vaeTKAN'>, which is the default behavior. You can override this default behavior by defining a from_config(cls, config) class method to specify how to create an instance of vaeTKAN from its config.

Received config={'name': 'vae_tkan', 'trainable': True, 'dtype': {'module': 'keras', 'class_name': 'DTypePolicy', 'config': {'name': 'float32'}, 'registered_name': None}}

Error encountered during deserialization: 'NoneType' object is not subscriptable"
}

@remigenet
Copy link
Owner

Could you give a code to reproduce as well as packages versions ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants