{ "cells": [ { "cell_type": "code", "execution_count": 6, "id": "5d978533-e16a-47ca-8a6b-068096157b51", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Weights at the beginning: \n", "[0.05570723 0.91606935 0.03272122 0.4935642 ]\n", "Training finished. Final weights:\n", "[0.06978601 0.83112226 0.03200317 0.56776683]\n" ] } ], "source": [ "#Imports\n", "import numpy as np\n", "import random\n", "\n", "#Variables\n", "eta = 0.01 # Learning Rate\n", "n = 3 # Dimension, \n", "training_set_size = 12 #Size of the training set\n", "\n", "#Initialise arrays\n", "x = np.mgrid[0.0:training_set_size, 0:n+1] # Training set, only use x[0] subarray\n", "w = np.arange(0, n + 1 , 1.0) # Weight vector\n", "t = np.arange(0, training_set_size, 1)\n", "\n", "# To ensure that random numbers stay the same for multiple executions\n", "random.seed(5)\n", "\n", "#Fill arrays with values\n", "for i in range(training_set_size):\n", " #Set value of t either to +1 or -1\n", " random_number = random.random() #Call random number\n", " if(random_number < 0.5):\n", " t[i] = -1\n", " else:\n", " t[i] = +1\n", " \n", " #Set values of x, take random values between -5 and 5\n", " for j in range(n + 1):\n", " random_number_2 = random.random()\n", " x[0][i][j] = (random.random())*5.0\n", " if(random_number_2 < 0.5):\n", " x[0][i][j] *= -1\n", " \n", "\n", "# Initialise weight vector, weights between 0 and +1\n", "for i in range(n + 1):\n", " w[i] = float(random.random())\n", "\n", "print(\"Weights at the beginning: \")\n", "print(w)\n", "\n", "# Training loop\n", "termination = False # Python has no do-while loop\n", "\n", "while(termination == False):\n", " # Select random training example\n", " random_number = int(random.random()*training_set_size)\n", " \n", " #Calculate Dot product\n", " result = 0.0\n", " for i in range(n+1):\n", " result += w[i]*x[0][random_number][i]\n", " \n", " # Check termination condition\n", " if(((np.sign(result) == -1) and (t[random_number] == -1)) or ((np.sign(result) == 1) and (t[random_number] == 1))):\n", " termination = True\n", " break\n", " \n", " # If no termination, update weights\n", " else:\n", " for i in range(n+1):\n", " w[i] += eta*(t[random_number] - np.sign(result)) * x[0][random_number][i]\n", "\n", "print(\"Training finished. Final weights:\")\n", "print(w)" ] }, { "cell_type": "code", "execution_count": null, "id": "c125185c-4cf3-4e43-83c7-9e1c3936e268", "metadata": {}, "outputs": [], "source": [] } ], "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.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }