{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:16.107284Z","iopub.execute_input":"2025-06-27T01:58:16.107650Z","iopub.status.idle":"2025-06-27T01:58:16.124115Z","shell.execute_reply.started":"2025-06-27T01:58:16.107625Z","shell.execute_reply":"2025-06-27T01:58:16.123327Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:16.411982Z","iopub.execute_input":"2025-06-27T01:58:16.412236Z","iopub.status.idle":"2025-06-27T01:58:16.417225Z","shell.execute_reply.started":"2025-06-27T01:58:16.412219Z","shell.execute_reply":"2025-06-27T01:58:16.416453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:16.779801Z","iopub.execute_input":"2025-06-27T01:58:16.780089Z","iopub.status.idle":"2025-06-27T01:58:19.279189Z","shell.execute_reply.started":"2025-06-27T01:58:16.780066Z","shell.execute_reply":"2025-06-27T01:58:19.278172Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:19.280456Z","iopub.execute_input":"2025-06-27T01:58:19.280705Z","iopub.status.idle":"2025-06-27T01:58:19.294993Z","shell.execute_reply.started":"2025-06-27T01:58:19.280681Z","shell.execute_reply":"2025-06-27T01:58:19.294167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_arr = train_df.iloc[:, 1:].values.reshape((-1, 28, 28))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:20.360310Z","iopub.execute_input":"2025-06-27T01:58:20.360651Z","iopub.status.idle":"2025-06-27T01:58:20.366301Z","shell.execute_reply.started":"2025-06-27T01:58:20.360629Z","shell.execute_reply":"2025-06-27T01:58:20.365153Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_arr_2 = train_df.iloc[:, 1:].values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:23.308321Z","iopub.execute_input":"2025-06-27T01:58:23.308669Z","iopub.status.idle":"2025-06-27T01:58:23.313255Z","shell.execute_reply.started":"2025-06-27T01:58:23.308644Z","shell.execute_reply":"2025-06-27T01:58:23.312429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# train_arr[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:23.703603Z","iopub.execute_input":"2025-06-27T01:58:23.703885Z","iopub.status.idle":"2025-06-27T01:58:23.707567Z","shell.execute_reply.started":"2025-06-27T01:58:23.703864Z","shell.execute_reply":"2025-06-27T01:58:23.706677Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_arr[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:58:24.378952Z","iopub.execute_input":"2025-06-27T01:58:24.379220Z","iopub.status.idle":"2025-06-27T01:58:24.536233Z","shell.execute_reply.started":"2025-06-27T01:58:24.379199Z","shell.execute_reply":"2025-06-27T01:58:24.535464Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_arr[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:02:17.902061Z","iopub.execute_input":"2025-06-27T02:02:17.902482Z","iopub.status.idle":"2025-06-27T02:02:17.909645Z","shell.execute_reply.started":"2025-06-27T02:02:17.902461Z","shell.execute_reply":"2025-06-27T02:02:17.908820Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"filter_1 = np.zeros((7, 7))\nfilter_2 = np.zeros((7, 7))\nfilter_2[:, 3] = 1\nfilter_2[:, 2] = -1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:06:43.643188Z","iopub.execute_input":"2025-06-27T02:06:43.643523Z","iopub.status.idle":"2025-06-27T02:06:43.649255Z","shell.execute_reply.started":"2025-06-27T02:06:43.643500Z","shell.execute_reply":"2025-06-27T02:06:43.648307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(filter_2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:06:44.830704Z","iopub.execute_input":"2025-06-27T02:06:44.831427Z","iopub.status.idle":"2025-06-27T02:06:45.006053Z","shell.execute_reply.started":"2025-06-27T02:06:44.831379Z","shell.execute_reply":"2025-06-27T02:06:45.005186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from scipy.signal import convolve2d","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:00:17.477943Z","iopub.execute_input":"2025-06-27T02:00:17.478564Z","iopub.status.idle":"2025-06-27T02:00:18.197770Z","shell.execute_reply.started":"2025-06-27T02:00:17.478541Z","shell.execute_reply":"2025-06-27T02:00:18.196850Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"k = 1\nxx = convolve2d(train_arr[k], filter_2)\nplt.imshow(train_arr[k])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:07:01.332951Z","iopub.execute_input":"2025-06-27T02:07:01.333239Z","iopub.status.idle":"2025-06-27T02:07:01.520153Z","shell.execute_reply.started":"2025-06-27T02:07:01.333218Z","shell.execute_reply":"2025-06-27T02:07:01.519341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(xx)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T02:07:14.308890Z","iopub.execute_input":"2025-06-27T02:07:14.309171Z","iopub.status.idle":"2025-06-27T02:07:14.474608Z","shell.execute_reply.started":"2025-06-27T02:07:14.309150Z","shell.execute_reply":"2025-06-27T02:07:14.473654Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_arr[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T00:54:17.105953Z","iopub.execute_input":"2025-06-27T00:54:17.106623Z","iopub.status.idle":"2025-06-27T00:54:17.269363Z","shell.execute_reply.started":"2025-06-27T00:54:17.106587Z","shell.execute_reply":"2025-06-27T00:54:17.268273Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def softmax(x):\n    return np.exp(x) / np.exp(x).sum(axis=0, keepdims=True)\n\ndef sigmoid(x):\n    return 1 / (1 + np.exp(-x))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T00:54:17.677460Z","iopub.execute_input":"2025-06-27T00:54:17.677761Z","iopub.status.idle":"2025-06-27T00:54:17.682556Z","shell.execute_reply.started":"2025-06-27T00:54:17.677740Z","shell.execute_reply":"2025-06-27T00:54:17.681748Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"- 手でSGDする\n- パラメータが多すぎて辛いので減らす","metadata":{}},{"cell_type":"code","source":"train_arr_77 = np.zeros((42000, 7, 7))\nfor i in range(4):\n    for j in range(4):\n        train_arr_77 += train_arr[:, i::4, j::4] / 16","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T00:58:48.633703Z","iopub.execute_input":"2025-06-27T00:58:48.633996Z","iopub.status.idle":"2025-06-27T00:58:48.828385Z","shell.execute_reply.started":"2025-06-27T00:58:48.633973Z","shell.execute_reply":"2025-06-27T00:58:48.827696Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.imshow(train_arr_77[3])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T00:59:05.123486Z","iopub.execute_input":"2025-06-27T00:59:05.123798Z","iopub.status.idle":"2025-06-27T00:59:05.292937Z","shell.execute_reply.started":"2025-06-27T00:59:05.123776Z","shell.execute_reply":"2025-06-27T00:59:05.292132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y = np.eye(10)[train_df[\"label\"].values]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:06:22.727827Z","iopub.execute_input":"2025-06-27T01:06:22.728098Z","iopub.status.idle":"2025-06-27T01:06:22.737303Z","shell.execute_reply.started":"2025-06-27T01:06:22.728077Z","shell.execute_reply":"2025-06-27T01:06:22.736535Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"W1 = np.random.normal(size=(49, 16))\nb1 = np.random.normal(size=(1, 16))\nW2 = np.random.normal(size=(16, 16))\nb2 = np.random.normal(size=(1, 16))\nW3 = np.random.normal(size=(16, 10))\nb3 = np.random.normal(size=(1, 10))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:03:34.353141Z","iopub.execute_input":"2025-06-27T01:03:34.353465Z","iopub.status.idle":"2025-06-27T01:03:34.359143Z","shell.execute_reply.started":"2025-06-27T01:03:34.353439Z","shell.execute_reply":"2025-06-27T01:03:34.358192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x0 = (train_arr_77 / 256).reshape((-1, 49))\nx1 = sigmoid(np.matmul(x0, W1) + b1)\nx2 = sigmoid(np.matmul(x1, W2) + b2)\nx3 = softmax(np.matmul(x2, W3) + b3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:03:34.649367Z","iopub.execute_input":"2025-06-27T01:03:34.649690Z","iopub.status.idle":"2025-06-27T01:03:34.672986Z","shell.execute_reply.started":"2025-06-27T01:03:34.649670Z","shell.execute_reply":"2025-06-27T01:03:34.671979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def cross_entropy(y_true, y_pred):\n    delta = 1e-7\n    return - np.sum(y_true * np.log(y_pred + delta), axis=1).mean()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:05:30.383813Z","iopub.execute_input":"2025-06-27T01:05:30.384087Z","iopub.status.idle":"2025-06-27T01:05:30.388625Z","shell.execute_reply.started":"2025-06-27T01:05:30.384065Z","shell.execute_reply":"2025-06-27T01:05:30.387731Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"cross_entropy(y, x3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:26:17.278085Z","iopub.execute_input":"2025-06-27T01:26:17.278357Z","iopub.status.idle":"2025-06-27T01:26:17.288028Z","shell.execute_reply.started":"2025-06-27T01:26:17.278336Z","shell.execute_reply":"2025-06-27T01:26:17.287314Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# parameterをちょっと変える\nloss_base = cross_entropy(y, x3)\nd = 0.1\neps = 0.05\n# W1\nfor i in range(W1.shape[0]):\n    for j in range(W1.shape[1]):\n        W1[i, j] += d\n        x1 = sigmoid(np.matmul(x0, W1) + b1)\n        x2 = sigmoid(np.matmul(x1, W2) + b2)\n        x3 = softmax(np.matmul(x2, W3) + b3)\n        loss_new = cross_entropy(y, x3)\n        dif = (loss_new - loss_base) / d\n        W1[i, j] -= d\n        W1[i, j] -= dif * eps\n        \n# b1\nfor j in range(b1.shape[1]):\n    b1[0, j] += d\n    x1 = sigmoid(np.matmul(x0, W1) + b1)\n    x2 = sigmoid(np.matmul(x1, W2) + b2)\n    x3 = softmax(np.matmul(x2, W3) + b3)\n    loss_new = cross_entropy(y, x3)\n    dif = (loss_new - loss_base) / d\n    b1[0, j] -= d\n    b1[0, j] -= dif * eps\n\n# W2\nfor i in range(W2.shape[0]):\n    for j in range(W2.shape[1]):\n        W2[i, j] += d\n        # x1 = sigmoid(np.matmul(x0, W1) + b1)\n        x2 = sigmoid(np.matmul(x1, W2) + b2)\n        x3 = softmax(np.matmul(x2, W3) + b3)\n        loss_new = cross_entropy(y, x3)\n        dif = (loss_new - loss_base) / d\n        W2[i, j] -= d\n        W2[i, j] -= dif * eps\n        \n# b2\nfor j in range(b2.shape[1]):\n    b2[0, j] += d\n    # x1 = sigmoid(np.matmul(x0, W1) + b1)\n    x2 = sigmoid(np.matmul(x1, W2) + b2)\n    x3 = softmax(np.matmul(x2, W3) + b3)\n    loss_new = cross_entropy(y, x3)\n    dif = (loss_new - loss_base) / d\n    b2[0, j] -= d\n    b2[0, j] -= dif * eps\n\n# W3\nfor i in range(W3.shape[0]):\n    for j in range(W3.shape[1]):\n        W3[i, j] += d\n        # x1 = sigmoid(np.matmul(x0, W1) + b1)\n        # x2 = sigmoid(np.matmul(x1, W2) + b2)\n        x3 = softmax(np.matmul(x2, W3) + b3)\n        loss_new = cross_entropy(y, x3)\n        dif = (loss_new - loss_base) / d\n        W3[i, j] -= d\n        W3[i, j] -= dif * eps\n        \n# b3\nfor j in range(b3.shape[1]):\n    b3[0, j] += d\n    # x1 = sigmoid(np.matmul(x0, W1) + b1)\n    # x2 = sigmoid(np.matmul(x1, W2) + b2)\n    x3 = softmax(np.matmul(x2, W3) + b3)\n    loss_new = cross_entropy(y, x3)\n    dif = (loss_new - loss_base) / d\n    b3[0, j] -= d\n    b3[0, j] -= dif * eps","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:26:56.514642Z","iopub.execute_input":"2025-06-27T01:26:56.515123Z","iopub.status.idle":"2025-06-27T01:27:14.198171Z","shell.execute_reply.started":"2025-06-27T01:26:56.515097Z","shell.execute_reply":"2025-06-27T01:27:14.197299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"x1 = sigmoid(np.matmul(x0, W1) + b1)\nx2 = sigmoid(np.matmul(x1, W2) + b2)\nx3 = softmax(np.matmul(x2, W3) + b3)\ncross_entropy(y, x3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-27T01:27:14.199446Z","iopub.execute_input":"2025-06-27T01:27:14.199731Z","iopub.status.idle":"2025-06-27T01:27:14.223302Z","shell.execute_reply.started":"2025-06-27T01:27:14.199706Z","shell.execute_reply":"2025-06-27T01:27:14.222608Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}