#!/usr/bin/python2.7
#coding:utf-8
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import savefig
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
# Hyper Parameters
BATCH_SIZE = 64
LR_G = 0.0001 # learning rate for generator
LR_D = 0.0001 # learning rate for discriminator
N_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 canvas 5个灵感生成的15个线段
PAINT_POINTS = np.vstack([np.linspace(-1, 1, ART_COMPONENTS) for _ in range(BATCH_SIZE)]) #纵轴连接(64,15)
# show our beautiful painting range
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.legend(loc='upper right')
# savefig('./GAN_range.jpg')
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 paintings
with 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 fromthese random ideas
with 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 artist
D_loss = -tf.reduce_mean(tf.log(prob_artist0) + tf.log(1-prob_artist1)) #minimize -
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 plotting
for 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='Generatedpainting',)
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()
# savefig('./GAN.jpg')
plt.show()

GANs (Generative Adversarial Networks)的更多相关文章

  1. (转)Introductory guide to Generative Adversarial Networks (GANs) and their promise!

    Introductory guide to Generative Adversarial Networks (GANs) and their promise! Introduction Neural ...

  2. StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks 论文笔记

    StackGAN: Text to Photo-realistic Image Synthesis with Stacked Generative Adversarial Networks  本文将利 ...

  3. 论文笔记之:Semi-Supervised Learning with Generative Adversarial Networks

    Semi-Supervised Learning with Generative Adversarial Networks 引言:本文将产生式对抗网络(GAN)拓展到半监督学习,通过强制判别器来输出类 ...

  4. 论文笔记之:UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS

    UNSUPERVISED REPRESENTATION LEARNING WITH DEEP CONVOLUTIONAL GENERATIVE ADVERSARIAL NETWORKS  ICLR 2 ...

  5. 生成对抗网络(Generative Adversarial Networks,GAN)初探

    1. 从纳什均衡(Nash equilibrium)说起 我们先来看看纳什均衡的经济学定义: 所谓纳什均衡,指的是参与人的这样一种策略组合,在该策略组合上,任何参与人单独改变策略都不会得到好处.换句话 ...

  6. Generative Adversarial Networks overview(1)

    Libo1575899134@outlook.com Libo (原创文章,转发请注明作者) 本文章会先从Gan的简单应用示例讲起,从三个方面问题以及解决思路覆盖25篇GAN论文,第二个大部分会进一步 ...

  7. StarGAN: Unified Generative Adversarial Networks for Multi-Domain Image-to-Image Translation - 1 - 多个域间的图像翻译论文学习

    Abstract 最近在两个领域上的图像翻译研究取得了显著的成果.但是在处理多于两个领域的问题上,现存的方法在尺度和鲁棒性上还是有所欠缺,因为需要为每个图像域对单独训练不同的模型.为了解决该问题,我们 ...

  8. SAGAN:Self-Attention Generative Adversarial Networks - 1 - 论文学习

    Abstract 在这篇论文中,我们提出了自注意生成对抗网络(SAGAN),它是用于图像生成任务的允许注意力驱动的.长距离依赖的建模.传统的卷积GANs只根据低分辨率图上的空间局部点生成高分辨率细节. ...

  9. 语音合成论文翻译:2019_MelGAN: Generative Adversarial Networks for Conditional Waveform Synthesis

    论文地址:MelGAN:条件波形合成的生成对抗网络 代码地址:https://github.com/descriptinc/melgan-neurips 音频实例:https://melgan-neu ...

随机推荐

  1. Fluentd初探 简介与安装

    Fluentd是一个开源的数据收集器,专为处理数据流设计,有点像 syslogd ,但是使用JSON作为数据格式.它采用了插件式的架构,具有高可扩展性高可用性,同时还实现了高可靠的信息转发. 据分(Y ...

  2. vue数据绑定数组,改变元素时不更新view问题

    关于这个问题,官网上说的很清楚官方文档  写个例子HTML<body> <div class="box"> <div v-for="aa i ...

  3. Jenkins以root用户运行的方法

    以centOS系统为例,记录下修改Jenkins以root用户运行的方法. 修改Jenkins配置文件 # 打开配置文件 vim /etc/sysconfig/jenkins # 修改$JENKINS ...

  4. 【ES6】import, require,export

    node编程中最重要的思想就是模块化,import和require都是被模块化所使用. 遵循规范 require 是 AMD规范引入方式 import是es6的一个语法标准,如果要兼容浏览器的话必须转 ...

  5. 1、原生jdbc连接oracle数据库简单介绍

    一.jbdc的常用API1.Connection:数据库的链接对象2.statement:数据库sql执行对象3.preparedStatment:sql的预编译处理对象,是statement子接口4 ...

  6. scipy 安装错误及解决

    pip 安装 scipy 时,因为是编译安装,所以如果缺少一些编译库,会报很多错误,以下总结可能缺失的安装包: sudo apt-get install gfortran sudo apt-get i ...

  7. fuser - 查看文件/网络端口等被什么进程占用

    前提 linux环境下,当使用umount命令卸载挂载点时,会遇到“device is busy”提示,这时fuser就能查出谁在使用这个资源;当然umount –lf  [挂载点] 也可以强制卸载 ...

  8. 支持“xxxContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库

    将项目的数据库连接用户及密码修改后(切换用户,用户名与原来不一样,用户下对象结构一致),报以下错误: 支持“XXXDBContext”上下文的模型已在数据库创建后发生更改.请考虑使用 Code Fir ...

  9. 开发手记:JedisConnectionException: Could not get a resource from the pool

    对于Redis,生产环境是集群模式,测试环境是单例模式,如果在生产环境中用单例模式会报错. 解决办法,通过云配置,将配置进行自动化配置. 另附一份Redis配置: #***************** ...

  10. 蛙蛙推荐: TensorFlow Hello World 之平面拟合

    tensorflow 已经发布了 2.0 alpha 版本,所以是时候学一波 tf 了.官方教程有个平面拟合的类似Hello World的例子,但没什么解释,新手理解起来比较困难. 所以本文对这个案例 ...