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

why make dict to self.__class__ #16

Open
ghost opened this issue Oct 30, 2019 · 1 comment
Open

why make dict to self.__class__ #16

ghost opened this issue Oct 30, 2019 · 1 comment

Comments

@ghost
Copy link

ghost commented Oct 30, 2019

    def __setattr__(self, name, value):
        if isinstance(value, (list, tuple)):
            value = [self.__class__(x)
                     if isinstance(x, dict) else x for x in value]
        elif isinstance(value, dict) and not isinstance(value, self.__class__):
            value = self.__class__(value)
        super(EasyDict, self).__setattr__(name, value)
        super(EasyDict, self).__setitem__(name, value)

I want to know why make dict to easydict.
I use the type dict of value , then this value can not use multiple gpu.

@ghost
Copy link
Author

ghost commented Oct 30, 2019

def scatter(inputs, target_gpus, dim=0):

    def scatter_map(obj):
        if isinstance(obj, torch.Tensor):
            return Scatter.apply(target_gpus, None, dim, obj)
        if isinstance(obj, tuple) and len(obj) > 0:
            return list(zip(*map(scatter_map, obj)))
        if isinstance(obj, list) and len(obj) > 0:
            return list(map(list, zip(*map(scatter_map, obj))))
        if isinstance(obj, dict) and len(obj) > 0:
            return list(map(type(obj), zip(*map(scatter_map, obj.items()))))
        return [obj for targets in target_gpus]


    try:
        res = scatter_map(inputs)
    finally:
        scatter_map = None
    return res
from easydict import EasyDict as edict
batch = edict()
a = dict({"st":[12, 29,  5,  8]})
batch["ts_label"] = a
target_gpus = [0,1]
print(type(batch["ts_label"])) # easydict not dict 
output = scatter(batch["ts_label"],target_gpus)
print(output)
  File "/home/ll/anaconda3/envs/py27_torch/lib/python2.7/site-packages/easydict/__init__.py", line 117, in __init__
    for k, v in d.items():
AttributeError: 'tuple' object has no attribute 'items'

the function scatter is from torch.nn.data_parallel.py

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

0 participants