PyTorch里面的optimizer.step(closure)

optimizer.step(closure)
Performs a single optimization step (parameter update).
Parameters: closure (callable) – A closure that reevaluates the model and returns the loss. Optional for most optimizers.

def closure():
    # correct the values of updated input image
    input_img.data.clamp_(0, 1)

    optimizer.zero_grad()
    model(input_img)
    style_score = 0
    content_score = 0

    for sl in style_losses:
        style_score += sl.loss
    for cl in content_losses:
        content_score += cl.loss

    style_score *= style_weight
    content_score *= content_weight

    loss = style_score + content_score
    loss.backward()

    run[0] += 1
    if run[0] % 50 == 0:
        print("run {}:".format(run))
        print('Style Loss : {:4f} Content Loss: {:4f}'.format(
            style_score.item(), content_score.item()))
        print()

    return style_score + content_score

optimizer.step(closure)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 金鳌中有聂氏者,名海芬,武林人也,操衙捕业,凡三十余载,历案数百,累擢至临安府提辖,秩六品,世人不察,皆以『神探』...
    大侠霍元乙阅读 2,294评论 35 62
  • 人就是佛,众人就是诸佛!所以,你的一切都是人为的,你被人肉,你身边的人被人肉,所有人都被人肉着,只是大家都不知道啊...
    魔世鬼事阅读 262评论 0 0
  • 你知道吗 ...
    寐七阅读 179评论 0 1
  • 语言的能力,美好信念的能力,能使人积极向上,快乐向善。勇往直前,能作用于身体的每一处,将我们的人生变成内在的美好!...
    淡淡的云_f0fd阅读 1,344评论 4 9