```python # kaggle: https://www.kaggle.com/competitions/digit-recognizer/data ``` ```python import tensorflow as tf import numpy as np from matplotlib.pyplot import plot import pandas as pd import matplotlib.pyplot as plt ``` ```python train_data = pd.read_csv("../data/digit-recognizer/train.csv") train_data.info() ``` RangeIndex: 42000 entries, 0 to 41999 Columns: 785 entries, label to pixel783 dtypes: int64(785) memory usage: 251.5 MB ```python train_y = train_data['label'] train_X = train_data.drop(columns='label').values train_X.shape ``` (42000, 784) ```python train_X_t = tf.convert_to_tensor(train_X, dtype=tf.float32) print("pre shape:", train_X_t.shape) train_X_t = tf.reshape(train_X_t, [-1, 28, 28, 1]) print("suf shape:", train_X_t.shape) # print(train_X_t[:1]) ``` pre shape: (42000, 784) suf shape: (42000, 28, 28, 1) ```python train_y_t = tf.one_hot(train_y, depth=10, axis=1) train_X_t.shape, train_y_t.shape ``` (TensorShape([42000, 28, 28, 1]), TensorShape([42000, 10])) ```python def AlexNet(): return tf.keras.models.Sequential([ # tf.keras.layers.Conv2D(filters=4, kernel_size=7, strides=1, # activation=tf.keras.activations.relu), # tf.keras.layers.MaxPool2D(pool_size=2, strides=1), # tf.keras.layers.Dropout(0.01), tf.keras.layers.Conv2D(filters=8, kernel_size=5, strides=1, activation=tf.keras.activations.relu), tf.keras.layers.MaxPool2D(pool_size=2, strides=2), tf.keras.layers.Dropout(0.1), tf.keras.layers.Conv2D(filters=16, kernel_size=3, padding='same', activation=tf.keras.activations.relu), tf.keras.layers.MaxPool2D(pool_size=2, strides=2), tf.keras.layers.Dropout(0.1), tf.keras.layers.Flatten(), tf.keras.layers.Dense(64, activation=tf.keras.activations.sigmoid), tf.keras.layers.Dense(10, activation=tf.keras.activations.softmax) ]) ``` ```python net = AlexNet() opt = tf.keras.optimizers.Adam(learning_rate=0.001) net.compile(optimizer=opt, loss=tf.keras.losses.categorical_crossentropy, metrics=['accuracy']) ``` ```python history = net.fit(train_X_t, train_y_t, batch_size=200, epochs=10, validation_split=0.2, callbacks=[], shuffle= True) net.summary() ``` Epoch 1/10 168/168 [==============================] - 1s 5ms/step - loss: 1.0596 - accuracy: 0.7315 - val_loss: 0.3905 - val_accuracy: 0.9190 Epoch 2/10 168/168 [==============================] - 1s 3ms/step - loss: 0.3291 - accuracy: 0.9253 - val_loss: 0.1932 - val_accuracy: 0.9529 Epoch 3/10 168/168 [==============================] - 1s 3ms/step - loss: 0.2030 - accuracy: 0.9475 - val_loss: 0.1457 - val_accuracy: 0.9605 Epoch 4/10 168/168 [==============================] - 1s 3ms/step - loss: 0.1603 - accuracy: 0.9567 - val_loss: 0.1173 - val_accuracy: 0.9667 Epoch 5/10 168/168 [==============================] - 1s 3ms/step - loss: 0.1356 - accuracy: 0.9618 - val_loss: 0.1053 - val_accuracy: 0.9687 Epoch 6/10 168/168 [==============================] - 1s 3ms/step - loss: 0.1223 - accuracy: 0.9650 - val_loss: 0.0924 - val_accuracy: 0.9737 Epoch 7/10 168/168 [==============================] - 1s 3ms/step - loss: 0.1079 - accuracy: 0.9689 - val_loss: 0.0898 - val_accuracy: 0.9717 Epoch 8/10 168/168 [==============================] - 1s 3ms/step - loss: 0.0997 - accuracy: 0.9713 - val_loss: 0.0781 - val_accuracy: 0.9767 Epoch 9/10 168/168 [==============================] - 1s 3ms/step - loss: 0.0918 - accuracy: 0.9732 - val_loss: 0.0750 - val_accuracy: 0.9786 Epoch 10/10 168/168 [==============================] - 1s 3ms/step - loss: 0.0851 - accuracy: 0.9751 - val_loss: 0.0774 - val_accuracy: 0.9761 Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_13 (Conv2D) (200, 24, 24, 8) 208 max_pooling2d_13 (MaxPoolin (200, 12, 12, 8) 0 g2D) dropout_13 (Dropout) (200, 12, 12, 8) 0 conv2d_14 (Conv2D) (200, 12, 12, 16) 1168 max_pooling2d_14 (MaxPoolin (200, 6, 6, 16) 0 g2D) dropout_14 (Dropout) (200, 6, 6, 16) 0 flatten_5 (Flatten) (200, 576) 0 dense_6 (Dense) (200, 64) 36928 dense_7 (Dense) (200, 10) 650 ================================================================= Total params: 38,954 Trainable params: 38,954 Non-trainable params: 0 _________________________________________________________________ ```python import matplotlib.pyplot as plt print(history.history.keys()) plt.plot(history.history["loss"], label="Training Loss") # plt.plot(history.history["accuracy"], label="accuracy") plt.plot(history.history["val_loss"], label="val_loss") plt.legend() plt.show() ``` dict_keys(['loss', 'accuracy', 'val_loss', 'val_accuracy']) ![png](https://oss.rustynail.me/someone/api/files/get?filePath=files/output_9_1.png) ```python test_data = pd.read_csv("../data/digit-recognizer/test.csv") ``` ```python test_X_t = tf.convert_to_tensor(test_data.values, dtype=tf.float32) test_X_t = tf.reshape(test_X_t, [-1, 28, 28, 1]) test_X_t.shape ``` TensorShape([28000, 28, 28, 1]) ```python preY = net.predict(test_X_t) preY.shape ``` (28000, 10) ```python preY_i = tf.argmax(preY, axis=1) preY_i ``` ```python with open("../data/digit-recognizer/test_submission.csv", "w") as f: f.write("ImageId,Label\n") for i, n in zip(range(preY_i.shape[0]), preY_i.numpy()): f.write("%d,%d\n"%(i+1, n)) f.close() ``` ```python f = plt.figure() #subplot(r,c) provide the no. of rows and columns f, axarr = plt.subplots(5,6) for i in range(5): for j in range(6): f.add_subplot(5, 6, i * 6 + j + 1) plt.imshow(test_data.values[i * 6 + j].reshape(28, 28)) plt.show(block=True) print(preY_i.numpy()[:30]) ```
![png](https://oss.rustynail.me/someone/api/files/get?filePath=files/output_15_1.png) [2 0 9 9 3 7 0 3 0 3 5 7 4 0 4 3 3 1 9 0 9 1 1 5 7 4 2 7 4 7] ```python ```