<jupyter_start><jupyter_text>Imports<jupyter_code>import tensorflow as tf
import numpy as np

from tensorflow.keras import datasets, layers, models, losses, regularizers
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Load mnist and reshape<jupyter_code>mnist = datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

x_train = x_train.reshape(len(x_train), 28, 28, 1)
x_test = x_test.reshape(len(x_test), 28, 28, 1)<jupyter_output><empty_output><jupyter_text>Reduce train data<jupyter_code>max_per_class = 1
counter = {}
min_x_train = []
min_y_train = []

for i in range(len(y_train)):
    if y_train[i] not in counter:
        counter[y_train[i]] = 0
    if counter[y_train[i]] == max_per_class:
        continue
    min_x_train.append(x_train[i])
    min_y_train.append(y_train[i])
    counter[y_train[i]] += 1

x_train = np.array(min_x_train)
y_train = np.array(min_y_train)<jupyter_output><empty_output><jupyter_text>Create model<jupyter_code>model = models.Sequential()
model.add(layers.Conv2D(32, kernel_size=(3, 3), strides=(1,1), activation='relu', input_shape=(28, 28, 1), kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.2))
model.add(layers.MaxPool2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.4))
model.add(layers.MaxPool2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.9))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu', kernel_regularizer=regularizers.l2(0.001)))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, kernel_regularizer=regularizers.l2(0.001)))

model.summary()

model.compile(optimizer='adam',
              loss=losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 26, 26, 32)        320       
_________________________________________________________________
dropout (Dropout)            (None, 26, 26, 32)        0         
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 13, 13, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        18496     
_________________________________________________________________
dropout_1 (Dropout)          (None, 11, 11, 64)        0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0         
________________________________________________________[...]<jupyter_text>Train<jupyter_code>history = model.fit(x_train, y_train, epochs=1000)<jupyter_output>Epoch 1/1000
1/1 [==============================] - 1s 1s/step - loss: 2.7902 - accuracy: 0.1000
Epoch 2/1000
1/1 [==============================] - 0s 16ms/step - loss: 2.9587 - accuracy: 0.1000
Epoch 3/1000
1/1 [==============================] - 0s 18ms/step - loss: 2.5397 - accuracy: 0.2000
Epoch 4/1000
1/1 [==============================] - 0s 17ms/step - loss: 2.4803 - accuracy: 0.0000e+00
Epoch 5/1000
1/1 [==============================] - 0s 18ms/step - loss: 2.6674 - accuracy: 0.1000
Epoch 6/1000
1/1 [==============================] - 0s 18ms/step - loss: 2.7811 - accuracy: 0.0000e+00
Epoch 7/1000
1/1 [==============================] - 0s 17ms/step - loss: 2.6330 - accuracy: 0.1000
Epoch 8/1000
1/1 [==============================] - 0s 17ms/step - loss: 2.5763 - accuracy: 0.0000e+00
Epoch 9/1000
1/1 [==============================] - 0s 16ms/step - loss: 2.4078 - accuracy: 0.2000
Epoch 10/1000
1/1 [==============================] - 0s 17ms/step - loss: 2.6472 - accuracy: 0.0000[...]<jupyter_text>View results and training progression<jupyter_code>train_loss, train_acc = model.evaluate(x_train,  y_train, verbose=2)
print('Train stats:', train_loss, train_acc)

test_loss, test_acc = model.evaluate(x_test,  y_test, verbose=2)
print('Test stats:', test_loss, test_acc)<jupyter_output>1/1 - 0s - loss: 0.1839 - accuracy: 1.0000
Train stats: 0.18391650915145874 1.0
313/313 - 3s - loss: 1.8052 - accuracy: 0.5993
Test stats: 1.8052103519439697 0.5993000268936157
{'fraction_non_alphanumeric': 0.20205779334500876, 'fraction_numerical': 0.09610332749562171, 'mean_word_length': 4.903100775193798, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '635289', 'license': 'MIT', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1526, 'n_words': 294, 'path': 'cnn/1_cnn_proc_60_test.ipynb', 'repo_name': 'bmaris98/mnist-cnn-vs-svm', 'size': '4568'}