简体   繁体   English

如何计算相对于输入的损失梯度?

[英]How to calculate Gradient of the loss with respect to input?

I have a pre-trained PyTorch model.我有一个预先训练好的 PyTorch 模型。 I need to calculate the gradient of the loss with respect to the network's inputs using this model (without training again and only using the pre-trained model).我需要使用此模型计算相对于网络输入的损失梯度(无需再次训练,仅使用预训练模型)。

I wrote the following code, but I am not sure it is correct or not.我写了以下代码,但我不确定它是否正确。

    test_X, test_y = load_data(mode='test')
    testset_original = MyDataset(test_X, test_y, transform=default_transform)
    testloader = DataLoader(testset_original, batch_size=32, shuffle=True)

    model = MyModel(device=device).to(device)
    checkpoint = torch.load('checkpoint.pt')
    model.load_state_dict(checkpoint['model_state_dict'])

    gradient_losses = []
    for i, data in enumerate(testloader):
        inputs, labels = data
        inputs= inputs.to(device)
        labels = labels.to(device)
        inputs.requires_grad = True
        output = model(inputs)
        loss = loss_function(output)
        loss.backward()
        gradient_losses.append(inputs.grad)

My question is, does this list gradient_losses actually storing what I wish to store?我的问题是,这个列表 gradient_losses 是否真的存储了我想要存储的内容? If not, what is the correct way to do that?如果不是,那么正确的方法是什么?

does this list gradient_losses actually storing what I wish to store?这个列表gradient_losses实际上存储了我想要存储的内容吗?

Yes, if you are looking to get the derivative of the loss with respect to the input then that seems to be the correct way to do it.是的,如果您希望获得损失相对于输入的导数,那么这似乎是正确的方法。 Here is minimal example, take f(x) = a*x .这是最小的例子,取f(x) = a*x Then df/dx = a .然后df/dx = a

>>> x = torch.rand(10, requires_grad=True)
>>> y = torch.rand(10)
>>> a = torch.tensor([3.], requires_grad=True)

>>> loss = a*x - y
>>> loss.mean().backward()

>>> x.grad
tensor([0.3000, 0.3000, ..., 0.3000, 0.3000])

Which, in this case is equal to a / len(x)其中,在这种情况下等于a / len(x)

Do note, each gradient you extract with input.grad will be averaged over the whole batch, and won't be a gradient over each individual input.请注意,您使用input.grad提取的每个梯度都将在整个批次中input.grad平均值,而不是每个单独输入的梯度。


Also, you don't need to .clone() your input gradients as they are not part of the model and won't get zeroed by model.zero_grad() .此外,您不需要.clone()输入梯度,因为它们不是模型的一部分,并且不会被model.zero_grad()归零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Keras获得相对于输入的损失函数梯度的错误 - Bugs of getting gradient of loss function with respect to input using Keras 计算损耗相对于层输入的偏导数| 连锁规则| 蟒蛇 - Calculate the partial derivative of the loss with respect to the input of the layer | Chain Rule | Python CS231n:如何计算 Softmax 损失函数的梯度? - CS231n: How to calculate gradient for Softmax loss function? 如何累积小批量的损失然后计算梯度 - How to accumulate my loss over mini batches then calculate my gradient 如何计算 output 相对于 pytorch 中每个输入的梯度 - How to compute the gradient of the output with respect to each input in pytorch 计算向量范数相对于python中向量的梯度 - Calculate gradient of norm of a vector with respect to a vector in python 如何获得相对于输入的梯度和更改输入(而不是可训练的无功)以最小化TF 2中的损耗? - How to get gradients with respect to input and change input (rather than trainable vars) to minimize loss in TF 2? 如何计算 keras model 中不同输入的不同损失 - How to calculate different loss for different input in keras model 如何使用 KerasClassifier 计算损失 - How to calculate loss with KerasClassifier 如何在深梦中定义损失函数,例如模型输入上的渐变体面 - How to define loss function on Deep dream like Gradient decent on Model input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM