2

我正在尝试为参数估计编写一个简单的脚本(这里的参数是权重)。当 .grad() 返回 None 时,我遇到了问题。我也经历了这个这个链接,并在理论上和实践上理解了这个概念。对我来说,以下脚本应该可以工作,但不幸的是,它不起作用。

我的第一次尝试:以下脚本是我的第一次尝试

alpha_xy = torch.tensor(3.7, device=device, dtype=torch.float, requires_grad=True)
beta_y = torch.tensor(1.5, device=device, dtype=torch.float, requires_grad=True)
alpha0 = torch.tensor(1.1, device=device, dtype=torch.float, requires_grad=True)
alpha_y = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha1 = torch.tensor(0.1, device=device, dtype=torch.float, requires_grad=True)
alpha2 = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha3 = torch.tensor(0.001, device=device, dtype=torch.float, requires_grad=True)

learning_rate = 1e-4
total_loss = []

for epoch in tqdm(range(500)):
    loss_1 = 0
    for j in range(x_train.size(0)):
        input = x_train[j:j+1]
        target = y_train[j:j+1]
        input = input.to(device,non_blocking=True)
        target = target.to(device,non_blocking=True)
        x_dt = gamma*input[0][0] + \
               alpha_xy*input[0][0]*input[0][2] + \
               alpha1*input[0][0]


        y0_dt = beta_y*input[0][0] + \
                alpha2*input[0][1]

        y_dt = alpha0*input[0][1] + \
               alpha_y*input[0][2] + \
               alpha3*input[0][0]*input[0][2]

        pred = torch.tensor([[x_dt],
                             [y0_dt],
                             [y_dt]],device=device

                                   )
        loss = (pred - target).pow(2).sum()
        loss_1 += loss
        loss.backward()
        print(pred.grad, x_dt.grad, gamma.grad)

上面的代码抛出错误信息

element 0 of tensors does not require grad and does not have a grad_fn

在线loss.backward()

我的尝试 2:第一次尝试的改进如下:

gamma = torch.tensor(2.0, device=device, dtype=torch.float, requires_grad=True)
alpha_xy = torch.tensor(3.7, device=device, dtype=torch.float, requires_grad=True)
beta_y = torch.tensor(1.5, device=device, dtype=torch.float, requires_grad=True)
alpha0 = torch.tensor(1.1, device=device, dtype=torch.float, requires_grad=True)
alpha_y = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha1 = torch.tensor(0.1, device=device, dtype=torch.float, requires_grad=True)
alpha2 = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha3 = torch.tensor(0.001, device=device, dtype=torch.float, requires_grad=True)

learning_rate = 1e-4
total_loss = []

for epoch in tqdm(range(500)):
    loss_1 = 0
    for j in range(x_train.size(0)):
        input = x_train[j:j+1]
        target = y_train[j:j+1]
        input = input.to(device,non_blocking=True)
        target = target.to(device,non_blocking=True)
        x_dt = gamma*input[0][0] + \
               alpha_xy*input[0][0]*input[0][2] + \
               alpha1*input[0][0]


        y0_dt = beta_y*input[0][0] + \
                alpha2*input[0][1]

        y_dt = alpha0*input[0][1] + \
               alpha_y*input[0][2] + \
               alpha3*input[0][0]*input[0][2]

        pred = torch.tensor([[x_dt],
                             [y0_dt],
                             [y_dt]],device=device, 
                                   dtype=torch.float,
                                   requires_grad=True)
        loss = (pred - target).pow(2).sum()
        loss_1 += loss
        loss.backward()
        print(pred.grad, x_dt.grad, gamma.grad)
#        with torch.no_grad():
#            gamma -= leraning_rate * gamma.grad

现在脚本正在运行,但除了 pred.gred 其他两个返回 None。

我想在计算 loss.backward() 后更新所有参数并更新它们,但由于 None 没有发生。谁能建议我如何改进这个脚本?谢谢。

4

1 回答 1

3

您通过为 声明一个新张量来破坏计算图pred。相反,您可以使用torch.stack. 此外,x_dtandpred是非叶张量,因此默认情况下不保留梯度。您可以使用 覆盖此行为.retain_grad()

gamma = torch.tensor(2.0, device=device, dtype=torch.float, requires_grad=True)
alpha_xy = torch.tensor(3.7, device=device, dtype=torch.float, requires_grad=True)
beta_y = torch.tensor(1.5, device=device, dtype=torch.float, requires_grad=True)
alpha0 = torch.tensor(1.1, device=device, dtype=torch.float, requires_grad=True)
alpha_y = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha1 = torch.tensor(0.1, device=device, dtype=torch.float, requires_grad=True)
alpha2 = torch.tensor(0.9, device=device, dtype=torch.float, requires_grad=True)
alpha3 = torch.tensor(0.001, device=device, dtype=torch.float, requires_grad=True)

learning_rate = 1e-4
total_loss = []

