GAN简单例子(生成正态分布图像)(源码)

#coding=utf-8import argparse#解析命令行参数和选项import numpy as np# numpy科学计算的库,可以提供矩阵运算from scipy.stats import norm#scipy数值计算库import tensorflow as tfimport matplotlib.pyplot as pltfrom matplotlib import anima...
摘要由CSDN通过智能技术生成
#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'))#第一层的输出
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值