#!/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. 用python设置定时任务

    python中的轻量级定时任务调度库:schedule   提到定时任务调度的时候,相信很多人会想到芹菜celery,要么就写个脚本塞到crontab中.不过,一个小的定时脚本,要用celery的话太 ...

  2. Linux 大爆炸:一个内核,无数发行版

    即使你是一个 Linux 新人,你可能也已经知道它不是一个单一的.整体的操作系统,而是一群项目.这个星座中不同的“星”组成了“发行版”.每个都提供了自己的 Linux 模式. 感谢这一系列发行版所提供 ...

  3. NOsql总结

    NOsql总结 1.存储: 任意格式的数据:值对存储,列存储,文档存储,图形数据库 灵活的表结构支持:类型动态.scheme动态: 灵活的存储形式:列存储: 弱关系:NoSQL数据库种类繁多,但是一个 ...

  4. 报错Unexpected token u

    报错Unexpected token u,出错位置提示在index.html的第一行. 出现这个错误的原因是尝试JOSN.parse一个undefined的对象导致的,解决办法就是如果为undefin ...

  5. CSAPP:第三章程序的机器级表示3

    程序的机器级表示3 关键点:过程.调试.指针 过程1.运行时栈2.转移控制3.数据传递4.栈上的局部存储5.寄存器中的局部存储空间理解指针使用GDB调试器 过程 1.运行时栈   x86-64的栈向低 ...

  6. 【转】TCP和SOCKET关系

    socket是TCP/IP协议的API   TCP是数据的介质,Socket是TCP的介质. 查了一下RFC文档,Socket是RFC147,更新时间是1971年.TCP是RFC793,更新时间是19 ...

  7. linux上安装完torch后仍报错:ImportError: No module named torch

    linux上安装完torch后仍报错: Traceback (most recent call last): File , in <module> import torch ImportE ...

  8. day10--函数之形参与实参

    ''' def fn(形参们): pass fn(实参们) ''' # 形参:定义函数,在括号内声明的变量名,用来结束外界传来的值 # 实参:调用函数,在括号内传入的实际值,值可以为常量.变量.表达式 ...

  9. mysql 监控工具(windows版本)

    文章转自  https://www.cnblogs.com/wucj/p/7152020.html 工具下载  http://www.profilesql.com/download/

  10. bzoj-1787-洛谷-4281(LCA板子题)

    传送门(bzoj) 传送门(洛谷) 可以说这道也是一个板子题 由于题中是三个人需经过的路径最短 就会有一点点不太一样 那么 就两两求LCA 这样之后就会出现两种状况 一.所得到的三个LCA是相等的 那 ...