首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在SRGAN中添加损失

在SRGAN中添加损失
EN

Stack Overflow用户
提问于 2022-11-17 07:32:41
回答 1查看 31关注 0票数 0

我想在https://github.com/leftthomas/SRGAN中添加train.py中的损失

代码语言:javascript
复制
g_loss = generator_criterion(fake_out, fake_img, real_img)

我可以自己写一个函数吗?

代码语言:javascript
复制
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

并将其添加到原损失中如下:

代码语言:javascript
复制
a = ContentLoss(a,b)
g_loss = generator_criterion(fake_out, fake_img, real_img) + a

有没有一种方法来计算这个损失在训练中的梯度?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-17 08:36:45

您链接的项目使用PyTorch。假设您也在使用它,您可以使用PyTorch而不是numpy来实现您的损失,您的损失就被覆盖了。

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74471691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档