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

TypeError: Object of type Tensor is not JSON serializable when running train_transformer_style with takes_target as 1 #197

Closed
97harsh opened this issue Nov 20, 2020 · 5 comments

Comments

@97harsh
Copy link
Collaborator

97harsh commented Nov 20, 2020

Traceback (most recent call last):
File "flood_forecast/trainer.py", line 108, in
main()
File "flood_forecast/trainer.py", line 103, in main
train_function(training_config["model_type"], training_config)
File "flood_forecast/trainer.py", line 42, in train_function
train_transformer_style(model=trained_model,
File "/home/harsh/Documents/Coronawhy/flow-forecast/flood_forecast/pytorch_training.py", line 146, in train_transformer_style
model.save_model(model_filepath, max_epochs)
File "/home/harsh/Documents/Coronawhy/flow-forecast/flood_forecast/time_model.py", line 152, in save_model
json.dump(self.params, p)
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/init.py", line 179, in dump
for chunk in iterable:
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/home/harsh/anaconda3/envs/flow-forecast/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type Tensor is not JSON serializable

@97harsh
Copy link
Collaborator Author

97harsh commented Nov 20, 2020

if takes_target:
forward_params["t"] = trg

this is updating the forward_params with Tensor objects in models.params when takes_target==1
This should be sent as copy of dictionary instead of dictionary itself
total_loss = torch_single_train(
model,
opt,
criterion,
data_loader,
takes_target,
meta_model,
meta_representation,
forward_params)

@97harsh
Copy link
Collaborator Author

97harsh commented Nov 20, 2020

@isaacmg should I create a PR to should I change this ?

@97harsh 97harsh changed the title TypeError: Object of type Tensor is not JSON serializable when running train_transformer_style with takes_param as 1 TypeError: Object of type Tensor is not JSON serializable when running train_transformer_style with takes_target as 1 Nov 20, 2020
97harsh added a commit that referenced this issue Nov 20, 2020
#197 
Sending copy of dictionary of forward_params, instead of dictionary itself
won't send "target" key with Tensor in model.params and cause issue during saving model
@isaacmg
Copy link
Collaborator

isaacmg commented Nov 20, 2020

This didn't seem to occur before with the full transformer though

@isaacmg
Copy link
Collaborator

isaacmg commented Nov 20, 2020

If that is the sole issue one would think this test should fail too

echo -e 'running trainer_full_transformer_test \n'

@97harsh
Copy link
Collaborator Author

97harsh commented Nov 20, 2020

The value t only gets tensor object when takes_target argument is passed as True to torch_single_train

@97harsh 97harsh closed this as completed Nov 20, 2020
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

2 participants