A simple version against the network - handwritten digital MNIST training

1, the confrontation network is a data-driven network. There are fewer human interventions.

The loss of the generated network exploits the loss of the discriminator. The data input of the discriminator utilizes the generated data and the real data of the generated network.

Two networks re-weight update is not interfere with each other. All only update their own weight values.

The following is the implementation code of the simple gan network:

Import tensorflow as tf
Import numpy as np
Import tensorflow.examples.tutorials.mnist as input_data
Import matplotlib.pyplot as plt
Import matplotlib.gridspec as gridspec
Import os.path as op
Import os

Mnist = input_data.input_data.read_data_sets("MNIST_data/", one_hot=True)

X_dim = 784
Z_dim = 100
Batch_size = 128

# Authentic placeholder for input images
X = tf.placeholder(tf.float32, shape=[None, X_dim])
#Enter a placeholder for the input data of the generator
Z = tf.placeholder(tf.float32, shape=[None, Z_dim])
#干扰样本
X_per = tf.placeholder(tf.float32, shape=[None, X_dim])
# Weight used by the discriminator
D_W1 = tf.Variable(tf.truncated_normal([X_dim, 100], stddev=0.1), dtype=tf.float32)
D_b1 = tf.Variable(tf.constant(0.1, shape=[100]))
D_W2 = tf.Variable(tf.truncated_normal([100, 1], stddev=0.1), dtype=tf.float32)
D_b2 = tf.Variable(tf.constant(0.1, shape=[1]))
D_var_list = [D_W1, D_b1, D_W2, D_b2]
# Generator usage weights
G_W1 = tf.Variable(tf.truncated_normal([Z_dim, 100], stddev=0.1), dtype=tf.float32)
G_b1 = tf.Variable(tf.constant(0.1, shape=[100]))
G_W2 = tf.Variable(tf.truncated_normal([100, X_dim], stddev=0.1), dtype=tf.float32)
G_b2 = tf.Variable(tf.constant(0.1, shape=[X_dim]))
G_var_list = [G_W1, G_b1, G_W2, G_b2]


# Define a function that generates the image of the generator
Def plot(samples):
    Fig = plt.figure(figsize=(4, 4))
    Gs = gridspec.GridSpec(4, 4)
    Gs.update(wspace=0.5, hspace=0.5)
    For i, sample in enumerate(samples):
        Plt.subplot(gs[i])
        Plt.imshow(sample.reshape(28, 28), cmap='Greys_r')

    Return fig


# Define the network to authenticate it
Def discriminator(x):
    Output1 = tf.nn.relu(tf.matmul(x, D_W1) + D_b1)
    Output2 = tf.matmul(output1, D_W2) + D_b2
    Return output2


# Define the generator network
Def genemator(x):
    Output1 = tf.nn.relu(tf.matmul(x, G_W1) + G_b1)
    # this builds it needs to use to sigmoid
    Output2 = tf.matmul(output1, G_W2) + G_b2
    Output3 = tf.nn.sigmoid(output2)
    Return output3


Def get_perterbed_batch(minibatch):
    Return minibatch + 0.5 * np.random.random(minibatch.shape)


# Define the loss function
G_samples = genemator(Z)
D_Gsample_out = discriminator(G_samples)
D_X_out = discriminator(X)
D_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=D_X_out, labels=tf.ones_like(D_X_out)))
D_loss_fake = tf.reduce_mean(
    Tf.nn.sigmoid_cross_entropy_with_logits(logits=D_Gsample_out, labels=tf.zeros_like(D_Gsample_out)))
# Add some interference sample loss to the training discriminator
Alpha = tf.random_uniform(
    Shape=[batch_size,1],
    Minval=0.,
    Maxval=1.
)
Differences = X_per - X
Interpolates=X+(alpha*differences)
Gradients = tf.gradients(discriminator(interpolates), [interpolates])[0]
Slopes=tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1]))
Gradient_penalty = tf.reduce_mean((slopes-1.)**2)


sum_D_loss = D_loss_real + D_loss_fake
G_loss = tf.reduce_mean(
    Tf.nn.sigmoid_cross_entropy_with_logits(logits=D_Gsample_out, labels=tf.ones_like(D_Gsample_out)))
# Define optimizer
D_train_op = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.5, beta2=0.9).minimize(sum_D_loss,
                                                                                         Var_list=D_var_list)
G_train_op = tf.train.AdamOptimizer(learning_rate=0.0001, beta1=0.5, beta2=0.9).minimize(G_loss, var_list=G_var_list)
#Create a session and initialize all defined variables
Session = tf.Session()
Session.run(tf.global_variables_initializer())

If not op.exists("outown/"):
    Os.makedirs("outown/")

# Define variables that store training data
plotD = []
plotG = []
i = 0
#进行训练
For it in range(1, 200000):
    X_mb, _ = mnist.train.next_batch(batch_size)
    X_permb = get_perterbed_batch(X_mb)
    Z_mb = np.random.uniform(-1., 1., size=[batch_size, Z_dim])
    #Run discriminator diagram
    _, D_curloss = session.run([D_train_op, sum_D_loss], feed_dict={X: X_mb, Z: Z_mb, X_per: X_permb})
    #Run builder diagram
    _, G_curloss = session.run([G_train_op, G_loss], feed_dict={Z: Z_mb})
    plotG.append(G_curloss)
    plotD.append(D_curloss)
    If it % 1000 == 0:
        Plt.subplot()
        plotnD = np.array(plotD)
        Plt.plot(plotnD)
        plotnG = np.array(plotG)
        Plt.plot(plotnG)
        Plt.show()
        showG = np.random.uniform(-1., 1., size=[16, Z_dim])
        Samples = session.run(G_samples, feed_dict={Z: showG})
        Curfig = plot(samples)
        Curfig.savefig("outown/{}.png".format(str(i).zfill(4)))
        Print("iterate:{} ,D_loss{:.4},G_loss{:.4}".format(i, G_curloss, D_curloss))
        i += 1

The loss curve of the network is as follows:

The last picture produced is as follows:

looks like the number is still model.