<jupyter_start><jupyter_text>Theoretical remark  Binary cross entropy loss\begin{equation*}L(\theta) = - \frac{1}{n} \sum_{i=1}^n [y_i log(p_i) + (1 - y_i) log(1 - p_i)]\end{equation*}- Discriminator final probability is 1 => REAL IMAGE- Discriminator final probability is 0 => FAKE IMAGELog values:- Log(1) => Loss would be 0- Log(0+) => Loss would be to - ∞ Generator:Maximize D(G(z)) Discriminator:Maximize D(x) AND minimize D(G(z))<jupyter_code># Losses have minus sign because I have to maximize them
D_loss = - tf.reduce_mean( tf.log(D_real) + tf.log(1. - D_fake) )
G_loss = - tf.reduce_mean( tf.log(D_fake) )

# Optimizers
D_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='discriminator')
D_optimizer = tf.train.AdamOptimizer(learning_rate=0.0005).minimize(D_loss, var_list=D_var)

G_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='generator')
G_optimizer = tf.train.AdamOptimizer(learning_rate=0.0005).minimize(G_loss, var_list=G_var)<jupyter_output><empty_output><jupyter_text>Training of the generator and discriminator network<jupyter_code>batch_size = 64

with tf.Session() as sess:

    # Run the initializer
    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    
    # Epochs
    for n in range(30):
        print("Epoch " + str(n+1))
        
        for i in range(len(x_train) // batch_size):
            
            X_tmp = np.reshape(x_train[i*batch_size:(i+1)*batch_size], (batch_size, -1))
            #X_tmp = x_train[i*batch_size:(i+1)*batch_size]
            
            sampled_noise = sample_Z(batch_size, Z_dim)
            one_hot_sampled = one_hot(batch_size, num_classes, y_train[i*batch_size:(i+1)*batch_size])
            
            _, D_loss_val = sess.run([D_optimizer, D_loss], feed_dict={X: X_tmp, 
                                                                       Y: one_hot_sampled,
                                                                       Z: sampled_noise})

            _, G_loss_val = sess.run([G_optimizer, G_loss], feed_dict={Y: one_hot_sampled,
                                                                       Z: sampled_noise})
            
            if i % 300 == 0:
                print(str(D_loss_val) + "  " + str(G_loss_val))
                save_path = saver.save(sess, "./checkpoints/model.ckpt")<jupyter_output>Epoch 1
0.95421404  2.7137277
0.16300228  4.1718526
0.013767928  5.6764627
0.013310271  6.8244886
Epoch 2
0.006558732  8.058235
0.0042580855  10.943196
0.0054634595  8.849958
0.04933323  3.9225428
Epoch 3
0.20747188  4.1831617
0.026214357  8.469037
0.04072192  7.346262
0.031122517  5.530775
Epoch 4
0.046090726  5.11003
0.10653249  6.1811914
0.17848419  4.4241347
0.08084142  4.8053803
Epoch 5
0.23476085  5.18079
0.07258764  5.5152216
0.059076965  5.4870343
0.06739945  5.7131615
Epoch 6
0.014707347  8.53326
0.061251864  6.100748
0.046907976  5.397523
0.050764  6.3975506
Epoch 7
0.12136417  6.4027653
0.11473328  5.735597
0.12664294  5.3891563
0.21780637  5.6415553
Epoch 8
0.16854784  6.121756
0.16422081  4.4690456
0.12301879  5.350264
0.16067845  4.7910204
Epoch 9
0.22020595  5.8412743
0.20873132  3.664666
0.23085178  4.1530704
0.2533369  4.857728
Epoch 10
0.24765852  4.557816
0.22004695  3.4729404
0.2738728  4.0640154
0.40452164  4.2124043
Epoch 11
0.3247204  3.8793058
0.41095752  3.597
[...]<jupyter_text>Generate images with constraint (Y)<jupyter_code>generate_number = 9

with tf.Session() as sess:
    
    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, "./checkpoints/model.ckpt")
    
    sampled_noise = sample_Z(1, Z_dim)
    one_hot_sampled = one_hot(1, num_classes, [generate_number])
    
    generated = sess.run(gen_sample, feed_dict={Y: one_hot_sampled, 
                                                Z: sampled_noise})
    img_generated = np.reshape(generated, (28, 28))
    
    plt.imshow(img_generated)
    plt.show()
fig, axes = plt.subplots(4, 4)
fig.subplots_adjust(hspace=0.1)

with tf.Session() as sess:
    
    saver = tf.train.Saver()
    sess.run(tf.global_variables_initializer())
    saver.restore(sess, "./checkpoints/model.ckpt")
    
    for i, ax in enumerate(axes.flat):
        generate_number = int(i / 4)
        sampled_noise = sample_Z(1, Z_dim)
        one_hot_sampled = one_hot(1, num_classes, [generate_number])

        generated = sess.run(gen_sample, feed_dict={Y: one_hot_sampled, 
                                                    Z: sampled_noise})
        img_generated = np.reshape(generated, (28, 28))

        ax.imshow(img_generated)            
        ax.set_xticks([])
        ax.set_yticks([])

    plt.show()<jupyter_output>INFO:tensorflow:Restoring parameters from ./checkpoints/model.ckpt
StarcoderdataJupyterStructuredCleanDedup