| 434 | while run[0] <= num_steps: |
| 435 | |
| 436 | def closure(): |
| 437 | # correct the values of updated input image |
| 438 | with torch.no_grad(): |
| 439 | input_img.clamp_(0, 1) |
| 440 | |
| 441 | optimizer.zero_grad() |
| 442 | model(input_img) |
| 443 | style_score = 0 |
| 444 | content_score = 0 |
| 445 | |
| 446 | for sl in style_losses: |
| 447 | style_score += sl.loss |
| 448 | for cl in content_losses: |
| 449 | content_score += cl.loss |
| 450 | |
| 451 | style_score *= style_weight |
| 452 | content_score *= content_weight |
| 453 | |
| 454 | loss = style_score + content_score |
| 455 | loss.backward() |
| 456 | |
| 457 | run[0] += 1 |
| 458 | if run[0] % 50 == 0: |
| 459 | print("run {}:".format(run)) |
| 460 | print('Style Loss : {:4f} Content Loss: {:4f}'.format( |
| 461 | style_score.item(), content_score.item())) |
| 462 | print() |
| 463 | |
| 464 | return style_score + content_score |
| 465 | |
| 466 | optimizer.step(closure) |
| 467 | |