pytorch GAN生成对抗网络实例

作者:全栈的方向 时间:2022-06-30 03:41:27 

我就废话不多说了,直接上代码吧!


import torch
import torch.nn as nn
from torch.autograd import Variable
import numpy as np
import matplotlib.pyplot as plt

torch.manual_seed(1)
np.random.seed(1)

BATCH_SIZE = 64
LR_G = 0.0001
LR_D = 0.0001
N_IDEAS = 5
ART_COMPONENTS = 15
PAINT_POINTS = np.vstack([np.linspace(-1,1,ART_COMPONENTS) for _ in range(BATCH_SIZE)])

def artist_works():
a = np.random.uniform(1,2,size=BATCH_SIZE)[:,np.newaxis]
paintings = a*np.power(PAINT_POINTS,2) + (a-1)
paintings = torch.from_numpy(paintings).float()
return Variable(paintings)

G = nn.Sequential(
nn.Linear(N_IDEAS,128),
nn.ReLU(),
nn.Linear(128,ART_COMPONENTS),
)

D = nn.Sequential(
nn.Linear(ART_COMPONENTS,128),
nn.ReLU(),
nn.Linear(128,1),
nn.Sigmoid(),
)

opt_D = torch.optim.Adam(D.parameters(),lr=LR_D)
opt_G = torch.optim.Adam(G.parameters(),lr=LR_G)

plt.ion()

for step in range(10000):
artist_paintings = artist_works()
G_ideas = Variable(torch.randn(BATCH_SIZE,N_IDEAS))
G_paintings = G(G_ideas)

prob_artist0 = D(artist_paintings)
prob_artist1 = D(G_paintings)

D_loss = - torch.mean(torch.log(prob_artist0) + torch.log(1-prob_artist1))
G_loss = torch.mean(torch.log(1 - prob_artist1))

opt_D.zero_grad()
D_loss.backward(retain_variables=True)
opt_D.step()

opt_G.zero_grad()
G_loss.backward()
opt_G.step()

if step % 50 == 0:
plt.cla()
plt.plot(PAINT_POINTS[0],G_paintings.data.numpy()[0],c='#4ad631',lw=3,label='Generated painting',)
plt.plot(PAINT_POINTS[0],2 * np.power(PAINT_POINTS[0], 2) + 1,c='#74BCFF',lw=3,label='upper bound',)
plt.plot(PAINT_POINTS[0],1 * np.power(PAINT_POINTS[0], 2) + 0,c='#FF9359',lw=3,label='lower bound',)
plt.text(-.5,2.3,'D accuracy=%.2f (0.5 for D to converge)' % prob_artist0.data.numpy().mean(), fontdict={'size':15})
plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -D_loss.data.numpy(), fontdict={'size': 15})
plt.ylim((0,3))
plt.legend(loc='upper right', fontsize=12)
plt.draw()
plt.pause(0.01)

plt.ioff()
plt.show()

pytorch GAN生成对抗网络实例

来源:https://blog.csdn.net/pureszgd/article/details/75095332

标签:pytorch,GAN,生成对抗网络
0
投稿

猜你喜欢

  • Python读取Excel数据实现批量生成合同

    2022-08-15 02:12:12
  • python中的字典操作及字典函数

    2023-02-06 05:17:42
  • Python3.5常见内置方法参数用法实例详解

    2023-02-24 21:38:38
  • 显卡驱动CUDA 和 pytorch CUDA 之间的区别

    2021-12-29 04:35:31
  • Python爬虫之m3u8文件里提取小视频的正确姿势

    2021-07-05 18:02:41
  • JavaScript中的私有成员 Javascript教程

    2008-12-02 17:57:00
  • Python Selenium Cookie 绕过验证码实现登录示例代码

    2021-11-12 08:25:24
  • Tensorflow 2.1完成对MPG回归预测详解

    2023-06-30 07:31:36
  • anaconda3安装及jupyter环境配置全教程

    2021-03-26 07:45:07
  • python flask框架快速入门

    2021-10-16 22:02:26
  • 纯numpy卷积神经网络实现手写数字识别的实践

    2023-11-08 10:44:50
  • 提升设计品质的8个布局方案[译]

    2010-03-18 16:06:00
  • Python输出各行命令详解

    2021-12-03 05:29:18
  • asp 通用修改和增加函数代码

    2011-03-16 11:15:00
  • Linux 发邮件磁盘空间监控(python)

    2022-03-15 09:17:24
  • 在python3.5中使用OpenCV的实例讲解

    2021-03-14 21:30:36
  • 13个你希望早点知道的实用WordPress SQL查询语句[译]

    2010-02-28 12:48:00
  • ASP内置对象Request和Response用法详解

    2007-09-14 10:35:00
  • python GUI库图形界面开发之PyQt5滚动条控件QScrollBar详细使用方法与实例

    2021-04-26 09:11:26
  • TensorFlow实现简单的CNN的方法

    2023-09-14 14:08:50
  • asp之家 网络编程 m.aspxhome.com