<jupyter_start><jupyter_text>Section 5---<jupyter_code>import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense , Activation

from keras.datasets import mnist 
def load_data():
    
    return mnist.load_data()
def prepare_data(train_images, test_images):

    x_train = train_images.reshape((60000, 784))
    x_train = x_train.astype('float32')/255

    x_test = test_images.reshape((10000, 784))
    x_test = x_test.astype('float32')/255

    return (x_train, x_test )
def add_noise(x_train, x_test, noise_factor_train, noise_factor_test):

    x_train_noisy = x_train + noise_factor_train * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 
    x_test_noisy = x_test + noise_factor_test * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 

    x_train_noisy = np.clip(x_train_noisy, 0., 1.)
    x_test_noisy = np.clip(x_test_noisy, 0., 1.)

    return x_train_noisy, x_test_noisy
def model(x_train, y_train, x_test, y_test ):
    
    model = Sequential()
    # 128 , 64 / 64 , 32
    model.add(Dense(128, activation='relu', input_shape=(784,)))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(784, activation = 'sigmoid'))

    model.summary()

    model.compile(optimizer='adam', loss='binary_crossentropy')

    history = model.fit(x_train, y_train,
            batch_size=512, epochs=25, verbose=0,
            validation_data=(x_train, y_train))

    denoise_image_test = model.predict(x_test)    

    return history, denoise_image_test
def show_result(x_test_noisy, test_images, denoise_image_test):
    n = 10
    for i in range(n):
        # Display original
        ax = plt.subplot(3, n, i + 1)
        plt.imshow(test_images[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

        # Display noisy 
        ax = plt.subplot(3, n, i + 1 + n)
        plt.imshow(x_test_noisy[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

        # Display reconstruction
        ax = plt.subplot(3, n, i + 1 + 2*n )
        plt.imshow(denoise_image_test[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.show()
def plot(history):
    # summarize history for loss
    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
def main(noise_factor_train = 0.4, noise_factor_test = 0.7):

    (train_images, _), (test_images, _) = load_data()
   
    x_train, x_test = prepare_data(train_images, test_images)

    x_train_noisy, x_test_noisy = add_noise(x_train, x_test, noise_factor_train, noise_factor_test)


    history, denoise_image_test = model(x_train_noisy, x_train, x_test_noisy, x_test)
    
    show_result(x_test_noisy, test_images, denoise_image_test)

    plot(history)
main(noise_factor_train = 0.3, noise_factor_test = 0.3)
main(noise_factor_train = 0.5, noise_factor_test = 0.5)
main(noise_factor_train = 0.7, noise_factor_test = 0.7)
main(noise_factor_train = 0.9, noise_factor_test = 0.9)
main(noise_factor_train = 0.4, noise_factor_test = 0.7)
main(noise_factor_train = 0.7, noise_factor_test = 0.4)<jupyter_output>Model: "sequential_6"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_18 (Dense)             (None, 128)               100480    
_________________________________________________________________
dense_19 (Dense)             (None, 64)                8256      
_________________________________________________________________
dense_20 (Dense)             (None, 784)               50960     
=================================================================
Total params: 159,696
Trainable params: 159,696
Non-trainable params: 0
_________________________________________________________________
{'fraction_non_alphanumeric': 0.14217557251908397, 'fraction_numerical': 0.03745229007633588, 'mean_word_length': 3.664071190211346, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '566036', 'license': 'MIT', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1392, 'n_words': 253, 'path': 'MNIST_Dence_Remove_Noise.ipynb', 'repo_name': 'ghazaleh-mahmoodi/-Preceptron-Implementation-without-framework', 'size': '4192'}