<jupyter_start><jupyter_text>Generator<jupyter_code>GZ1_L = tf.keras.layers.Conv2DTranspose(filters=8, kernel_size=[5, 5], use_bias=False, padding="same")
GB1_L = tf.keras.layers.BatchNormalization(axis=3)
GA1_L = tf.keras.layers.LeakyReLU(alpha=0.1)
GU1_L = tf.keras.layers.UpSampling2D(size=(2, 2))

GZ2_L = tf.keras.layers.Conv2DTranspose(filters=16, kernel_size=[5, 5], use_bias=False, padding="same")
GB2_L = tf.keras.layers.BatchNormalization(axis=3)
GA2_L = tf.keras.layers.LeakyReLU(alpha=0.1)
GU2_L = tf.keras.layers.UpSampling2D(size=(2, 2))

GZ3_L = tf.keras.layers.Conv2DTranspose(filters=32, kernel_size=[5, 5], use_bias=False, padding="same")
GB3_L = tf.keras.layers.BatchNormalization(axis=3)
GA3_L = tf.keras.layers.LeakyReLU(alpha=0.1)
GU3_L = tf.keras.layers.UpSampling2D(size=(2, 2))

GZ4_L = tf.keras.layers.Conv2DTranspose(filters=16, kernel_size=[5, 5], use_bias=False, padding="same")
GB4_L = tf.keras.layers.BatchNormalization(axis=3)
GA4_L = tf.keras.layers.LeakyReLU(alpha=0.1)
GU4_L = tf.keras.layers.UpSampling2D(size=(2, 2))

GZ5_L = tf.keras.layers.Conv2DTranspose(filters=8, kernel_size=[5, 5], use_bias=False, padding="same")
GB5_L = tf.keras.layers.BatchNormalization(axis=3)
GA5_L = tf.keras.layers.LeakyReLU(alpha=0.1)
GU5_L = tf.keras.layers.UpSampling2D(size=(2, 2))

GZ6_L = tf.keras.layers.Conv2DTranspose(filters=3, kernel_size=[5, 5], use_bias=False, padding="same")
GB6_L = tf.keras.layers.BatchNormalization(axis=3)
GA6_L = tf.keras.layers.Activation('sigmoid')

def buildup_generator(GX):
    GZ1 = GZ1_L(GX)
    GB1 = GB1_L(GZ1)
    GA1 = GA1_L(GB1)
    GU1 = GU1_L(GA1)

    GZ2 = GZ2_L(GU1)
    GB2 = GB2_L(GZ2)
    GA2 = GA2_L(GB2)
    GU2 = GU2_L(GA2)

    GZ3 = GZ3_L(GU2)
    GB3 = GB3_L(GZ3)
    GA3 = GA3_L(GB3)
    GU3 = GU3_L(GA3)

    GZ4 = GZ4_L(GU3)
    GB4 = GB4_L(GZ4)
    GA4 = GA4_L(GB4)
    GU4 = GU4_L(GA4)

    GZ5 = GZ5_L(GU4)
    GB5 = GB5_L(GZ5)
    GA5 = GA5_L(GB5)
    GU5 = GU5_L(GA5)

    GZ6 = GZ6_L(GU5)
    GB6 = GB6_L(GZ6)
    GY = GA6_L(GB6)
    return GY
GX = tf.keras.layers.Input(shape=[8, 8, 1], dtype=tf.float32)
GY = buildup_generator(GX)

generator = tf.keras.models.Model(inputs=GX, outputs=GY)
generator.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01, clipvalue=1.0), loss=tf.keras.losses.MeanSquaredError(), metrics=['accuracy'])
generator.summary()
generator.save('generator.h5')
noiseimg = np.random.rand(1, 8, 8, 1)
plt.imshow(np.reshape(noiseimg, (8, 8, 1)))
plt.imshow(generator.predict(noiseimg)[0])<jupyter_output><empty_output><jupyter_text>Discriminator<jupyter_code>DZ1_L = tf.keras.layers.Conv2D(filters=32, kernel_size=[5, 5], use_bias=False, padding="same")
DB1_L = tf.keras.layers.BatchNormalization(axis=3)
DA1_L = tf.keras.layers.LeakyReLU(alpha=0.1)
DU1_L = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2], padding="same")

DZ2_L = tf.keras.layers.Conv2D(filters=64, kernel_size=[5, 5], use_bias=False, padding="same")
DB2_L = tf.keras.layers.BatchNormalization(axis=3)
DA2_L = tf.keras.layers.LeakyReLU(alpha=0.1)
DU2_L = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2], padding="same")

DZ3_L = tf.keras.layers.Conv2D(filters=64, kernel_size=[5, 5], use_bias=False, padding="same")
DB3_L = tf.keras.layers.BatchNormalization(axis=3)
DA3_L = tf.keras.layers.LeakyReLU(alpha=0.1)
DU3_L = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2], padding="same")

DZ4_L = tf.keras.layers.Conv2D(filters=32, kernel_size=[5, 5], use_bias=False, padding="same")
DB4_L = tf.keras.layers.BatchNormalization(axis=3)
DA4_L = tf.keras.layers.LeakyReLU(alpha=0.1)
DU4_L = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2], padding="same")

DZ5_L = tf.keras.layers.Conv2D(filters=8, kernel_size=[5, 5], use_bias=False, padding="same")
DB5_L = tf.keras.layers.BatchNormalization(axis=3)
DA5_L = tf.keras.layers.LeakyReLU(alpha=0.1)
DU5_L = tf.keras.layers.MaxPool2D(pool_size=[2,2], strides=[2,2], padding="same")

