{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": 55, "metadata": { "id": "SHsGymlK6Q5d" }, "outputs": [], "source": [ "!pip install torch safetensors huggingface_hub" ] }, { "cell_type": "code", "source": [ "import torch\n", "import torch.nn as nn\n", "import torch.optim as optim\n", "\n", "from safetensors.torch import save_file" ], "metadata": { "id": "-hrp9a078H5x" }, "execution_count": 56, "outputs": [] }, { "cell_type": "code", "source": [ "import random\n", "\n", "X = []\n", "Y = []\n", "\n", "for _ in range(5000):\n", "\n", " a = random.uniform(1, 10)\n", " b = random.uniform(1, 10)\n", "\n", " inputs = [a, b]\n", "\n", " outputs = [\n", " a + b,\n", " a - b,\n", " a * b,\n", " a / b\n", " ]\n", "\n", " X.append(inputs)\n", " Y.append(outputs)\n", "\n", "X = torch.tensor(X, dtype=torch.float32)\n", "Y = torch.tensor(Y, dtype=torch.float32)\n", "\n", "print(X.shape)\n", "print(Y.shape)" ], "metadata": { "id": "WYAJTDJI8Kqm" }, "execution_count": 57, "outputs": [] }, { "cell_type": "code", "source": [ "class MathModel(nn.Module):\n", "\n", " def __init__(self):\n", " super().__init__()\n", "\n", " self.net = nn.Sequential(\n", "\n", " nn.Linear(2, 32),\n", " nn.ReLU(),\n", "\n", " nn.Linear(32, 64),\n", " nn.ReLU(),\n", "\n", " nn.Linear(64, 32),\n", " nn.ReLU(),\n", "\n", " nn.Linear(32, 4)\n", " )\n", "\n", " def forward(self, x):\n", " return self.net(x)" ], "metadata": { "id": "a9T_97D_8NAc" }, "execution_count": 58, "outputs": [] }, { "cell_type": "code", "source": [ "model = MathModel()\n", "\n", "print(model)" ], "metadata": { "id": "1jxqfO6v8PGD" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "criterion = nn.MSELoss()\n", "\n", "optimizer = optim.Adam(\n", " model.parameters(),\n", " lr=0.001\n", ")" ], "metadata": { "id": "BhcNFeMZ8Qti" }, "execution_count": 60, "outputs": [] }, { "cell_type": "code", "source": [ "epochs = 500\n", "\n", "for epoch in range(epochs):\n", "\n", " predictions = model(X)\n", "\n", " loss = criterion(predictions, Y)\n", "\n", " optimizer.zero_grad()\n", "\n", " loss.backward()\n", "\n", " optimizer.step()\n", "\n", " if epoch % 50 == 0:\n", " print(f\"Epoch {epoch} Loss: {loss.item():.4f}\")" ], "metadata": { "id": "ZdNlDDp48lVE" }, "execution_count": 61, "outputs": [] }, { "cell_type": "code", "source": [ "test_input = torch.tensor([[6.0, 2.0]])\n", "\n", "prediction = model(test_input)\n", "\n", "print(prediction)" ], "metadata": { "id": "bwfPd2e_8nkb" }, "execution_count": 62, "outputs": [] }, { "cell_type": "code", "source": [ "a = 6\n", "b = 2\n", "\n", "test_input = torch.tensor([[a, b]], dtype=torch.float32)\n", "\n", "prediction = model(test_input).detach().numpy()[0]\n", "\n", "print(\"INPUT\")\n", "print(a, b)\n", "\n", "print(\"\\nPREDICTIONS\")\n", "\n", "print(\"ADD :\", prediction[0])\n", "print(\"SUB :\", prediction[1])\n", "print(\"MUL :\", prediction[2])\n", "print(\"DIV :\", prediction[3])" ], "metadata": { "id": "2yRAy_ek8peN" }, "execution_count": 63, "outputs": [] }, { "cell_type": "code", "source": [ "save_file(\n", " model.state_dict(),\n", " \"math_add_sub_mul_div_model.safetensors\"\n", ")\n", "\n", "print(\"Saved!\")" ], "metadata": { "id": "FcPuskx58rkF" }, "execution_count": 64, "outputs": [] }, { "cell_type": "code", "source": [ "import os\n", "\n", "print(os.listdir())" ], "metadata": { "id": "8vhzfzAe8tKW" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "from huggingface_hub import login\n", "\n", "login()" ], "metadata": { "id": "U5qADMmz8u65" }, "execution_count": 66, "outputs": [] }, { "cell_type": "code", "source": [ "from huggingface_hub import HfApi\n", "\n", "api = HfApi()\n", "\n", "api.upload_file(\n", " path_or_fileobj=\"math_add_sub_mul_div_model.safetensors\",\n", " path_in_repo=\"math_add_sub_mul_div_model.safetensors\",\n", " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n", " repo_type=\"model\",\n", ")\n", "\n", "print(\"Uploaded!\")" ], "metadata": { "id": "5o1XmEOX8ycJ" }, "execution_count": 67, "outputs": [] }, { "cell_type": "code", "source": [ "readme = \"\"\"\n", "# Basic Math AI\n", "\n", "Tiny neural network trained to perform:\n", "\n", "- Addition\n", "- Subtraction\n", "- Multiplication\n", "- Division\n", "\n", "Inputs:\n", "[a, b]\n", "\n", "Outputs:\n", "[a+b, a-b, a*b, a/b]\n", "\n", "Saved using SafeTensors.\n", "\"\"\"\n", "\n", "with open(\"README.md\", \"w\") as f:\n", " f.write(readme)" ], "metadata": { "id": "SjlZCtl69H8R" }, "execution_count": 68, "outputs": [] }, { "cell_type": "code", "source": [ "api.upload_file(\n", " path_or_fileobj=\"README.md\",\n", " path_in_repo=\"README.md\",\n", " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n", " repo_type=\"model\",\n", ")\n", "\n", "print(\"README uploaded!\")" ], "metadata": { "id": "N3ALvS4n9hKe" }, "execution_count": 69, "outputs": [] }, { "cell_type": "code", "source": [ "from huggingface_hub import hf_hub_download\n", "from safetensors.torch import load_file" ], "metadata": { "id": "r5gb1XAO-7bh" }, "execution_count": 70, "outputs": [] }, { "cell_type": "code", "source": [ "def load_math_model():\n", "\n", " model_path = hf_hub_download(\n", " repo_id=\"harishforaiandml/math-add-sub-mul-div-model\",\n", " filename=\"math_add_sub_mul_div_model.safetensors\"\n", " )\n", "\n", " state_dict = load_file(model_path)\n", "\n", " model = MathModel()\n", "\n", " model.load_state_dict(state_dict)\n", "\n", " model.eval()\n", "\n", " return model" ], "metadata": { "id": "JEnr8lUS-PVr" }, "execution_count": 71, "outputs": [] }, { "cell_type": "code", "source": [ "model = load_math_model()\n", "\n", "x = torch.tensor([[10.0, 5.0]])\n", "\n", "result = model(x)\n", "\n", "print(result)" ], "metadata": { "id": "lbVdBthn-RIv" }, "execution_count": null, "outputs": [] } ] }