基于卷积与反卷积的GAN简单实现---MNIST+Tensorflow

目前来说简单的GAN模型生成器的框架主要有两种,一种是通过全连接的方式实现,而另一种是通过反卷积的方式实现;全连接的方式简单但是对于一个多层模型来说,其参数量显得过于庞大,而反卷积方法的好处就在于能够在较少参数量的情况下实现不错质量的图像生成。

本文主要参照DCGAN的程序,演示如何使用卷积和反卷积网络构建GAN;设计的生成器由一个100维的随机向量通过反卷积网络生成28*28的MNIST类似的手写字体样本,鉴别器主要是通过卷积网络对图像进行处理最终输出图像真实概率。

程序及注释如下:

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import math

#数据集路径
mnist = input_data.read_data_sets("/home/mnist/raw/",one_hot=True)
sess = tf.InteractiveSession()
#参数定义
batchs = 64
df_dim = 64#鉴别器通道
gf_dim = 64#生成器通道
z_dim = 100#随机向量维度
output_height = 28#输出图像高度
output_weight = 28#输出图像宽度
image_dims = [output_height,output_weight,1]
inputs = tf.placeholder(tf.float32,[batchs]+image_dims,name = "real_images")
z = tf.placeholder(tf.float32,[None,z_dim],name = 'z')
x = tf.placeholder(tf.float32,[batchs,784],name = 'x')
x_image = tf.reshape(x ,[batchs,28,28,1])

#相关函数定义
#卷积函数定义
def conv2d(input_,output_dim,k_h=5,k_w=5,d_h=2,d_w=2,steddev=0.02,name="conv2d"):
    with tf.variable_scope(name):
        w = tf.get_variable('w',[k_h,k_w,input_.get_shape()[-1],output_dim],\
                            initializer= tf.truncated_normal_initializer(stddev=steddev))
        conv = tf.nn.conv2d(input_,w,strides=[1,d_h,d_w,1],padding='SAME')
        biases = tf.get_variable('biases',[output
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值