DF6_L = tf.keras.layers.Flatten()
DZ6_L = tf.keras.layers.Dense(units=16, use_bias=False)
DB6_L = tf.keras.layers.BatchNormalization()
DA6_L = tf.keras.layers.LeakyReLU(alpha=0.1)

DZ7_L = tf.keras.layers.Dense(units=1, use_bias=True)

DY_L = tf.keras.layers.Activation('sigmoid')

def buildup_dicriminator(DX):
    DZ1 = DZ1_L(DX)
    DB1 = DB1_L(DZ1)
    DA1 = DA1_L(DB1)
    DU1 = DU1_L(DA1)

    DZ2 = DZ2_L(DU1)
    DB2 = DB2_L(DZ2)
    DA2 = DA2_L(DB2)
    DU2 = DU2_L(DA2)

    DZ3 = DZ3_L(DU2)
    DB3 = DB3_L(DZ3)
    DA3 = DA3_L(DB3)
    DU3 = DU3_L(DA3)

    DZ4 = DZ4_L(DU3)
    DB4 = DB4_L(DZ4)
    DA4 = DA4_L(DB4)
    DU4 = DU4_L(DA4)

    DZ5 = DZ5_L(DU4)
    DB5 = DB5_L(DZ5)
    DA5 = DA5_L(DB5)
    DU5 = DU5_L(DA5)

    DF6 = DF6_L(DU5)
    DZ6 = DZ6_L(DF6)
    DB6 = DB6_L(DZ6)
    DA6 = DA6_L(DB6)

    DZ7 = DZ7_L(DA6)

    DY = DY_L(DZ7)
    return DY

def set_discriminator(trainable):
    DZ1_L.trainable = trainable
    DB1_L.trainable = trainable
    DZ2_L.trainable = trainable
    DB2_L.trainable = trainable
    DZ3_L.trainable = trainable
    DB3_L.trainable = trainable
    DZ4_L.trainable = trainable
    DB4_L.trainable = trainable
    DZ5_L.trainable = trainable
    DB5_L.trainable = trainable
    DZ6_L.trainable = trainable
    DB6_L.trainable = trainable
    DZ7_L.trainable = trainable    
DX = tf.keras.layers.Input(shape=[256, 256, 3], dtype=tf.float32)
DY = buildup_dicriminator(DX)

discriminator = tf.keras.models.Model(inputs=DX, outputs=DY)
discriminator.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01, clipvalue=1.0), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])
discriminator.summary()
discriminator.predict(generator.predict(np.random.rand(10, 8, 8, 1)))
discriminator.predict(monet_images[np.random.randint(monet_images.shape[0], size=[10])])<jupyter_output><empty_output><jupyter_text>Learning data for discriminator<jupyter_code>batch_semisize = 1000
monet_X = monet_images[np.random.randint(monet_images.shape[0], size=[batch_semisize]), :, :, :]
monet_Y = np.array([1] * batch_semisize)
generated_X = generator.predict(np.random.rand(batch_semisize, 8, 8, 1))
generated_Y = np.array([0] * batch_semisize)
perm = np.random.permutation(2 * batch_semisize)
batch_X = np.concatenate((monet_X, generated_X))[perm]
batch_Y = np.concatenate((monet_Y, generated_Y))[perm]<jupyter_output><empty_output><jupyter_text>Train Discriminator<jupyter_code>set_discriminator(True)  # trainable
discriminator.fit(tf.convert_to_tensor(batch_X, dtype=tf.float32), tf.convert_to_tensor(batch_Y, dtype=tf.float32), epochs=1000)
discriminator.save('discriminator.h5')<jupyter_output><empty_output><jupyter_text>Join Discriminator-Generator<jupyter_code>X = tf.keras.layers.Input(shape=[8, 8, 1], dtype=tf.float32)
Z = buildup_generator(X)
Y = buildup_dicriminator(Z)

model = tf.keras.models.Model(inputs=X, outputs=Y)
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01, clipvalue=1.0), loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])
model.summary()<jupyter_output>Model: "functional_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_6 (InputLayer)         [(None, 8, 8, 1)]         0         
_________________________________________________________________
conv2d_transpose_5 (Conv2DTr (None, 8, 8, 8)           200       
_________________________________________________________________
batch_normalization_11 (Batc (None, 8, 8, 8)           32        
_________________________________________________________________
leaky_re_lu_11 (LeakyReLU)   (None, 8, 8, 8)           0         
_________________________________________________________________
up_sampling2d_5 (UpSampling2 (None, 16, 16, 8)         0         
_________________________________________________________________
conv2d_transpose_6 (Conv2DTr (None, 16, 16, 16)        3200      
_____________________________________________________[...]<jupyter_text>Learning data for Generator<jupyter_code>g_batch_size = 1000
noises = np.random.rand(g_batch_size, 8, 8, 1)
positives = np.array([1] * g_batch_size)<jupyter_output><empty_output><jupyter_text>Train Generator<jupyter_code>set_discriminator(False)  # non-trainable discriminator weights
model.fit(tf.convert_to_tensor(noises, dtype=tf.float32), tf.convert_to_tensor(positives, dtype=tf.float32), epochs=1000)
model.save('model.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15106558342164136, 'fraction_numerical': 0.055221947486165074, 'mean_word_length': 5.43972706595906, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '227446', 'license': 'MIT', 'n_tokens_mistral': 3741, 'n_tokens_neox': 3542, 'n_words': 462, 'path': '1.Model.ipynb', 'repo_name': 'pablo53/gan-getting-started', 'size': '8493'}