建立神经网络
我们将通过部署以下函数来建立GANs的主要部分
- model_input
- discirminator
- generator
- model_loss
- model_opt
- train
Input
def model_inputs(image_width, image_height, image_channels, z_dim):
"""
Create the model inputs
:param image_width: The input image width
:param image_height: The input image height
:param image_channels: The number of image channels
:param z_dim: The dimension of Z
:return: Tuple of (tensor of real input images, tensor of z data, learning rate)
"""
# TODO: Implement Function
input_real = tf.placeholder(tf.float32, [None,image_width,image_height,image_channels],name = 'input_real')
input_z=tf.placeholder(tf.float32,[None, z_dim],name= 'input_z')
lr = tf.placeholder(tf.float32, name='lr')
return input_real,input_z,lr
discriminator
def discriminator(images, reuse=False,alpha = 0.2):
"""
Create the discriminator network
:param image: Tensor of input image(s)
:param reuse: Boolean if the weights should be reused
:return: Tuple of (tensor output of the discriminator, tensor logits of the discriminator)
"""
# TODO: Implement Function
with tf.variable_scope('discriminator', reuse=reuse):
#print(images.shape) 28*28*3
x1 = tf.layers.conv2d(images, 32, 5, strides=2, padding='same')
relu1 = tf.maximum(alpha * x1, x1)
x2 = tf.layers.conv2d(relu1, 64, 5, strides=2, padding='same