Skip to content

Instantly share code, notes, and snippets.

@mattbullen
Created October 16, 2025 11:56
Show Gist options
  • Select an option

  • Save mattbullen/2dff79dced221948360cee151af54c20 to your computer and use it in GitHub Desktop.

Select an option

Save mattbullen/2dff79dced221948360cee151af54c20 to your computer and use it in GitHub Desktop.
Unit07 Ex2 perceptron_AND_operator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/mattbullen/2dff79dced221948360cee151af54c20/unit07-ex2-perceptron_and_operator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Tz5LwA7Dp3WB"
},
"source": [
"### Author: Dr Mike Lakoju, CardiffMet"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Mq7zWJMrp3WC"
},
"source": [
"### Import Library"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "keVkVuxrp3WC"
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "m-6C3YaMp3WD"
},
"source": [
"### Define \"Inputs, outputs and weights\" as Numpy arrays"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "sDk36SJ9p3WD"
},
"source": [
"#### Inputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_ML2Asrcp3WD"
},
"outputs": [],
"source": [
"# Creating input values as a matrix not as a vector\n",
"inputs = np.array([[0,0], [0,1], [1,0], [1,1]])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "leg-tBcCp3WD",
"outputId": "43ca262c-7df5-4556-99c2-05c871963995"
},
"outputs": [
{
"data": {
"text/plain": [
"(4, 2)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Chcking the shape of the inputs\n",
"\n",
"inputs.shape"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hcdrWiXgp3WE"
},
"source": [
"#### Outputs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "UkMc-RpAp3WE"
},
"outputs": [],
"source": [
"outputs = np.array([0, 0, 0, 1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "56x1d0Wep3WF",
"outputId": "39272c07-31fc-4b30-942e-678bd512775a"
},
"outputs": [
{
"data": {
"text/plain": [
"(4,)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Checking the shape of the outputs\n",
"\n",
"outputs.shape"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Cku67EZsp3WF"
},
"source": [
"#### Weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d4UU6K9Up3WF"
},
"outputs": [],
"source": [
"# one weight for x1 and one for x2\n",
"weights = np.array([0.0, 0.0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "wLubPiBBp3WF"
},
"source": [
"#### Learning Rate"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7Nwrr2n9p3WF"
},
"outputs": [],
"source": [
"learning_rate = 0.1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qNhzY3Pxp3WF"
},
"source": [
"## Step function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6LjEwRCDp3WF"
},
"outputs": [],
"source": [
"# This is our Activation function\n",
"\n",
"def step_function(sum):\n",
" if (sum >= 1):\n",
" #print(f'The Sum of Weights is Greater or equal to 1')\n",
" return 1\n",
" else:\n",
" #print(f'The Sum of Weights is NOT > or = to 1')\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IgP1ob56p3WF"
},
"source": [
"## Process Output"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ECIj-7dvp3WF"
},
"source": [
"<b>We define a function that allows us to calculate/ process the output. The function accepts an instance of our data, then calculate the sum function using Numpy. Finally, we check the output by passing it through the \"Step Function.\"</b>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "u7eS6c8jp3WF"
},
"outputs": [],
"source": [
"def cal_output(instance):\n",
" sum_func = instance.dot(weights)\n",
" return step_function(sum_func)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aith4zwPp3WF"
},
"source": [
"We pass it as alist in a numpy array ..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0Cg_mChop3WF",
"outputId": "7a6728df-ea8f-46ca-c968-88d5649fd009"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cal_output(np.array([[1,1]]))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qoGBraU9p3WG"
},
"source": [
"## Train"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "auhzkF0Ep3WG",
"outputId": "11e5ba07-78b7-419e-d37a-16bbf3643940"
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check the number of outputs\n",
"\n",
"len(outputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YCPhpDsLp3WG",
"outputId": "e1b67975-fced-4659-c653-c85a0f2dd2b6"
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 1])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Checking the index of the input at postion 3 ..\n",
"# this is the last inpute value\n",
"inputs[3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ip-jghrOp3WG",
"outputId": "86a86635-30ee-4159-eb0e-a4faf9ade853"
},
"outputs": [
{
"data": {
"text/plain": [
"array([[0, 0],\n",
" [0, 1],\n",
" [1, 0],\n",
" [1, 1]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inputs"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "nmiHkIQap3WG"
},
"source": [
"<b>Note that:</b> usually, we will need to define the number of epochs, because we will never really get a value of zero when dealing with real-world data. However, for this small data, we will run the loop till we obtain zero error"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ja2fS0P0p3WG"
},
"outputs": [],
"source": [
"def train():\n",
" #\n",
" total_error_value = 1\n",
" # While the total_error_value is not equal to zero. we are asumming that at the start of running our network there will be no zero\n",
" while (total_error_value != 0):\n",
" #making the total_error 0 so we can do other calculations\n",
" total_error_value = 0\n",
" #Looping into each row of the dataset (remember indexing in python starts at zero hence 0-3 which are 4 values)\n",
" for i in range(len(outputs)):\n",
" #Calculating predictions\n",
" prediction = cal_output(inputs[i])\n",
" # Calculating the absolute value of the error\n",
" error = abs(outputs[i] - prediction)\n",
" #Updating the error\n",
" total_error_value += error\n",
"\n",
" if error > 0:\n",
" for j in range(len(weights)):\n",
" #updating the weights for x1 and x2\n",
" weights[j] = weights[j] + (learning_rate * inputs[i][j] * error)\n",
" print('Weight updated to: ' + str(weights[j]))\n",
" print('Total error Value: ' + str(total_error_value))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rMM7_I37p3WG",
"outputId": "f467364f-2e60-453e-9c80-ee89c510151c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Weight updated to: 0.1\n",
"Weight updated to: 0.1\n",
"Total error Value: 1\n",
"Weight updated to: 0.2\n",
"Weight updated to: 0.2\n",
"Total error Value: 1\n",
"Weight updated to: 0.30000000000000004\n",
"Weight updated to: 0.30000000000000004\n",
"Total error Value: 1\n",
"Weight updated to: 0.4\n",
"Weight updated to: 0.4\n",
"Total error Value: 1\n",
"Weight updated to: 0.5\n",
"Weight updated to: 0.5\n",
"Total error Value: 1\n",
"Total error Value: 0\n"
]
}
],
"source": [
"train()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IyUK-40jp3WG"
},
"source": [
"## Classification"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SfCD6mc_p3WG",
"outputId": "7e442371-9cb6-49fa-b31f-566156921288"
},
"outputs": [
{
"data": {
"text/plain": [
"array([0.5, 0.5])"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Now we have the final weights that will be used to classify new instances of the data after training.\n",
"\n",
"weights"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Kpco_TiEp3WG",
"outputId": "cea7c37b-a152-45e2-d4e3-194a05971eab"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cal_output(np.array([0,0]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "g3fy25hOp3WG",
"outputId": "bfc9b814-20ce-41c1-e246-f1eefd810053"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cal_output(np.array([0,1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "nLuxTTRrp3WH",
"outputId": "0d40e3e5-f8a8-43e8-f894-5376bfe0af5b"
},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cal_output(np.array([1,0]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Ewi4l7afp3WH",
"outputId": "d857be66-acef-45b4-9b77-7c1789b5ffc1"
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cal_output(np.array([1,1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "wxW6tWtup3WH"
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fgiK1TJ_p3WH"
},
"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.6"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment