Tensorflow实例:实现GAN(生成对抗网)
来源:互联网 发布:练六级英语听力的软件 编辑:程序博客网 时间:2024/05/29 07:05
"""Dependencies:tensorflow: 1.1.0matplotlibnumpy"""import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plttf.set_random_seed(1)np.random.seed(1)# Hyper ParametersBATCH_SIZE = 64LR_G = 0.0001 # learning rate for generatorLR_D = 0.0001 # learning rate for discriminatorN_IDEAS = 5 # think of this as number of ideas for generating an art work (Generator)ART_COMPONENTS = 15 # it could be total point G can draw in the canvasPAINT_POINTS = np.vstack([np.linspace(-1, 1, ART_COMPONENTS) for _ in range(BATCH_SIZE)])# show our beautiful painting rangeplt.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.legend(loc='upper right')plt.show()def artist_works(): # painting from the famous artist (real target) a = np.random.uniform(1, 2, size=BATCH_SIZE)[:, np.newaxis] paintings = a * np.power(PAINT_POINTS, 2) + (a-1) return paintingswith tf.variable_scope('Generator'): G_in = tf.placeholder(tf.float32, [None, N_IDEAS]) # random ideas (could from normal distribution) G_l1 = tf.layers.dense(G_in, 128, tf.nn.relu) G_out = tf.layers.dense(G_l1, ART_COMPONENTS) # making a painting from these random ideaswith tf.variable_scope('Discriminator'): real_art = tf.placeholder(tf.float32, [None, ART_COMPONENTS], name='real_in') # receive art work from the famous artist D_l0 = tf.layers.dense(real_art, 128, tf.nn.relu, name='l') prob_artist0 = tf.layers.dense(D_l0, 1, tf.nn.sigmoid, name='out') # probability that the art work is made by artist # reuse layers for generator D_l1 = tf.layers.dense(G_out, 128, tf.nn.relu, name='l', reuse=True) # receive art work from a newbie like G prob_artist1 = tf.layers.dense(D_l1, 1, tf.nn.sigmoid, name='out', reuse=True) # probability that the art work is made by artistD_loss = -tf.reduce_mean(tf.log(prob_artist0) + tf.log(1-prob_artist1))G_loss = tf.reduce_mean(tf.log(1-prob_artist1))train_D = tf.train.AdamOptimizer(LR_D).minimize( D_loss, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Discriminator'))train_G = tf.train.AdamOptimizer(LR_G).minimize( G_loss, var_list=tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='Generator'))sess = tf.Session()sess.run(tf.global_variables_initializer())plt.ion() # something about continuous plottingfor step in range(5000): artist_paintings = artist_works() # real painting from artist G_ideas = np.random.randn(BATCH_SIZE, N_IDEAS) G_paintings, pa0, Dl = sess.run([G_out, prob_artist0, D_loss, train_D, train_G], # train and get results {G_in: G_ideas, real_art: artist_paintings})[:3] if step % 50 == 0: # plotting plt.cla() plt.plot(PAINT_POINTS[0], G_paintings[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)' % pa0.mean(), fontdict={'size': 15}) plt.text(-.5, 2, 'D score= %.2f (-1.38 for G to converge)' % -Dl, fontdict={'size': 15}) plt.ylim((0, 3)); plt.legend(loc='upper right', fontsize=12); plt.draw(); plt.pause(0.01)plt.ioff()plt.show()
运行结果:
阅读全文
0 0
- Tensorflow实例:实现GAN(生成对抗网)
- 生成对抗网络(GAN)原理+tensorflow代码实现
- GAN生成对抗网络的TensorFlow实现
- 如何用 TensorFlow 实现生成式对抗网络(GAN)
- 不要怂,就是GAN (生成式对抗网络) (六):Wasserstein GAN(WGAN) TensorFlow 代码
- 生成对抗网络(GAN)
- 《白话深度学习与Tensorflow》学习笔记(6)生成式对抗网络(GAN)
- [TensorFlow]生成对抗网络(GAN)介绍与实践
- 生成式对抗网络(GAN)资源
- 贝叶斯生成对抗网络(GAN)
- 生成对抗网络(GAN)初探
- GAN生成对抗神经网络原理(一)
- 7 什么是GAN(生成对抗网络)?
- 生成对抗网络GAN
- GAN生成对抗网络
- 生成对抗网络-GAN
- Gan 生成对抗网络
- [生成对抗网络] GAN
- 数据结构->栈和队列的基本操作
- 《程序员》:唯品会双11大促技术保障实践 为什么耗资数亿的12306一直都非常难用? 96年美女拿下了刘强东的猪脸识别冠军,30万奖金到手! 2017 科技大事件回顾:智能音箱大战、数据泄露于
- ubuntu安装好matlab后键盘复制粘贴快捷键不能使用 解决办法
- FTI FormingSuite 2018 官方版下载附安装教程
- spring配置bean的细节二
- Tensorflow实例:实现GAN(生成对抗网)
- [区间DP] POJ 3186
- AES过程
- Python之旅
- 数据结构->二叉树的基本操作
- 斯坦福大学Tensorflow与深度学习实战课程
- 51nod 1172 Partial Sums V2 任意模数FFT
- 高级用法
- 程序员偷偷深爱的9个不良编程习惯