Object of type device is not JSON serializable
See original GitHub issueHi,
I have trained my TabNet model with pretrainer + regressor with CUDA. However, upon completing the training, if I attempt to save the model, I get the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_932/2022071721.py in <module>
1 # saving the TABNET model
----> 2 fitted_tbn.selfsupervised.save_model("tbn_0.zip")
/opt/conda/envs/rapids/lib/python3.8/site-packages/pytorch_tabnet/abstract_model.py in save_model(self, path)
373 # Save models params
374 with open(Path(path).joinpath("model_params.json"), "w", encoding="utf8") as f:
--> 375 json.dump(saved_params, f, cls=ComplexEncoder)
376
377 # Save state_dict
/opt/conda/envs/rapids/lib/python3.8/json/__init__.py in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
177 # could accelerate with writelines in some versions of Python, at
178 # a debuggability cost
--> 179 for chunk in iterable:
180 fp.write(chunk)
181
/opt/conda/envs/rapids/lib/python3.8/json/encoder.py in _iterencode(o, _current_indent_level)
429 yield from _iterencode_list(o, _current_indent_level)
430 elif isinstance(o, dict):
--> 431 yield from _iterencode_dict(o, _current_indent_level)
432 else:
433 if markers is not None:
/opt/conda/envs/rapids/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
403 else:
404 chunks = _iterencode(value, _current_indent_level)
--> 405 yield from chunks
406 if newline_indent is not None:
407 _current_indent_level -= 1
/opt/conda/envs/rapids/lib/python3.8/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
403 else:
404 chunks = _iterencode(value, _current_indent_level)
--> 405 yield from chunks
406 if newline_indent is not None:
407 _current_indent_level -= 1
/opt/conda/envs/rapids/lib/python3.8/json/encoder.py in _iterencode(o, _current_indent_level)
436 raise ValueError("Circular reference detected")
437 markers[markerid] = o
--> 438 o = _default(o)
439 yield from _iterencode(o, _current_indent_level)
440 if markers is not None:
/opt/conda/envs/rapids/lib/python3.8/site-packages/pytorch_tabnet/utils.py in default(self, obj)
337 return int(obj)
338 # Let the base class default method raise the TypeError
--> 339 return json.JSONEncoder.default(self, obj)
/opt/conda/envs/rapids/lib/python3.8/json/encoder.py in default(self, o)
177
178 """
--> 179 raise TypeError(f'Object of type {o.__class__.__name__} '
180 f'is not JSON serializable')
181
TypeError: Object of type device is not JSON serializable
I have the following versions
poetry version: N/A
python version: 3.8.12
Operating System: Linux Kernel version 5.4.0-89-generic
CUDA: 11.2
I am using Paperspace Gradient with Quadro P5000 16 GB GPU.
About reproducible code:
I am providing as much context as possible since the training data is yielded by a lengthy pipeline. Please let me know if I am to provide it as well.
Thanks in advance for looking into the issue.
Issue Analytics
- State:
- Created a year ago
- Comments:5
Top Results From Across the Web
How to make a class JSON serializable - Stack Overflow
Here is a simple solution for a simple feature: .toJSON() Method. Instead of a JSON serializable class, implement a serializer method: import json...
Read more >Object of type is not JSON serializable in Python - LinuxPip
“Object of type is not JSON serializable” is a common error message in Python. It basically says that the built-in json module cannot...
Read more >Make a Python Class JSON Serializable - PYnative
Make a Python class JSON Serializable so that we can convert any custom Python object ... a TypeError: Object of type SampleClass is...
Read more >Object of type Device is not JSON serializable #45 - GitHub
Using Connexoon IO Trying to dump the devices: Traceback (most recent call last): File "test.py", line 20, in dumps = json.dumps(devices, ...
Read more >Pandas : Object of type Response is not JSON serializable
Pandas : Object of type Response is not JSON serializable [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] Pandas ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Great!
Thanks for looking into it @Optimox . However, I have solved the issue. Noticing that the error mentioned that the “device” object is not serializable, I found that I was using “cuda:0” as the
device name
parameter like following:Interestingly, it was not causing any other trouble other than saving the model. I have removed this key from the parameter dictionary and have been able to save/load the model. Thanks again! Please, close the issue if you wish. Cheers!