Last active
September 20, 2021 07:30
-
-
Save sigmaroles/b7b7955e8254260dbe25389cadc164b9 to your computer and use it in GitHub Desktop.
delta_rule_chap12_Anderson
This file contains 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": 0, | |
"metadata": { | |
"colab": { | |
"name": "delta_rule_chap12_Anderson", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNLS5HFxuNAQb8yJumAzWM5", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/sigmaroles/b7b7955e8254260dbe25389cadc164b9/delta_rule_chap12_anderson.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ca1om2FOGLGF" | |
}, | |
"source": [ | |
"**Anderson chapter 12:** <br>\n", | |
"12.2 Octave Functions for the Delta Rule Exercise <br>\n", | |
"and <br>\n", | |
"12.3 Using Octave to Solve the Delta Rule Exercise" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "QoZJFWjS1zWv" | |
}, | |
"source": [ | |
"import numpy as np\n", | |
"rand = np.random.rand" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4yy6JYAv0sIc" | |
}, | |
"source": [ | |
"def mkLine():\n", | |
" return rand(), rand()\n", | |
"\n", | |
"def mkXs(n):\n", | |
" return rand(n,1)*40 - 20" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "LjOqTGe-2p8F" | |
}, | |
"source": [ | |
"def mkYs(m,b,xs):\n", | |
" #xs = np.array([1,2,3,4]).reshape(-1,1)\n", | |
" #m, b = mkLine()\n", | |
" ys = m * xs + b\n", | |
" ys = ys.reshape(-1,1)\n", | |
"\n", | |
" c1 = np.tile([1, -1], (round(len(xs)/2), 1)).reshape(-1,1)\n", | |
" # repmat is tile in numpy: https://docs.scipy.org/doc/numpy-1.15.0/user/numpy-for-matlab-users.html\n", | |
" # reshape is to make it one-dimensional\n", | |
"\n", | |
" yp = ys + np.multiply(5*rand(len(xs),1), c1)\n", | |
" temp1 = np.tile([1], (len(xs),1))\n", | |
" ymat = np.hstack([c1,ys,xs,yp,temp1])\n", | |
"\n", | |
" return ymat" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BAHLuEbr7l4S" | |
}, | |
"source": [ | |
"def compOut(t,wv,iv):\n", | |
" a = np.dot(wv,iv);\n", | |
" if a>=t:\n", | |
" return 1\n", | |
" else:\n", | |
" return -1" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2M6PKmaW9Hkq" | |
}, | |
"source": [ | |
"def dR(eta, obs, des, iv, wv):\n", | |
" return eta * np.dot((des-obs),iv) + wv" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "n1MY94L19SeQ" | |
}, | |
"source": [ | |
"def oneLoop(t,eta, cls, iv, wv):\n", | |
" obs = compOut(t,wv,iv)\n", | |
" nwv = dR(eta, obs, cls, iv, wv)\n", | |
" return nwv" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "6v166GWp9gcK" | |
}, | |
"source": [ | |
"def onePass(dataMat, wv, eta, t):\n", | |
" ss = dataMat.shape[0]\n", | |
" for i in range(ss):\n", | |
" wv = oneLoop(t,eta,dataMat[i,0], [dataMat[i,2], dataMat[i,3], dataMat[i,4]], wv)\n", | |
" return wv" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "G4BxJRD4BwmL" | |
}, | |
"source": [ | |
"def train(dataMat, wv, eta, t):\n", | |
" epsilon = 0.001\n", | |
" owv = [0, 0, 0]\n", | |
" nwv = wv\n", | |
" while (np.sum(np.abs(nwv-owv)) > epsilon):\n", | |
" owv = nwv\n", | |
" nwv = onePass(dataMat, nwv, eta, t)\n", | |
" \n", | |
" return nwv" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "k_VanL-KCOaj" | |
}, | |
"source": [ | |
"s, i = mkLine()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "2NYEa4aeClRc" | |
}, | |
"source": [ | |
"xs = mkXs(20)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "0cBdO6JFCnOz" | |
}, | |
"source": [ | |
"dm = mkYs(s,i,xs)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "1MRDrSESCpiD" | |
}, | |
"source": [ | |
"trainSet = dm[:10, :]\n", | |
"testSet = dm[10:20, :]" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "sgjhLR1kC1mP" | |
}, | |
"source": [ | |
"saveWt = train(trainSet, rand(1,3), 0.1, 0)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "zypn9WHkDT-P", | |
"outputId": "c0211b12-ab03-4c1d-ddf8-36a975904972" | |
}, | |
"source": [ | |
"# indices must be reduced by one because Python arrays start at zero, Matlab's start at one\n", | |
"(np.dot(saveWt, trainSet[:,[2,3,4]].transpose()) > 0) * 2 - 1" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[ 1, -1, 1, -1, 1, -1, 1, -1, 1, -1]])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 14 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "-Kj4K3b8E4FY", | |
"outputId": "e65e9e0c-1afd-46f4-95a3-ac8aa2e3103d" | |
}, | |
"source": [ | |
"# what about test set?\n", | |
"(np.dot(saveWt, testSet[:,[2,3,4]].transpose()) > 0) * 2 - 1" | |
], | |
"execution_count": null, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[ 1, -1, 1, -1, 1, -1, 1, -1, 1, -1]])" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 15 | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment