<jupyter_start><jupyter_text>prework* import basic dependencies* load data* check data<jupyter_code># This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
from keras.datasets import mnist

# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
path = '/kaggle/input/mnist-numpy/mnist.npz'
f = np.load(path)
train_images, train_labels = f['x_train'], f['y_train']
test_images, test_labels = f['x_test'], f['y_test']
f.close()<jupyter_output><empty_output><jupyter_text>Data Preprocessing* reshape* transform data type* normalization* label => one-hot encoding<jupyter_code>from keras.utils import to_categorical
# reshape
X_train = X_train.reshape([60000, 784])
X_test = X_test.reshape([10000, 784])
X_train.shape, X_test.shape
# transform np data type
X_train = X_train.astype(np.float32)
X_test = X_test.astype(np.float32)
# normalization
X_train = X_train/255
X_test = X_test/255
print(X_train)
# one-hot encoding
train_labels = to_categorical(y_train)
test_labels = to_categorical(y_test)<jupyter_output><empty_output><jupyter_text>Model* build model* compile, summary* fit, evaluate* advanced NN model<jupyter_code>from keras.models import Sequential
from keras.layers import Dense, Activation
# build simple model
model = Sequential()
model.add(Dense(10, input_shape=(28*28, )))
model.add(Activation('softmax'))
# compile
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
# summary
model.summary()
# fit
model.fit(X_train, train_labels, epochs=200, batch_size=128, verbose=True, validation_split=0.2)
# evaluate
test_loss, test_acc = model.evaluate(X_test, test_labels, verbose=True)
print('test_loss', test_loss, '\n', 'test_acc', test_acc)
# advanced NN model
model = Sequential()
model.add(Dense(128, input_shape=(28*28, ),activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))
# compile
model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])
# summary
model.summary()
# fit
model.fit(X_train, train_labels, epochs=200, batch_size=128, verbose=True, validation_split=0.2)
# evaluate
test_loss, test_acc = model.evaluate(X_test, test_labels, verbose=True)
print('test_loss', test_loss, '\n', 'test_acc', test_acc)<jupyter_output>313/313 [==============================] - 1s 2ms/step - loss: 0.0775 - accuracy: 0.9766
test_loss 0.07749566435813904 
 test_acc 0.9765999913215637<jupyter_text>Advanced CNN<jupyter_code># reshape
X_train = X_train.reshape(-1, 28, 28, 1)
X_test = X_test.reshape(-1, 28, 28, 1)
from keras.layers import Conv2D, BatchNormalization, Dropout, Conv2D, MaxPool2D, Conv2D, Flatten, Dense
from keras.optimizers import Adam
# build advanced advanced CNN model
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='Same', activation='relu', input_shape=(28, 28, 1)))
# add two layers => avoid overfit
model.add(BatchNormalization(axis=1)) 
model.add(Dropout(0.5))
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='Same', activation='relu', input_shape=(28, 28, 1)))
model.add(BatchNormalization(axis=1)) 

model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='Same', activation='relu'))
model.add(BatchNormalization(axis=1)) 
model.add(Dropout(0.5))
model.add(Conv2D(filters=64, kernel_size=(3, 3), padding='Same', activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(BatchNormalization()) 
model.add(Dropout(0.5))
model.add(Dense(512, activation='relu'))
model.add(BatchNormalization()) 
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# compile
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
# summary
model.summary()

model.fit(X_train, train_labels, epochs=15, batch_size=128, verbose=True, validation_split=0.1)
# evaluate
test_loss, test_acc = model.evaluate(X_test, test_labels, verbose=True)
print('test_loss', test_loss, '\n', 'test_acc', test_acc)<jupyter_output>313/313 [==============================] - 1s 3ms/step - loss: 0.0223 - accuracy: 0.9932
test_loss 0.02232465147972107 
 test_acc 0.9932000041007996<jupyter_text>Add image_generator for data augmentation<jupyter_code>from keras.preprocessing.image import ImageDataGenerator
generator = ImageDataGenerator(rotation_range=10, width_shift_range=0.10, shear_range=0.5, height_shift_range=0.10, zoom_range=0.10)
# compile model with data generator
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
model.optimizer.lr=0.001
batches = generator.flow(X_train, train_labels, batch_size=128)
# fit
%time history = model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=15)
# evaluate
test_loss, test_acc = model.evaluate(X_test, test_labels, verbose=True)
print('test_loss', test_loss, '\n', 'test_acc', test_acc)<jupyter_output>313/313 [==============================] - 1s 3ms/step - loss: 0.0162 - accuracy: 0.9939
test_loss 0.016232000663876534 
 test_acc 0.9939000010490417
{'fraction_non_alphanumeric': 0.14788373278939318, 'fraction_numerical': 0.0543940166581676, 'mean_word_length': 7.206415620641562, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 20, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '310037', 'license': 'MIT', 'n_tokens_mistral': 2258, 'n_tokens_neox': 2022, 'n_words': 466, 'path': 'mnist-nn.ipynb', 'repo_name': 'Aczy156/ml-course-homework', 'size': '5883'}