{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30665,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import (\n    Conv2D, MaxPooling2D, Flatten, Dense, Dropout, BatchNormalization,\n    AveragePooling2D  # 如果需要使用AveragePooling2D\n)\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import LearningRateScheduler\n# GPU\nprint(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\nif tf.config.experimental.list_physical_devices('GPU'):\n    print('Using GPU')\nelse:\n    print('Using CPU')","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:33.500494Z","iopub.execute_input":"2024-03-05T12:07:33.501347Z","iopub.status.idle":"2024-03-05T12:07:33.508686Z","shell.execute_reply.started":"2024-03-05T12:07:33.501315Z","shell.execute_reply":"2024-03-05T12:07:33.507778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 数据预处理\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\ny_train = train_data[\"label\"].values\nX_train = train_data.drop(labels=[\"label\"], axis=1).values.reshape(-1, 28, 28, 1) / 255.0\nX_test = test_data.values.reshape(-1, 28, 28, 1) / 255.0\n\ny_train = to_categorical(y_train, num_classes=10)\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:33.510510Z","iopub.execute_input":"2024-03-05T12:07:33.510918Z","iopub.status.idle":"2024-03-05T12:07:38.382651Z","shell.execute_reply.started":"2024-03-05T12:07:33.510887Z","shell.execute_reply":"2024-03-05T12:07:38.381791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 数据增强\ndata_augmentation = tf.keras.Sequential([\n    tf.keras.layers.RandomRotation(0.1),\n    tf.keras.layers.RandomZoom(0.1),\n])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:38.383728Z","iopub.execute_input":"2024-03-05T12:07:38.383994Z","iopub.status.idle":"2024-03-05T12:07:38.393853Z","shell.execute_reply.started":"2024-03-05T12:07:38.383971Z","shell.execute_reply":"2024-03-05T12:07:38.392869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 构建模型\nmodel = Sequential([\n    data_augmentation,  # 数据增强层\n    Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=(28, 28, 1), padding='same'),\n    BatchNormalization(),  # 添加批归一化\n    MaxPooling2D(pool_size=(2, 2)),\n    Conv2D(64, (5, 5), activation='relu', padding='same'),\n    BatchNormalization(),  # 添加批归一化\n    MaxPooling2D(pool_size=(2, 2)),\n    Flatten(),\n    Dense(1024, activation='relu'),\n    Dropout(0.5),\n    Dense(10, activation='softmax')\n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:38.396285Z","iopub.execute_input":"2024-03-05T12:07:38.397027Z","iopub.status.idle":"2024-03-05T12:07:38.434574Z","shell.execute_reply.started":"2024-03-05T12:07:38.396994Z","shell.execute_reply":"2024-03-05T12:07:38.433754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 学习率调度器\ndef scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return lr * tf.math.exp(-0.1)\n\ncallback = LearningRateScheduler(scheduler)\n#model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.00094), loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.01, momentum=0.9),loss='categorical_crossentropy',metrics=['accuracy'])\n#model.compile(optimizer=tf.keras.optimizers.RMSprop(learning_rate=0.001),loss='categorical_crossentropy',metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:38.435563Z","iopub.execute_input":"2024-03-05T12:07:38.435813Z","iopub.status.idle":"2024-03-05T12:07:38.445698Z","shell.execute_reply.started":"2024-03-05T12:07:38.435790Z","shell.execute_reply":"2024-03-05T12:07:38.444703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 模型训练\nhistory = model.fit(X_train, y_train, epochs=20, batch_size=64, validation_data=(X_val, y_val), verbose=2)\n\n# 模型评估\nval_loss, val_acc = model.evaluate(X_val, y_val, verbose=2)\nprint(f'Validation Loss: {val_loss}, Validation Accuracy: {val_acc}')","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:07:38.446916Z","iopub.execute_input":"2024-03-05T12:07:38.447193Z","iopub.status.idle":"2024-03-05T12:08:55.051587Z","shell.execute_reply.started":"2024-03-05T12:07:38.447170Z","shell.execute_reply":"2024-03-05T12:08:55.050664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 生成预测并保存为Kaggle格式\npredictions = model.predict(X_test)\npredictions = np.argmax(predictions, axis=1)\n\nsubmission = pd.DataFrame({'ImageId': range(1, len(predictions)+1), 'Label': predictions})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-05T12:08:55.053375Z","iopub.execute_input":"2024-03-05T12:08:55.054203Z","iopub.status.idle":"2024-03-05T12:08:57.179062Z","shell.execute_reply.started":"2024-03-05T12:08:55.054168Z","shell.execute_reply":"2024-03-05T12:08:57.178109Z"},"trusted":true},"execution_count":null,"outputs":[]}]}