TypeError: normal() received an invalid combination of arguments

def __init__(self, std_range=[0, 0.10], noiseless_item_key='clean'):
    self.std_range = std_range
    self.key = noiseless_item_key
def __call__(self, data):
    noise_std = random.uniform(*self.std_range)
    data[self.key] = data['pos']
    data['pos'] = data['pos'] + torch.normal(mean=0, std=noise_std, size=data['pos'].size())
    return data

报错:

TypeError: normal() received an invalid combination of arguments - got (size=torch.Size, std=float, mean=int, ), but expected one of:
 * (Tensor mean, Tensor std, torch.Generator generator, Tensor out)
 * (Tensor mean, float std, torch.Generator generator, Tensor out)
 * (float mean, Tensor std, torch.Generator generator, Tensor out)

torch1.1.0版本函数定义不同,于是修改代码:

def __call__(self, data):
    noise_std = random.uniform(*self.std_range)
    data[self.key] = data['pos']
    data['pos'] = data['pos'] + torch.tensor(np.random.normal(0,noise_std,size=data['pos'].size()),dtype=data['pos'].dtype)
    return data

报错:

RuntimeError: Expected tensor to have CPU Backend, but got tensor with CUDA Backend (while checking arguments for bmm)

修改代码:

def __call__(self, data):
    noise_std = random.uniform(*self.std_range)
    data[self.key] = data['pos']
    noise = torch.tensor(np.random.normal(0, noise_std, size=data['pos'].size()), dtype=data['pos'].dtype)
    noise = noise.to(data['pos'].device)
    data['pos'] = data['pos'] + noise
    return data

参考

你可能感兴趣的:(python)