def force_gatherable(data, device):
    'Change object to gatherable in torch.nn.DataParallel recursively\n\n    The difference from to_device() is changing to torch.Tensor if float or int\n    value is found.\n\n    The restriction to the returned value in DataParallel:\n        The object must be\n        - torch.cuda.Tensor\n        - 1 or more dimension. 0-dimension-tensor sends warning.\n        or a list, tuple, dict.\n\n    '
    if isinstance(data, dict):
        return {k: force_gatherable(v, device) for (k, v) in data.items()}
    elif (isinstance(data, tuple) and (type(data) is not tuple)):
        return type(data)(*[force_gatherable(o, device) for o in data])
    elif isinstance(data, (list, tuple, set)):
        return type(data)((force_gatherable(v, device) for v in data))
    elif isinstance(data, np.ndarray):
        return force_gatherable(torch.from_numpy(data), device)
    elif isinstance(data, torch.Tensor):
        if (data.dim() == 0):
            data = data[None]
        return data.to(device)
    elif isinstance(data, float):
        return torch.tensor([data], dtype=torch.float, device=device)
    elif isinstance(data, int):
        return torch.tensor([data], dtype=torch.long, device=device)
    elif (data is None):
        return None
    else:
        warnings.warn(f'{type(data)} may not be gatherable by DataParallel')
        return data
Change object to gatherable in torch.nn.DataParallel recursively

The difference from to_device() is changing to torch.Tensor if float or int
value is found.

The restriction to the returned value in DataParallel:
    The object must be
    - torch.cuda.Tensor
    - 1 or more dimension. 0-dimension-tensor sends warning.
    or a list, tuple, dict.
def force_gatherable(data, device):
    'Change object to gatherable in torch.nn.DataParallel recursively\n\n    The difference from to_device() is changing to torch.Tensor if float or int\n    value is found.\n\n    The restriction to the returned value in DataParallel:\n        The object must be\n        - torch.cuda.Tensor\n        - 1 or more dimension. 0-dimension-tensor sends warning.\n        or a list, tuple, dict.\n\n    '
    if isinstance(data, dict):
        return {k: force_gatherable(v, device) for (k, v) in data.items()}
    elif (isinstance(data, tuple) and (type(data) is not tuple)):
        return type(data)(*[force_gatherable(o, device) for o in data])
    elif isinstance(data, (list, tuple, set)):
        return type(data)((force_gatherable(v, device) for v in data))
    elif isinstance(data, np.ndarray):
        return force_gatherable(torch.from_numpy(data), device)
    elif isinstance(data, torch.Tensor):
        if (data.dim() == 0):
            data = data[None]
        return data.to(device)
    elif isinstance(data, float):
        return torch.tensor([data], dtype=torch.float, device=device)
    elif isinstance(data, int):
        return torch.tensor([data], dtype=torch.long, device=device)
    elif (data is None):
        return None
    else:
        warnings.warn(f'{type(data)} may not be gatherable by DataParallel')
        return data