for epoch in tqdm(range(500)):
    loss_1 = 0
    for j in range(x_train.size(0)):
        input = x_train[j:j+1]
        target = y_train[j:j+1]
        input = input.to(device,non_blocking=True)
        target = target.to(device,non_blocking=True)
        x_dt = gamma*input[0][0] + \
               alpha_xy*input[0][0]*input[0][2] + \
               alpha1*input[0][0]

        # retain the gradient for non-leaf tensors
        x_dt.retain_grad()

        y0_dt = beta_y*input[0][0] + \
                alpha2*input[0][1]

        y_dt = alpha0*input[0][1] + \
               alpha_y*input[0][2] + \
               alpha3*input[0][0]*input[0][2]

        # use stack instead of declaring a new tensor
        pred = torch.stack([x_dt, y0_dt, y_dt], dim=0).unsqueeze(1)

        # pred is also a non-leaf tensor so we need to tell pytorch to retain its grad
        pred.retain_grad()

        loss = (pred - target).pow(2).sum()
        loss_1 += loss
        loss.backward()
        print(pred.grad, x_dt.grad, gamma.grad)
        with torch.no_grad():
            gamma -= learning_rate * gamma.grad

封闭式解决方案

假设您要优化在函数顶部定义的参数gamma, alpha_xy,beta_y等...那么您在这里拥有的是普通最小二乘法的示例。有关该主题的稍微友好的介绍,请参见最小二乘法。看一下 的组件,pred您会注意到x_dty0_dty_dt实际上在参数方面彼此独立(在这种情况下很明显,因为它们每个都使用完全不同的参数)。这使问题变得更加容易(x_dt - target[0])**2,因为这意味着我们实际上可以单独优化术语!(y0_dt - target[1])**2(y_dt - target[2])**2

在不深入细节的情况下,解决方案(没有反向传播或梯度下降)最终成为

# supposing x_train is [N,3] and y_train is [N,3]
x1 = torch.stack((x_train[:, 0], x_train[:, 0] * x_train[:, 2]), dim=0)
y1 = y_train[:, 0].unsqueeze(1)

# avoid inverses using solve to get p1 = inv(x1 . x1^T) . x1 . y1
p1, _ = torch.solve(x1 @ y1, x1 @ x1.transpose(1, 0))

# gamma and alpha1 are redundant. As long as gamma + alpha1 = p1[0] we get the same optimal value for loss
gamma = p1[0] / 2
alpha_xy = p1[1]
alpha1 = p1[0] / 2

x2 = torch.stack((x_train[:, 0], x_train[:, 1]), dim=0)
y2 = y_train[:, 1].unsqueeze(1)

p2, _ = torch.solve(x2 @ y2, x2 @ x2.transpose(1, 0))

beta_y = p2[0]
alpha2 = p2[1]

x3 = torch.stack((x_train[:, 1], x_train[:, 2], x_train[:, 0] * x_train[:, 2]), dim=0)
y3 = y_train[:, 2].unsqueeze(1)

p3, _ = torch.solve(x3 @ y3, x3 @ x3.transpose(1, 0))

alpha0 = p3[0]
alpha_y = p3[1]
alpha3 = p3[2]

loss_1 = torch.sum((x1.transpose(1, 0) @ p1 - y1)**2 + (x2.transpose(1, 0) @ p2 - y2)**2 + (x3.transpose(1, 0) @ p3 - y3)**2)
mse = loss_1 / x_train.size(0)

为了测试此代码是否正常工作,我生成了一些我知道基础模型系数的假数据(添加了一些噪声,因此最终结果与预期不完全匹配)。

def gen_fake_data(samples=50000):
    x_train = torch.randn(samples, 3)
    # define fake data with known minimal solutions
    x1 = torch.stack((x_train[:, 0], x_train[:, 0] * x_train[:, 2]), dim=0)
    x2 = torch.stack((x_train[:, 0], x_train[:, 1]), dim=0)
    x3 = torch.stack((x_train[:, 1], x_train[:, 2], x_train[:, 0] * x_train[:, 2]), dim=0)
    y1 = x1.transpose(1, 0) @ torch.tensor([[1.0], [2.0]])  # gamma + alpha1 = 1.0
    y2 = x2.transpose(1, 0) @ torch.tensor([[3.0], [4.0]])
    y3 = x3.transpose(1, 0) @ torch.tensor([[5.0], [6.0], [7.0]])
    y_train = torch.cat((y1, y2, y3), dim=1) + 0.1 * torch.randn(samples, 3)
    return x_train, y_train

x_train, y_train = gen_fake_data()

# optimization code from above
...

print('loss_1:', loss_1.item())
print('MSE:', mse.item())

print('Expected 0.5, 2.0, 0.5, 3.0, 4.0, 5.0, 6.0, 7.0')
print('Actual', gamma.item(), alpha_xy.item(), alpha1.item(), beta_y.item(), alpha2.item(), alpha0.item(), alpha_y.item(), alpha3.item())

这导致

loss_1: 1491.731201171875
MSE: 0.029834624379873276
Expected 0.5, 2.0, 0.5, 3.0, 4.0, 5.0, 6.0, 7.0
Actual 0.50002 2.0011 0.50002 3.0009 3.9997 5.0000 6.0002 6.9994
于 2019-11-25T13:10:20.390 回答