{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30776,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-02T13:58:00.343611Z","iopub.execute_input":"2024-10-02T13:58:00.343896Z","iopub.status.idle":"2024-10-02T13:58:01.279724Z","shell.execute_reply.started":"2024-10-02T13:58:00.343860Z","shell.execute_reply":"2024-10-02T13:58:01.278817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-10-02T14:38:02.729154Z","iopub.execute_input":"2024-10-02T14:38:02.730206Z","iopub.status.idle":"2024-10-02T14:38:02.734662Z","shell.execute_reply.started":"2024-10-02T14:38:02.730162Z","shell.execute_reply":"2024-10-02T14:38:02.733489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ndata = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndata = np.array(data)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:12.766295Z","iopub.execute_input":"2024-10-02T13:58:12.766845Z","iopub.status.idle":"2024-10-02T13:58:16.274813Z","shell.execute_reply.started":"2024-10-02T13:58:12.766811Z","shell.execute_reply":"2024-10-02T13:58:16.273806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Shuffle and split data\nnp.random.shuffle(data)\ndata_val = data[:1000].T\nY_val = data_val[0]\nX_val = data_val[1:] / 255.0  # Normalizing validation data","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:16.277643Z","iopub.execute_input":"2024-10-02T13:58:16.278046Z","iopub.status.idle":"2024-10-02T13:58:16.799063Z","shell.execute_reply.started":"2024-10-02T13:58:16.278001Z","shell.execute_reply":"2024-10-02T13:58:16.798144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_train = data[1000:].T\nY_train = data_train[0]\nX_train = data_train[1:] / 255.0  # Normalizing training data","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:16.800447Z","iopub.execute_input":"2024-10-02T13:58:16.800837Z","iopub.status.idle":"2024-10-02T13:58:16.914869Z","shell.execute_reply.started":"2024-10-02T13:58:16.800798Z","shell.execute_reply":"2024-10-02T13:58:16.913552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# One-hot encode labels\nY_train = np.eye(10)[Y_train.astype(int)]\nY_val = np.eye(10)[Y_val.astype(int)]","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:16.916262Z","iopub.execute_input":"2024-10-02T13:58:16.916779Z","iopub.status.idle":"2024-10-02T13:58:16.925226Z","shell.execute_reply.started":"2024-10-02T13:58:16.916715Z","shell.execute_reply":"2024-10-02T13:58:16.924359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set model parameters\nn_input = 784  # 28x28 input images\nn_hidden = 10  # Hidden layer neurons\nn_output = 10  # Output layer neurons\nlearning_rate = 0.001\nepochs = 10\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:16.926847Z","iopub.execute_input":"2024-10-02T13:58:16.927261Z","iopub.status.idle":"2024-10-02T13:58:17.012246Z","shell.execute_reply.started":"2024-10-02T13:58:16.927209Z","shell.execute_reply":"2024-10-02T13:58:17.011172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define weights and biases\nweights = {\n    'hidden': tf.Variable(tf.random.normal([n_input, n_hidden])),\n    'output': tf.Variable(tf.random.normal([n_hidden, n_output]))\n}\nbiases = {\n    'hidden': tf.Variable(tf.random.normal([n_hidden])),\n    'output': tf.Variable(tf.random.normal([n_output]))\n}","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:17.013659Z","iopub.execute_input":"2024-10-02T13:58:17.014033Z","iopub.status.idle":"2024-10-02T13:58:17.780645Z","shell.execute_reply.started":"2024-10-02T13:58:17.013995Z","shell.execute_reply":"2024-10-02T13:58:17.779396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the neural network\ndef neural_network(X):\n    hidden_layer = tf.add(tf.matmul(X, weights['hidden']), biases['hidden'])\n    hidden_layer = tf.nn.relu(hidden_layer)\n    output_layer = tf.add(tf.matmul(hidden_layer, weights['output']), biases['output'])\n    return output_layer","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:17.782368Z","iopub.execute_input":"2024-10-02T13:58:17.782918Z","iopub.status.idle":"2024-10-02T13:58:17.790101Z","shell.execute_reply.started":"2024-10-02T13:58:17.782854Z","shell.execute_reply":"2024-10-02T13:58:17.788943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the loss and optimizer\nX_train_tf = tf.convert_to_tensor(X_train.T, dtype=tf.float32)\nY_train_tf = tf.convert_to_tensor(Y_train, dtype=tf.float32)\nX_val_tf = tf.convert_to_tensor(X_val.T, dtype=tf.float32)\nY_val_tf = tf.convert_to_tensor(Y_val, dtype=tf.float32)\n\noptimizer = tf.optimizers.Adam(learning_rate)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:17.793641Z","iopub.execute_input":"2024-10-02T13:58:17.794009Z","iopub.status.idle":"2024-10-02T13:58:18.147321Z","shell.execute_reply.started":"2024-10-02T13:58:17.793972Z","shell.execute_reply":"2024-10-02T13:58:18.146520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the training step\ndef train_step(X, Y):\n    with tf.GradientTape() as tape:\n        logits = neural_network(X)\n        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))\n    gradients = tape.gradient(loss, [weights['hidden'], weights['output'], biases['hidden'], biases['output']])\n    optimizer.apply_gradients(zip(gradients, [weights['hidden'], weights['output'], biases['hidden'], biases['output']]))\n    return loss","metadata":{"execution":{"iopub.status.busy":"2024-10-02T13:58:18.148416Z","iopub.execute_input":"2024-10-02T13:58:18.148712Z","iopub.status.idle":"2024-10-02T13:58:18.154645Z","shell.execute_reply.started":"2024-10-02T13:58:18.148680Z","shell.execute_reply":"2024-10-02T13:58:18.153793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Store loss and accuracy for each epoch\nlosses = []\naccuracies = []\n\n# Modify the training loop to record loss and accuracy\nfor epoch in range(epochs):\n    epoch_loss = 0\n    for i in range(0, X_train.shape[1], batch_size):\n        X_batch = X_train_tf[i:i + batch_size]\n        Y_batch = Y_train_tf[i:i + batch_size]\n        loss = train_step(X_batch, Y_batch)\n        epoch_loss += loss.numpy()\n    \n    # Calculate validation accuracy\n    logits_val = neural_network(X_val_tf)\n    correct_prediction = tf.equal(tf.argmax(logits_val, 1), tf.argmax(Y_val_tf, 1))\n    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)).numpy()\n\n    losses.append(epoch_loss / (X_train.shape[1] // batch_size))\n    accuracies.append(accuracy)\n    print(f\"Epoch {epoch + 1}, Loss: {losses[-1]:.4f}, Validation Accuracy: {accuracy:.4f}\")\n\n# Plotting the loss and accuracy\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(losses, label='Loss')\nplt.title('Training Loss')\nplt.xlabel('Epochs')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.subplot(1, 2, 2)\nplt.plot(accuracies, label='Validation Accuracy', color='orange')\nplt.title('Validation Accuracy')\nplt.xlabel('Epochs')\nplt.ylabel('Accuracy')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-10-02T14:38:13.940282Z","iopub.execute_input":"2024-10-02T14:38:13.940719Z","iopub.status.idle":"2024-10-02T14:42:56.739561Z","shell.execute_reply.started":"2024-10-02T14:38:13.940681Z","shell.execute_reply":"2024-10-02T14:42:56.738547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the test data\ntest_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\ntest_data = np.array(test_data)\n\n# Preprocess the test data\nX_test = test_data / 255.0\nX_test_tf = tf.convert_to_tensor(X_test, dtype=tf.float32)","metadata":{"execution":{"iopub.status.busy":"2024-10-02T14:03:03.168952Z","iopub.execute_input":"2024-10-02T14:03:03.169257Z","iopub.status.idle":"2024-10-02T14:03:05.616883Z","shell.execute_reply.started":"2024-10-02T14:03:03.169222Z","shell.execute_reply":"2024-10-02T14:03:05.615790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test data\nlogits_test = neural_network(X_test_tf)\ntest_predictions = tf.argmax(logits_test, axis=1).numpy()\n\n# Save the predictions to a CSV file\nsubmission = pd.DataFrame({\n    \"ImageId\": np.arange(1, len(test_predictions) + 1),\n    \"Label\": test_predictions\n})\nsubmission.to_csv(\"submission.csv\", index=False)\nprint(\"Predictions saved to submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-10-02T14:03:05.618236Z","iopub.execute_input":"2024-10-02T14:03:05.618678Z","iopub.status.idle":"2024-10-02T14:03:05.662398Z","shell.execute_reply.started":"2024-10-02T14:03:05.618623Z","shell.execute_reply":"2024-10-02T14:03:05.661509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}