如何将总和向量化?张量[i,:,:,:] +张量[i]

时间:2019-06-25 08:42:26

标签: python python-3.x vectorization pytorch

我要向量化以下代码:

def style_noise(self, y, style):
    n = torch.randn(y.shape)
    for i in range(n.shape[0]):
        n[i] = (n[i] - n.mean(dim=(1, 2, 3))[i]) * style.std(dim=(1, 2, 3))[i] / n.std(dim=(1, 2, 3))[i] + style.mean(dim=(1, 2, 3))[i]
    noise = Variable(n, requires_grad=False).to(y.device)
    return noise

我没有找到一种很好的方法。

y和style是4d张量,例如style.shape = y.shape = [64, 3, 128, 128]

我想返回噪声张量noise.shape = [64, 3, 128, 128]

如果问题不清楚,请在评论中让我知道。

2 个答案:

答案 0 :(得分:1)

您的用例正是.mean.std方法带有keepdim参数的原因。您可以利用此功能来使broadcasting semantics为您向量化事物:

def style_noise(self, y, style):
    n = torch.randn(y.shape)
    n_mean = n.mean(dim=(1, 2, 3), keepdim=True)
    n_std = n.std(dim=(1, 2, 3), keepdim=True)
    style_mean = style.mean(dim=(1, 2, 3), keepdim=True)
    style_std = style.std(dim=(1, 2, 3), keepdim=True)
    n = (n - n_mean) * style_std / n_std + style_mean
    noise = Variable(n, requires_grad=False).to(y.device)
    return noise

答案 1 :(得分:1)

要计算整个张量的均值和标准差,请不要设置任何参数

m = t.mean(); print(m) # if you don't set the dim for the whole tensor
s = t.std(); print(s) # if you don't set the dim for the whole tensor

然后,如果您的形状是2,2,2,则创建张量以广播减法和除法。

ss = torch.empty(2,2,2).fill_(s)
print(ss)

mm = torch.empty(2,2,2).fill_(m)
print(mm)

当您未设置keepdim时,dim目前无法正常工作。

m = t.mean(); print(m) # for the whole tensor
s = t.std(); print(s) # for the whole tensor

m = t.mean(dim=0); print(m) # 0 means columns mean
s = t.std(dim=0); print(s) # 0 means columns mean

m = t.mean(dim=1); print(m) # 1 means rows mean
s = t.std(dim=1); print(s) # 1 means rows mean

s = t.mean(keepdim=True);print(s) # will not work
m = t.std(keepdim=True);print(m) # will not work

如果将一个dim设置为元组,则它将返回平均值,而不是全部。