#coding=utf-8
import argparse#解析命令行参数和选项
import numpy as np# numpy科学计算的库,可以提供矩阵运算
from scipy.stats import norm#scipy数值计算库
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib import animation#matplotlib绘图库
import seaborn as sns# 数据模块可视化
sns.set(color_codes=True) #sns.set(style="white", palette="muted", color_codes=True) #set( )设置主题,调色板更常用 ,muted,柔和的
seed = 42# 设置seed,使得每次生成的随机数相同
np.random.seed(seed)
tf.set_random_seed(seed)
class DataDistribution(object):#真实数据分布(蓝色的线)
def __init__(self):
self.mu = 4#均值
self.sigma = 0.5#标准差
def sample(self, N):
samples = np.random.normal(self.mu, self.sigma, N)
samples.sort()
return samples
class GeneratorDistribution(object):#G网络的输入,随机噪声分布
def __init__(self, range):
self.range = range
def sample(self, N):
#均匀分布
return np.linspace(-self.range, self.range, N) + \
np.random.random(N) * 0.01#随机0-1
'''
samples = np.random.normal(4, 0.5, N)
samples.sort()
return samples
'''
def linear(input, output_dim, scope=None, stddev=1.0):#w和b参数的初始化#线性计算,计算y=wx+b
norm = tf.random_normal_initializer(stddev=stddev)#用高斯的随机初始化给w进行初始化
const = tf.constant_initializer(0.0)#用常量0给b进行初始化
with tf.variable_scope(scope or 'linear'):#变量域为scope(默认继承外层变量域)的值,当值为None时,域为linear
w = tf.get_variable('w', [input.get_shape()[1], output_dim], initializer=norm)#input.get_shape()[1]获取input的列数
b = tf.get_variable('b', [output_dim], initializer=const)
return tf.matmul(input, w) + b
def generator(input, h_dim):#生成网络
#h0 = tf.nn.tanh(linear(input, h_dim, 'g0'))
#h0 = tf.nn.sigmoid(linear(input, h_dim, 'g0'))
h0 = tf.nn.relu(linear(input, h_dim, 'g0'))#较好
#h1 = tf.nn.relu(linear(h0, h_dim, 'g1'))#
#h2 = linear(h1, 1, 'g2')
#return h2
#h0 = tf.nn.softplus(linear(input, h_dim, 'g0'))#原
h1 = linear(h0, 1, 'g1')#原
return h1#原
def discriminator(input, h_dim):#初始判别网络
h0 = tf.tanh(linear(input, h_dim * 2, 'd0'))#第一层的输出
import argparse#解析命令行参数和选项
import numpy as np# numpy科学计算的库,可以提供矩阵运算
from scipy.stats import norm#scipy数值计算库
import tensorflow as tf
import matplotlib.pyplot as plt
from matplotlib import animation#matplotlib绘图库
import seaborn as sns# 数据模块可视化
sns.set(color_codes=True) #sns.set(style="white", palette="muted", color_codes=True) #set( )设置主题,调色板更常用 ,muted,柔和的
seed = 42# 设置seed,使得每次生成的随机数相同
np.random.seed(seed)
tf.set_random_seed(seed)
class DataDistribution(object):#真实数据分布(蓝色的线)
def __init__(self):
self.mu = 4#均值
self.sigma = 0.5#标准差
def sample(self, N):
samples = np.random.normal(self.mu, self.sigma, N)
samples.sort()
return samples
class GeneratorDistribution(object):#G网络的输入,随机噪声分布
def __init__(self, range):
self.range = range
def sample(self, N):
#均匀分布
return np.linspace(-self.range, self.range, N) + \
np.random.random(N) * 0.01#随机0-1
'''
samples = np.random.normal(4, 0.5, N)
samples.sort()
return samples
'''
def linear(input, output_dim, scope=None, stddev=1.0):#w和b参数的初始化#线性计算,计算y=wx+b
norm = tf.random_normal_initializer(stddev=stddev)#用高斯的随机初始化给w进行初始化
const = tf.constant_initializer(0.0)#用常量0给b进行初始化
with tf.variable_scope(scope or 'linear'):#变量域为scope(默认继承外层变量域)的值,当值为None时,域为linear
w = tf.get_variable('w', [input.get_shape()[1], output_dim], initializer=norm)#input.get_shape()[1]获取input的列数
b = tf.get_variable('b', [output_dim], initializer=const)
return tf.matmul(input, w) + b
def generator(input, h_dim):#生成网络
#h0 = tf.nn.tanh(linear(input, h_dim, 'g0'))
#h0 = tf.nn.sigmoid(linear(input, h_dim, 'g0'))
h0 = tf.nn.relu(linear(input, h_dim, 'g0'))#较好
#h1 = tf.nn.relu(linear(h0, h_dim, 'g1'))#
#h2 = linear(h1, 1, 'g2')
#return h2
#h0 = tf.nn.softplus(linear(input, h_dim, 'g0'))#原
h1 = linear(h0, 1, 'g1')#原
return h1#原
def discriminator(input, h_dim):#初始判别网络
h0 = tf.tanh(linear(input, h_dim * 2, 'd0'))#第一层的输出