我想在https://github.com/leftthomas/SRGAN中添加train.py中的损失
g_loss = generator_criterion(fake_out, fake_img, real_img)我可以自己写一个函数吗?
def ContentLoss(a, b):
result = 0
for x, y in zip(a, b):
shape = x.shape
k = np.prod(shape[0:])
diff = x - y
#l2 norm
diff = np. sqrt(np. sum(np. square(diff)))
diff = diff*diff
diff = diff / k
result = result + diff
return result并将其添加到原损失中如下:
a = ContentLoss(a,b)
g_loss = generator_criterion(fake_out, fake_img, real_img) + a有没有一种方法来计算这个损失在训练中的梯度?
发布于 2022-11-17 08:36:45
您链接的项目使用PyTorch。假设您也在使用它,您可以使用PyTorch而不是numpy来实现您的损失,您的损失就被覆盖了。
import torch
import math
def ContentLoss(a, b):
result = 0
for x, y in zip(a, b):
shape = x.shape
k = math.prod(shape[0:]) # you can also use np, but not torch
diff = x - y
#l2 norm
# just replacing np with torch
diff = torch.sqrt(torch.sum(torch.square(diff)))
diff = diff*diff # torch differentiates through these as well
diff = diff / k
result = result + diff
return result
# I assume you only want gradients for a (are these your model outputs?)
# This is just test data anyway. If this is your model output,
# it will pass on the gradients to the weights.
a = [
torch.tensor([1.0, 1.0, 1.0], requires_grad=True),
torch.tensor([0.0, -1.0, 1.0], requires_grad=True),
torch.tensor([7.0, 6.0, -5.0], requires_grad=True),
]
b = [
torch.tensor([1.0, 0.0, 1.0]),
torch.tensor([1.0, -2.0, 3.0]),
torch.tensor([0.0, 0.0, 0.0]),
]
loss = ContentLoss(a, b)
loss.backward() # computes the gradients
for x in a:
print(f"a={a}, gradient={a.grad}")如果您需要保持numpy,您必须创建一个torch.autograd.Function并实现向前和向后:https://pytorch.org/tutorials/beginner/examples_autograd/two_layer_net_custom_function.html。
https://stackoverflow.com/questions/74471691
复制相似问题