<jupyter_start><jupyter_text>Generative Models<jupyter_code>vgg19 = VGG19()
vgg19.summary()
for layer in vgg19.layers[1:9]:
    print(layer.name)
for layer in vgg19.layers[:9]:
    layer.trainable = False
vgg19.summary()
(data_train, labels_train), (data_test, labels_test) = load_data()
tf.expand_dims(data_train, -1).shape
data_train = tf.expand_dims(data_train, -1)
data_test = tf.expand_dims(data_test, -1)
data_train = tf.cast(data_train, tf.float32) / 255.0
data_test = tf.cast(data_test, tf.float32) / 255.0
tf.keras.backend.clear_session()
cnn = None
input_layer = Input(shape = (28, 28, 1))

cnn = Conv2D(filters = 64, kernel_size = (3, 3), activation = "relu", padding = "same")(input_layer)
cnn = MaxPool2D()(cnn)
cnn = Conv2D(filters = 32, kernel_size = (3, 3), activation = "relu", padding = "same")(cnn)
cnn = MaxPool2D()(cnn)

representation = Conv2D(filters = 16, kernel_size = (3, 3), activation = "relu", padding = "same", name = "representation")(cnn)

cnn = Conv2D(filters = 16, kernel_size = (3, 3), activation = "relu", padding = "same")(representation)
cnn = UpSampling2D()(cnn)
cnn = Conv2D(filters = 32, kernel_size = (3, 3), activation = "relu", padding = "same")(cnn)
cnn = UpSampling2D()(cnn)
cnn = Conv2D(filters = 64, kernel_size = (3, 3), activation = "relu", padding = "same")(cnn)

output_layer = Conv2D(filters = 1, kernel_size = (3, 3), padding = "same")(cnn)
model = Model(inputs = input_layer, outputs = output_layer)
model.summary()
encoder = Model(inputs = input_layer, outputs = representation)
encoder.summary()
model.compile(loss = "mse", optimizer  = "adam")
model.fit(data_train, data_train, epochs  = 1)
data_train[:10].shape
prediction_train = model.predict(tf.squeeze(data_train[:10]))
prediction_train.shape
for image in prediction_train:
    print(image.shape)
    break
for image in prediction_train:
    plt.imshow(tf.squeeze(image))
    break
for original, pred in zip(tf.squeeze(data_train[:10]), prediction_train):
    plt.imshow(tf.squeeze(original), cmap = "Reds")
    plt.show()     
    plt.imshow(tf.squeeze(pred), cmap = "Blues")
    plt.show() 
    
    plt.imshow(tf.abs(tf.squeeze(original) - tf.squeeze(pred)))
    plt.show()
tf.abs(tf.squeeze(original) - tf.squeeze(pred))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1623290692545214, 'fraction_numerical': 0.035288928098809, 'mean_word_length': 5.3, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '520720', 'license': 'MIT', 'n_tokens_mistral': 919, 'n_tokens_neox': 865, 'n_words': 178, 'path': 'generative_models/Generative Models Demos.ipynb', 'repo_name': 'PetkoAndreev/Python-Deep-Learning', 'size': '2267'}