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

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D , Activation, Flatten , Dense, MaxPooling2D
from tensorflow.keras.layers import Dropout

from keras.layers.normalization import BatchNormalization
from keras.datasets import mnist
from keras.utils import np_utils, to_categorical
from sklearn.model_selection import KFold
def load_data():
    
    return mnist.load_data()
def prepare_data(train_images, train_labels, test_images, test_labels):

    x_train = train_images.reshape(60000,28,28,1)

    x_test = test_images.reshape(10000,28,28,1)

    #one hot encoding
    y_train =  to_categorical(train_labels) 
    y_test = to_categorical(test_labels)

    return (x_train, y_train, x_test, y_test )
def model(x_train, y_train, x_test, y_test ):
    
    kfold = KFold(n_splits=3, shuffle=True)
    fold_no = 0
    history = ''
    
    # Define per-fold score containers
    for train_index, test_index in kfold.split(x_train):
        print('------------------------------------------------------------------------')
        print(f'Training for fold {fold_no} ...')

        model = Sequential()

        model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='valid', input_shape=(28,28,1)))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=(2,2)))
        model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', padding='valid'))
        model.add(BatchNormalization())
        model.add(MaxPooling2D(pool_size=(2,2)))
        model.add(Flatten())
        model.add(Dense(64, activation='relu'))
        model.add(Dense(10, activation='softmax'))

        model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
        
        history = model.fit(x_train[train_index], y_train[train_index],
                batch_size=2048, epochs=10, verbose=2,
                validation_data=(x_train[test_index], y_train[test_index]))
        
        
        scores = model.evaluate(x_train[test_index], y_train[test_index], verbose=0)
        print(f'Score for fold {fold_no}: {model.metrics_names[0]} of {scores[0]}; {model.metrics_names[1]} of {scores[1]*100}%')

        fold_no += 1
    
    model.summary()

    score = model.evaluate(x_test, y_test)
    print('Test loss :', score[0])
    print('Test accuracy:', score[1] * 100)

    return history
def plot(history):
    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.legend(['train', 'test'], loc='upper left')
    plt.show()
    # 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():
    (train_images, train_labels), (test_images, test_labels) = load_data()
   
    x_train, y_train, x_test, y_test = prepare_data(train_images, train_labels, test_images, test_labels)

    history = model(x_train, y_train, x_test, y_test)
    
    plot(history)
main()<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
------------------------------------------------------------------------
Training for fold 0 ...
Epoch 1/10
20/20 - 9s - loss: 1.1231 - accuracy: 0.6632 - val_loss: 1.7051 - val_accuracy: 0.7250
Epoch 2/10
20/20 - 1s - loss: 0.2246 - accuracy: 0.9323 - val_loss: 1.0008 - val_accuracy: 0.8038
Epoch 3/10
20/20 - 1s - loss: 0.1344 - accuracy: 0.9594 - val_loss: 0.4424 - val_accuracy: 0.8837
Epoch 4/10
20/20 - 1s - loss: 0.0961 - accuracy: 0.9702 - val_loss: 0.2530 - val_accuracy: 0.9259
Epoch 5/10
20/20 - 1s - loss: 0.0751 - accuracy: 0.9773 - val_loss: 0.1413 - val_accuracy: 0.9567
Epoch 6/10
20/20 - 1s - loss: 0.0617 - accuracy: 0.9815 - val_loss: 0.1024 - val_accuracy: 0.9684
Epoch 7/10
20/20 - 1s - loss: 0.0521 - accuracy: 0.9844 - val_loss: 0.0894 - val_accuracy: 0.9719
Epoch 8/10
20/20 - 1s - loss: 0.0445 - accuracy: 0.9867 - va[...]<jupyter_text>noisy<jupyter_code>def show_result(x_test_noisy, test_images):
    n = 10
    for i in range(n):
        # Display original
        ax = plt.subplot(2, 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(2, n, i + 1)
        plt.imshow(x_test_noisy[i].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)

    plt.show()
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 main_noisy(noise_factor_train = 0.4, noise_factor_test = 0.7):

    (train_images, train_labels), (test_images, test_labels) = load_data()

    x_train, y_train, x_test, y_test = prepare_data(train_images, train_labels, test_images, test_labels)
    
    x_train_noisy, x_test_noisy = add_noise(x_train, x_test, noise_factor_train, noise_factor_test)

    history = model(x_train_noisy, y_train, x_test_noisy, y_test)
    
    show_result(x_train_noisy, x_test_noisy)

    plot(history)
main_noisy(noise_factor_train = 0.9, noise_factor_test = 0.9)
main_noisy(noise_factor_train = 0.7, noise_factor_test = 0.7)
main_noisy(noise_factor_train = 0.3, noise_factor_test = 0.3)
main_noisy(noise_factor_train = 0.4, noise_factor_test = 0.7)
main_noisy(noise_factor_train = 0.7, noise_factor_test = 0.4)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1577570704593755, 'fraction_numerical': 0.05787150564001962, 'mean_word_length': 3.2873160476524177, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '47622', 'license': 'MIT', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2217, 'n_words': 420, 'path': 'MNIST_CNN.ipynb', 'repo_name': 'ghazaleh-mahmoodi/-Preceptron-Implementation-without-framework', 'size': '6117'}