{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30786,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import necessary libraries\nimport tensorflow as tf\nimport numpy as np\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nimport matplotlib.pyplot as plt\n\n# Load and preprocess the data\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\n\n# Separate features and labels\nX = data.iloc[:, 1:].values  # pixel values\nY = data.iloc[:, 0].values   # labels (0-9)\n\n# Normalize pixel values to be between 0 and 1\nX = X / 255.0\n\n# Convert labels to one-hot encoding\nY = to_categorical(Y)\n\n# Split into training and development sets\nX_train, X_dev, Y_train, Y_dev = train_test_split(X, Y, test_size=0.2, random_state=42)","metadata":{"_uuid":"98f419cb-375f-45e2-a21a-988d60defee0","_cell_guid":"590a969a-e425-4f82-8531-dede3bcfa998","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-15T13:46:48.441353Z","iopub.execute_input":"2024-10-15T13:46:48.441753Z","iopub.status.idle":"2024-10-15T13:47:07.505978Z","shell.execute_reply.started":"2024-10-15T13:46:48.441720Z","shell.execute_reply":"2024-10-15T13:47:07.505077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Custom initializer 1 (init_params equivalent)\ndef custom_initializer_1(shape, dtype=None):\n    return tf.random.uniform(shape, minval=-0.5, maxval=0.5, dtype=dtype)\n\ndef custom_initializer_2(shape, dtype=None):\n    if len(shape) == 1:\n        return tf.random.normal(shape, mean=0, stddev=0.1, dtype=dtype)\n    elif len(shape) == 2:\n        stddev = 4 / (shape[0] + shape[1])\n        return tf.random.normal(shape, mean=0, stddev=stddev, dtype=dtype)\n    else:\n        raise ValueError(f\"Expected shape with 1 or 2 dimensions, got {shape}\")\n\n\n\n# Build a shallow neural network with custom initializers\ndef build_model(custom_initializer):\n    model = tf.keras.models.Sequential()\n    \n    # Add the input layer explicitly\n    model.add(tf.keras.layers.Input(shape=(784,)))\n\n    # Add the input and hidden layers with the custom initializer\n    model.add(tf.keras.layers.Dense(units=10, activation='relu', \n                                    kernel_initializer=custom_initializer, \n                                    bias_initializer=custom_initializer))\n    \n    # Add the output layer with the custom initializer\n    model.add(tf.keras.layers.Dense(units=10, activation='softmax', \n                                    kernel_initializer=custom_initializer, \n                                    bias_initializer=custom_initializer))\n    \n    # Compile the model\n    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\n    \n    return model","metadata":{"_uuid":"a7095824-5833-478c-9f49-9fc81bfa1aca","_cell_guid":"d1fae3ae-1c13-4286-b310-20adb5a8084f","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-15T13:47:07.508148Z","iopub.execute_input":"2024-10-15T13:47:07.508554Z","iopub.status.idle":"2024-10-15T13:47:07.518513Z","shell.execute_reply.started":"2024-10-15T13:47:07.508517Z","shell.execute_reply":"2024-10-15T13:47:07.517135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train and evaluate with both custom initializations\ninitializers = {'Initializer 1 (Uniform)': custom_initializer_1, \n                'Initializer 2 (Normal)': custom_initializer_2}\n\nfor name, initializer in initializers.items():\n    print(f\"Training model with {name}\")\n    \n    # Build model with the current initializer\n    model = build_model(initializer)\n    \n    # Train the model\n    history = model.fit(X_train, Y_train, epochs=50, validation_data=(X_dev, Y_dev), batch_size=32)\n    \n    # Plot accuracy\n    plt.plot(history.history['accuracy'], label='Train Accuracy')\n    plt.plot(history.history['val_accuracy'], label='Validation Accuracy')\n    plt.title(f'Model Accuracy - {name}')\n    plt.xlabel('Epoch')\n    plt.ylabel('Accuracy')\n    plt.legend(loc=\"best\")\n    plt.show()\n    \n    # Plot loss\n    plt.plot(history.history['loss'], label='Train Loss')\n    plt.plot(history.history['val_loss'], label='Validation Loss')\n    plt.title(f'Model Loss - {name}')\n    plt.xlabel('Epoch')\n    plt.ylabel('Loss')\n    plt.legend(loc=\"best\")\n    plt.show()\n\n    # Evaluate the model on the development set\n    dev_loss, dev_accuracy = model.evaluate(X_dev, Y_dev)\n    print(f\"Accuracy on development set with {name}: {dev_accuracy * 100:.2f}%\")","metadata":{"_uuid":"6fde7e46-8712-44dd-8313-fd929df903df","_cell_guid":"8f8da808-790c-4d55-b8fb-c8563e37c192","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-15T13:47:07.520185Z","iopub.execute_input":"2024-10-15T13:47:07.520591Z","iopub.status.idle":"2024-10-15T13:49:50.737862Z","shell.execute_reply.started":"2024-10-15T13:47:07.520555Z","shell.execute_reply":"2024-10-15T13:49:50.736903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    # Evaluate the model on the development set\n    dev_loss, dev_accuracy = model.evaluate(X_dev, Y_dev)\n    print(f\"Accuracy on development set: {dev_accuracy * 100:.2f}%\")\n\n    # Predict a sample and visualize\n    index = 5  # Pick a sample index\n    current_image = X_dev[index].reshape(28, 28)  # Reshape from 784 to 28x28\n\n    # Display the image\n    plt.imshow(current_image, cmap='gray')\n    plt.show()\n\n    # Make a prediction\n    prediction = np.argmax(model.predict(X_dev[index].reshape(1, 784)))\n    true_label = np.argmax(Y_dev[index])\n\n    print(f\"Prediction: {prediction}, True Label: {true_label}\")","metadata":{"_uuid":"bc0fe76c-10eb-46c5-9e79-c3690f4abf69","_cell_guid":"4edb93dc-d8ad-434b-965d-b338262589ee","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2024-10-15T13:49:50.740151Z","iopub.execute_input":"2024-10-15T13:49:50.740536Z","iopub.status.idle":"2024-10-15T13:49:51.410284Z","shell.execute_reply.started":"2024-10-15T13:49:50.740499Z","shell.execute_reply":"2024-10-15T13:49:51.409276Z"},"trusted":true},"execution_count":null,"outputs":[]}]}