Created
          November 8, 2021 08:01 
        
      - 
      
 - 
        
Save amoux/f3c569dc469713a75f2a46ad3c0eb648 to your computer and use it in GitHub Desktop.  
    Logistic Regression - Ego.ipynb
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | { | |
| "nbformat": 4, | |
| "nbformat_minor": 5, | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.9.7" | |
| }, | |
| "colab": { | |
| "name": "Logistic Regression - Ego.ipynb", | |
| "provenance": [], | |
| "collapsed_sections": [], | |
| "include_colab_link": true | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/amoux/f3c569dc469713a75f2a46ad3c0eb648/logistic-regression-ego.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "27c3a3f7" | |
| }, | |
| "source": [ | |
| "import ego\n", | |
| "import ego.nn as nn\n", | |
| "import ego.nn.functional as F\n", | |
| "from random import shuffle, randint" | |
| ], | |
| "id": "27c3a3f7", | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "e93b8958" | |
| }, | |
| "source": [ | |
| "def sigmoid(x):\n", | |
| " return 0.5 * (F.tanh(x) + 1)\n", | |
| "\n", | |
| "def logistic_model(x, *params):\n", | |
| " return sigmoid(F.linear(x, *params))\n", | |
| "\n", | |
| "def loss_fn(h, y):\n", | |
| " p = h*y + (1 - h) * (1 - y)\n", | |
| " return -ego.sum(ego.log(p))" | |
| ], | |
| "id": "e93b8958", | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "df994dde", | |
| "outputId": "8c360faf-1654-4fd8-fc6d-13959065e47c" | |
| }, | |
| "source": [ | |
| "inputs = ego.tensor([[0.52, 1.12, 0.77],\n", | |
| " [0.88, -1.08, 0.15],\n", | |
| " [0.52, 0.06, -1.30],\n", | |
| " [0.74, -2.49, 1.39]])\n", | |
| "targets = ego.tensor([1, 1, 0, 1])\n", | |
| "\n", | |
| "# initialize the parameters\n", | |
| "use_bias = True\n", | |
| "params = [\n", | |
| " nn.Parameter(ego.tensor([[0., 0, 0]])),\n", | |
| " nn.Parameter(ego.tensor([0.])) if use_bias else None]\n", | |
| "print(params)" | |
| ], | |
| "id": "df994dde", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "[Parameter containing:\n", | |
| "tensor([[0., 0., 0.]], requires_grad=True), Parameter containing:\n", | |
| "tensor([0.0000], requires_grad=True)]\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "scrolled": false, | |
| "id": "1abdf571", | |
| "outputId": "b4df8ca6-3018-4446-9816-264a44702326" | |
| }, | |
| "source": [ | |
| "lr = 0.1\n", | |
| "rate = 3\n", | |
| "steps = 30\n", | |
| "# setup a fake \"dataloader\"\n", | |
| "dataloader = list(zip(inputs.clone(), targets.clone()))\n", | |
| "for step in range(1, steps + 1):\n", | |
| " losses = []\n", | |
| " shuffle(dataloader) # shuffle at every step\n", | |
| " for batch in dataloader:\n", | |
| " x, y = batch\n", | |
| " yhat = logistic_model(x.view(1, -1), *params)\n", | |
| " loss = loss_fn(yhat, y)\n", | |
| " loss.backward()\n", | |
| " with ego.no_grad():\n", | |
| " for param in params:\n", | |
| " if param is None: # handle when bias is None\n", | |
| " continue\n", | |
| " param -= lr * param.grad\n", | |
| " param.grad.zero_() # zero the gradients\n", | |
| " losses.append(loss.detach())\n", | |
| " if step % rate == 1:\n", | |
| " cost = ego.mean(ego.stack(losses))\n", | |
| " print(f't={step} z={cost.item():.5f}')" | |
| ], | |
| "id": "1abdf571", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "t=1 z=0.63303\n", | |
| "t=4 z=0.19865\n", | |
| "t=7 z=0.12029\n", | |
| "t=10 z=0.08629\n", | |
| "t=13 z=0.06727\n", | |
| "t=16 z=0.05512\n", | |
| "t=19 z=0.04669\n", | |
| "t=22 z=0.04049\n", | |
| "t=25 z=0.03574\n", | |
| "t=28 z=0.03199\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "2413585b", | |
| "outputId": "7c563f24-1b30-4023-a938-607b7bb36dcb" | |
| }, | |
| "source": [ | |
| "params" | |
| ], | |
| "id": "2413585b", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[Parameter containing:\n", | |
| " tensor([[ 0.5230, -0.3229, 1.7643]], requires_grad=True),\n", | |
| " Parameter containing:\n", | |
| " tensor([0.5188], requires_grad=True)]" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "a70f0612", | |
| "outputId": "c3ea23dd-f8cc-4242-defb-ec7e1380415e" | |
| }, | |
| "source": [ | |
| "with ego.no_grad():\n", | |
| " probs = logistic_model(inputs, *params)\n", | |
| "probs = probs.flatten()\n", | |
| "preds = ego.zeros(probs.size(),\n", | |
| " dtype=ego.long).masked_fill_(probs > 0.5, 1)\n", | |
| "acc = 100 * ((targets == preds).sum() / targets.numel()).item()\n", | |
| "print(f'scores : {probs}')\n", | |
| "print(f'predictions : {preds}')\n", | |
| "print(f'accuracy : {acc} %')" | |
| ], | |
| "id": "a70f0612", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "scores : tensor([0.9728, 0.9603, 0.0455, 0.9998])\n", | |
| "predictions : tensor([1, 1, 0, 1])\n", | |
| "accuracy : 100.0 %\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "021e81de", | |
| "outputId": "47b70410-c08c-41bc-c27a-bd883957a2a1" | |
| }, | |
| "source": [ | |
| "with ego.enable_profile():\n", | |
| " print('\\t----- Forward ----\\n')\n", | |
| " track_loss = loss_fn(logistic_model(inputs, *params), targets)\n", | |
| " print('\\t----- Backward ----\\n')\n", | |
| " track_loss.backward()" | |
| ], | |
| "id": "021e81de", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "\t----- Forward ----\n", | |
| "\n", | |
| "\u001b[1m\u001b[36m Transpose (in:[1], out:[1]) : 0.02 ms [(1, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(1,)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(4, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(3, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Addmm (in:[3], out:[1]) : 0.91 ms [(1,), (4, 3), (3, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Tanh (in:[1], out:[1]) : 0.01 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Add (in:[2], out:[1]) : 0.01 ms [(4, 1), ('tensor',)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Mul (in:[2], out:[1]) : 0.01 ms [('tensor',), (4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Mul (in:[2], out:[1]) : 0.01 ms [(4, 1), (4,)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Sub (in:[2], out:[1]) : 0.01 ms [('tensor',), (4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Sub (in:[2], out:[1]) : 0.01 ms [('tensor',), (4,)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Mul (in:[2], out:[1]) : 0.01 ms [(4, 1), (4,)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Add (in:[2], out:[1]) : 0.01 ms [(4, 4), (4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Log (in:[1], out:[1]) : 0.03 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Sum (in:[1], out:[1]) : 0.01 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Neg (in:[1], out:[1]) : 0.01 ms [('tensor',)]\u001b[0m\n", | |
| "\t----- Backward ----\n", | |
| "\n", | |
| "\u001b[1m\u001b[35m <NegBackward> (in:[1], out:[1]) : 0.01 ms [('ndarray',)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <SumBackward> (in:[1], out:[1]) : 0.04 ms [('ndarray',)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <LogBackward> (in:[1], out:[1]) : 0.01 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <AddBackward> (in:[1], out:[2]) : 0.01 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <MulBackward> (in:[1], out:[2]) : 0.04 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <SubBackward> (in:[1], out:[2]) : 0.02 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <MulBackward> (in:[1], out:[2]) : 0.04 ms [(4, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <MulBackward> (in:[1], out:[2]) : 0.03 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <AddBackward> (in:[1], out:[2]) : 0.02 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <TanhBackward> (in:[1], out:[1]) : 0.02 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Sum (in:[1], out:[1]) : 0.01 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Transpose (in:[1], out:[1]) : 0.01 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(1, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(1, 4)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m NoGrad (in:[1], out:[1]) : 0.00 ms [(4, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Mm (in:[3], out:[1]) : 0.46 ms [(1, 3), (1, 4), (4, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[36m Transpose (in:[1], out:[1]) : 0.01 ms [(1, 3)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <AddmmBackward> (in:[1], out:[3]) : 0.90 ms [(4, 1)]\u001b[0m\n", | |
| "\u001b[1m\u001b[35m <TransposeBackward> (in:[1], out:[1]) : 0.01 ms [(3, 1)]\u001b[0m\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "c0701897", | |
| "outputId": "925fbf28-3b48-4306-83ec-0e7192877355" | |
| }, | |
| "source": [ | |
| "params[0].grad" | |
| ], | |
| "id": "c0701897", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tensor([[ 0.9547, -5.1347, 11.7294]])" | |
| ] | |
| }, | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "17a58308", | |
| "outputId": "aca6c48e-22a1-44f4-c10a-8ec57cf734fb" | |
| }, | |
| "source": [ | |
| "params[1].grad" | |
| ], | |
| "id": "17a58308", | |
| "execution_count": null, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "tensor([-0.1740])" | |
| ] | |
| }, | |
| "execution_count": 17, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "46229832" | |
| }, | |
| "source": [ | |
| "" | |
| ], | |
| "id": "46229832", | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment