{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import torch\n", "from torch import tensor, sigmoid, mm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Gradient" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(9., grad_fn=)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = tensor(8.0, requires_grad=True)\n", "y = (x-5) ** 2\n", "y" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor(6.)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "y.backward()\n", "x.grad" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Descent" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8.0 tensor(6.)\n", "7.4 tensor(4.8000)\n", "6.92 tensor(3.8400)\n", "6.536 tensor(3.0720)\n", "6.229 tensor(2.4576)\n", "5.983 tensor(1.9661)\n", "5.786 tensor(1.5729)\n", "5.629 tensor(1.2583)\n", "5.503 tensor(1.0066)\n", "5.403 tensor(0.8053)\n", "5.322 tensor(0.6442)\n", "5.258 tensor(0.5154)\n", "5.206 tensor(0.4123)\n", "5.165 tensor(0.3299)\n", "5.132 tensor(0.2639)\n" ] }, { "data": { "text/plain": [ "tensor(5.1056, requires_grad=True)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = tensor(8.0, requires_grad=True)\n", "for i in range(15):\n", " y = (x-5) ** 2\n", " y.backward()\n", " with torch.no_grad():\n", " print(round(float(x), 3), x.grad)\n", " x -= x.grad * 0.1\n", " x.grad.zero_()\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Example 2: Diabetes" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
pregnanciesglucosebpskininsulinbmipedigreeageoutcome
061487235033.60.627501
11856629026.60.351310
28183640023.30.672321
318966239428.10.167210
40137403516843.12.288331
\n", "
" ], "text/plain": [ " pregnancies glucose bp skin insulin bmi pedigree age outcome\n", "0 6 148 72 35 0 33.6 0.627 50 1\n", "1 1 85 66 29 0 26.6 0.351 31 0\n", "2 8 183 64 0 0 23.3 0.672 32 1\n", "3 1 89 66 23 94 28.1 0.167 21 0\n", "4 0 137 40 35 168 43.1 2.288 33 1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_csv(\"diabetes.csv\")\n", "df.head()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0.63994726, 0.84832379, 0.14964075, ..., 0.46849198,\n", " 1.4259954 , 1. ],\n", " [-0.84488505, -1.12339636, -0.16054575, ..., -0.36506078,\n", " -0.19067191, 1. ],\n", " [ 1.23388019, 1.94372388, -0.26394125, ..., 0.60439732,\n", " -0.10558415, 1. ],\n", " ...,\n", " [ 0.3429808 , 0.00330087, 0.14964075, ..., -0.68519336,\n", " -0.27575966, 1. ],\n", " [-0.84488505, 0.1597866 , -0.47073225, ..., -0.37110101,\n", " 1.17073215, 1. ],\n", " [-0.84488505, -0.8730192 , 0.04624525, ..., -0.47378505,\n", " -0.87137393, 1. ]])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = df.iloc[:, :-1].values\n", "X -= X.mean(axis=0)\n", "X /= X.std(axis=0)\n", "np.concatenate([X, np.ones(X.shape[0]).reshape(-1, 1)], axis=1)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "X = tensor(X, dtype=torch.float32)\n", "y = tensor(df.iloc[:, -1].values, dtype=torch.float32)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "tensor([[-0.6886, 0.7149, -0.8651, 0.8087, 0.6591, 1.4141, -1.0567, -1.6358]],\n", " requires_grad=True)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = tensor(np.random.normal(size=(1, X.shape[1])), dtype=torch.float, requires_grad=True)\n", "A" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.4752604166666667" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def accuracy(A, X, y):\n", " predicted = (sigmoid(mm(A, X.T))[0] - 0.5).ceil()\n", " correct = 0\n", " for i in range(len(y)):\n", " if predicted[i] == y[i]:\n", " correct += 1\n", " return correct / len(y)\n", "accuracy(A, X, y)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "LOSS 0.3989822566509247\n", "LOSS 0.3881254196166992\n", "LOSS 0.37732815742492676\n", "LOSS 0.366690993309021\n", "LOSS 0.3563186228275299\n" ] } ], "source": [ "for i in range(50):\n", " loss = ((sigmoid(mm(A, X.T))[0] - y) ** 2).mean()\n", " if i % 10 == 0:\n", " print(\"LOSS\", float(loss))\n", " loss.backward()\n", " with torch.no_grad():\n", " A -= A.grad * 0.1\n", " A.grad.zero_()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5299479166666666" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "accuracy(A, X, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.2" } }, "nbformat": 4, "nbformat_minor": 2 }