banner
RustyNail

RustyNail

coder. 【blog】https://rustynail.me 【nostr】wss://ts.relays.world/ wss://relays.world/nostr

【调包侠的机器学习】Kaggle手写数字识别

# kaggle: https://www.kaggle.com/competitions/digit-recognizer/data
import tensorflow as tf
import numpy as np
from matplotlib.pyplot import plot
import pandas as pd
import matplotlib.pyplot as plt
train_data = pd.read_csv("../data/digit-recognizer/train.csv")
train_data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 42000 entries, 0 to 41999
Columns: 785 entries, label to pixel783
dtypes: int64(785)
memory usage: 251.5 MB
train_y = train_data['label']
train_X = train_data.drop(columns='label').values
train_X.shape
(42000, 784)
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)
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]))
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)
    ])
net = AlexNet()
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
net.compile(optimizer=opt, loss=tf.keras.losses.categorical_crossentropy, metrics=['accuracy'])
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
_________________________________________________________________
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

test_data = pd.read_csv("../data/digit-recognizer/test.csv")
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])
preY = net.predict(test_X_t)
preY.shape
(28000, 10)
preY_i = tf.argmax(preY, axis=1)
preY_i
<tf.Tensor: shape=(28000,), dtype=int64, numpy=array([2, 0, 9, ..., 3, 9, 2], dtype=int64)>
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()
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])
<Figure size 640x480 with 0 Axes>



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]
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.