question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object of type device is not JSON serializable

See original GitHub issue

Hi,

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:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
Optimoxcommented, Mar 30, 2022

Great!

0reactions
tanweer-mahdicommented, Mar 30, 2022

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:

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

pretrainer_params["optimizer_fn"] = torch.optim.Adam
pretrainer_params["optimizer_params"] = dict(lr = 2e-2)
pretrainer_params["mask_type"] = "entmax"
pretrainer_params["n_shared"] = 1
pretrainer_params["n_independent"] = 1
pretrainer_params["lambda_sparse"] = 0
pretrainer_params["n_d"] = 128
pretrainer_params["n_a"] = 64
pretrainer_params["n_steps"] = 3
pretrainer_params["gamma"] = 1.5
#pretrainer_params["device_name"] = device
pretrainer_params["scheduler_fn"] = torch.optim.lr_scheduler.StepLR
pretrainer_params["scheduler_params"] = {"step_size":10, # how to use learning rate scheduler
                                         "gamma":0.9}

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found