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

Avoid AttributeError: 'torch.dtype' object has no attribute 'type' #89

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/episode_buffer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import numbers

import torch as th
import numpy as np
from types import SimpleNamespace as SN
Expand Down Expand Up @@ -60,7 +62,7 @@ def _setup_data(self, scheme, groups, batch_size, max_seq_length, preprocess):
group = field_info.get("group", None)
dtype = field_info.get("dtype", th.float32)

if isinstance(vshape, int):
if isinstance(vshape, numbers.Integral):
vshape = (vshape,)

if group:
Expand Down Expand Up @@ -100,7 +102,7 @@ def update(self, data, bs=slice(None), ts=slice(None), mark_filled=True):
raise KeyError("{} not found in transition or episode data".format(k))

dtype = self.scheme[k].get("dtype", th.float32)
v = th.tensor(v, dtype=dtype, device=self.device)
v = th.as_tensor(v, dtype=dtype, device=self.device)
self._check_safe_view(v, target[k][_slices])
target[k][_slices] = v.view_as(target[k][_slices])

Expand Down
2 changes: 1 addition & 1 deletion src/learners/coma_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def train(self, batch: EpisodeBatch, t_env: int, episode_num: int):

self.logger.log_stat("advantage_mean", (advantages * mask).sum().item() / mask.sum().item(), t_env)
self.logger.log_stat("coma_loss", coma_loss.item(), t_env)
self.logger.log_stat("agent_grad_norm", grad_norm, t_env)
self.logger.log_stat("agent_grad_norm", grad_norm.item(), t_env)
self.logger.log_stat("pi_max", (pi.max(dim=1)[0] * mask).sum().item() / mask.sum().item(), t_env)
self.log_stats_t = t_env

Expand Down
2 changes: 1 addition & 1 deletion src/learners/q_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def train(self, batch: EpisodeBatch, t_env: int, episode_num: int):

if t_env - self.log_stats_t >= self.args.learner_log_interval:
self.logger.log_stat("loss", loss.item(), t_env)
self.logger.log_stat("grad_norm", grad_norm, t_env)
self.logger.log_stat("grad_norm", grad_norm.item(), t_env)
mask_elems = mask.sum().item()
self.logger.log_stat("td_error_abs", (masked_td_error.abs().sum().item()/mask_elems), t_env)
self.logger.log_stat("q_taken_mean", (chosen_action_qvals * mask).sum().item()/(mask_elems * self.args.n_agents), t_env)
Expand Down
2 changes: 1 addition & 1 deletion src/learners/qtran_learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def train(self, batch: EpisodeBatch, t_env: int, episode_num: int):
self.logger.log_stat("td_loss", td_loss.item(), t_env)
self.logger.log_stat("opt_loss", opt_loss.item(), t_env)
self.logger.log_stat("nopt_loss", nopt_loss.item(), t_env)
self.logger.log_stat("grad_norm", grad_norm, t_env)
self.logger.log_stat("grad_norm", grad_norm.item(), t_env)
if self.args.mixer == "qtran_base":
mask_elems = mask.sum().item()
self.logger.log_stat("td_error_abs", (masked_td_error.abs().sum().item()/mask_elems), t_env)
Expand Down