{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":108813,"databundleVersionId":13355142,"sourceType":"competition"}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport numpy as np\nfrom keras.datasets import fashion_mnist\n\n# -----------------\n# 1. Load & Preprocess Data\n# -----------------\n(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()\n\n# Normalize\nx_train = x_train.reshape(x_train.shape[0], -1) / 255.0\nx_test = x_test.reshape(x_test.shape[0], -1) / 255.0\n\n# One-hot encode labels\ndef one_hot(y, num_classes=10):\n    return np.eye(num_classes)[y]\n\ny_train_oh = one_hot(y_train)\ny_test_oh = one_hot(y_test)\n\n# -----------------\n# 2. Initialize Parameters\n# -----------------\ninput_dim = 784\nhidden_dim = 128\noutput_dim = 10\n\nnp.random.seed(42)\nW1 = np.random.randn(input_dim, hidden_dim) * np.sqrt(2.0/input_dim)\nb1 = np.zeros((1, hidden_dim))\nW2 = np.random.randn(hidden_dim, output_dim) * np.sqrt(2.0/hidden_dim)\nb2 = np.zeros((1, output_dim))\n\n# -----------------\n# 3. Activations & Helpers\n# -----------------\ndef relu(z):\n    return np.maximum(0, z)\n\ndef relu_deriv(z):\n    return (z > 0).astype(float)\n\ndef softmax(z):\n    exp_z = np.exp(z - np.max(z, axis=1, keepdims=True))\n    return exp_z / np.sum(exp_z, axis=1, keepdims=True)\n\ndef cross_entropy(y_true, y_pred):\n    m = y_true.shape[0]\n    return -np.sum(y_true * np.log(y_pred + 1e-8)) / m\n\n# -----------------\n# 4. Training Loop\n# -----------------\nlearning_rate = 0.01\nbatch_size = 64\nepochs = 20\n\nfor epoch in range(epochs):\n    # Shuffle training data\n    indices = np.arange(x_train.shape[0])\n    np.random.shuffle(indices)\n    x_train = x_train[indices]\n    y_train_oh = y_train_oh[indices]\n\n    for i in range(0, x_train.shape[0], batch_size):\n        X_batch = x_train[i:i+batch_size]\n        Y_batch = y_train_oh[i:i+batch_size]\n\n        # Forward pass\n        Z1 = X_batch.dot(W1) + b1\n        A1 = relu(Z1)\n        Z2 = A1.dot(W2) + b2\n        A2 = softmax(Z2)\n\n        # Loss\n        loss = cross_entropy(Y_batch, A2)\n\n        # Backward pass\n        m = X_batch.shape[0]\n        dZ2 = (A2 - Y_batch) / m\n        dW2 = A1.T.dot(dZ2)\n        db2 = np.sum(dZ2, axis=0, keepdims=True)\n\n        dA1 = dZ2.dot(W2.T)\n        dZ1 = dA1 * relu_deriv(Z1)\n        dW1 = X_batch.T.dot(dZ1)\n        db1 = np.sum(dZ1, axis=0, keepdims=True)\n\n        # Update parameters\n        W1 -= learning_rate * dW1\n        b1 -= learning_rate * db1\n        W2 -= learning_rate * dW2\n        b2 -= learning_rate * db2\n\n    # Accuracy on training set per epoch\n    Z1 = x_train.dot(W1) + b1\n    A1 = relu(Z1)\n    Z2 = A1.dot(W2) + b2\n    preds = np.argmax(softmax(Z2), axis=1)\n    acc = np.mean(preds == y_train)\n    print(f\"Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}, Train Acc: {acc:.4f}\")\n\n# -----------------\n# 5. Evaluate on Test Set\n# -----------------\nZ1 = x_test.dot(W1) + b1\nA1 = relu(Z1)\nZ2 = A1.dot(W2) + b2\npreds_test = np.argmax(softmax(Z2), axis=1)\nacc_test = np.mean(preds_test == y_test)\nprint(\"Final Test Accuracy:\", acc_test)\n\n# -----------------\n# 6. Prepare Submission File\n# -----------------\nimport pandas as pd\nsubmission = pd.DataFrame({\"id\": np.arange(len(preds_test)), \"target_feature\": preds_test})\nsubmission.to_csv(\"submission.csv\", index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-19T09:57:34.388769Z","iopub.execute_input":"2025-08-19T09:57:34.389010Z","iopub.status.idle":"2025-08-19T09:58:23.231224Z","shell.execute_reply.started":"2025-08-19T09:57:34.388988Z","shell.execute_reply":"2025-08-19T09:58:23.230274Z"}},"outputs":[],"execution_count":null}]}