diff --git a/README.md b/README.md new file mode 100644 index 0000000..26929c9 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Ann Perceptrons + +This project implements artificial neural networks (ANN) using perceptrons as per the ANN university subject. + +1. Single Layer +2. Single Layer and Training +3. Multi Layer \ No newline at end of file diff --git a/perceptrons.ipynb b/perceptrons.ipynb index 4423abf..551e357 100644 --- a/perceptrons.ipynb +++ b/perceptrons.ipynb @@ -1 +1 @@ -{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Perceptron Networks\n","1. Single Layer\n","2. Single layer and Training\n","3. Multi Layer\n","\n","## 1. Single Layer perceptron"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.183684Z","iopub.status.busy":"2024-09-23T22:05:27.181778Z","iopub.status.idle":"2024-09-23T22:05:27.191100Z","shell.execute_reply":"2024-09-23T22:05:27.189735Z","shell.execute_reply.started":"2024-09-23T22:05:27.183607Z"},"trusted":true},"outputs":[],"source":["import numpy as np"]},{"cell_type":"code","execution_count":3,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.194942Z","iopub.status.busy":"2024-09-23T22:05:27.194269Z","iopub.status.idle":"2024-09-23T22:05:27.202310Z","shell.execute_reply":"2024-09-23T22:05:27.201067Z","shell.execute_reply.started":"2024-09-23T22:05:27.194868Z"},"trusted":true},"outputs":[],"source":["def sum_function(inputs, weights):\n"," return inputs.dot(weights)\n"," \n","def step_function(raw_output):\n"," return int(raw_output >= 1)"]},{"cell_type":"code","execution_count":4,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.204206Z","iopub.status.busy":"2024-09-23T22:05:27.203839Z","iopub.status.idle":"2024-09-23T22:05:27.214666Z","shell.execute_reply":"2024-09-23T22:05:27.213260Z","shell.execute_reply.started":"2024-09-23T22:05:27.204165Z"},"trusted":true},"outputs":[],"source":["inputs = np.array([-1, 7, 5])\n","weights = np.array([.8, .1, 0])"]},{"cell_type":"code","execution_count":5,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.217168Z","iopub.status.busy":"2024-09-23T22:05:27.216617Z","iopub.status.idle":"2024-09-23T22:05:27.227334Z","shell.execute_reply":"2024-09-23T22:05:27.226101Z","shell.execute_reply.started":"2024-09-23T22:05:27.217088Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Summation: -0.09999999999999998\n","Step Func: 0\n"]}],"source":["s = sum_function(inputs, weights)\n","r = step_function(s)\n","\n","print(f'Summation: {s}\\nStep Func: {r}')"]},{"cell_type":"markdown","metadata":{},"source":["## 2. Single layer with Training\n","> Now a ANN to predict logic gate (AND, NAND, OR, NOR) outputs based on two inputs"]},{"cell_type":"code","execution_count":6,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.230633Z","iopub.status.busy":"2024-09-23T22:05:27.230175Z","iopub.status.idle":"2024-09-23T22:05:27.240407Z","shell.execute_reply":"2024-09-23T22:05:27.239036Z","shell.execute_reply.started":"2024-09-23T22:05:27.230590Z"},"trusted":true},"outputs":[],"source":["inputs = np.array([\n"," [0,0], # 0\n"," [0,1], # 1\n"," [1,0], # 2\n"," [1,1], # 3\n","])\n","\n","outputs = {\n"," 'AND': np.array([0,0,0,1]),\n"," 'NAND': np.array([1,1,1,0]),\n"," 'OR': np.array([0,1,1,1]),\n"," 'NOR': np.array([1,0,0,0]),\n","}\n","\n","LEARN_RATE = 0.1"]},{"cell_type":"code","execution_count":7,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:06:51.503104Z","iopub.status.busy":"2024-09-23T22:06:51.502560Z","iopub.status.idle":"2024-09-23T22:06:51.518048Z","shell.execute_reply":"2024-09-23T22:06:51.516346Z","shell.execute_reply.started":"2024-09-23T22:06:51.503057Z"},"trusted":true},"outputs":[],"source":["def get_output(inputs, weights):\n"," s = sum_function(inputs, weights)\n"," return step_function(s)\n","\n","def train(gate):\n"," total_error = 1\n"," weights = np.array([0.0, 0.0]) # manually set\n"," GATE = outputs[gate]\n"," epoch = 0\n"," \n"," while(total_error != 0): # STOP CONDITION\n"," total_error = 0\n"," epoch += 1\n"," \n"," print(f'WEIGHTS: {weights}')\n"," for i in range(len(GATE)): # ERROR UPDATE\n"," current_output = get_output(np.asarray(inputs[i]), weights)\n"," current_error = abs(GATE[i] - current_output)\n"," total_error += current_error\n"," \n","\n"," print(f'IN{[i]}: ________ {inputs[i]}')\n"," for j in range(len(weights)): # LEARNING PHASE\n"," tmp_weight = weights[j]\n"," weights[j] = weights[j] + (LEARN_RATE * inputs[i][j] * current_error)\n","\n"," print(f'\\tOUT[{j}]: {current_output}', end='')\n"," \n"," if weights[j] != tmp_weight:\n"," print(f', Weight[{j}] updated: {weights[j]}')\n"," else:\n"," print()\n"," \n"," print(f'\\tcurrent_error: {current_error}')\n"," \n"," print(f'\\n --- END OF EPOCH {epoch} Total error {total_error}', '----------------------------', '\\n')\n"," print(f'>>> Training complete!')\n"," print(f'Final weights: W0={weights[0]} and W1={weights[1]}')"]},{"cell_type":"code","execution_count":8,"metadata":{"collapsed":true,"execution":{"iopub.execute_input":"2024-09-23T22:06:54.010218Z","iopub.status.busy":"2024-09-23T22:06:54.009791Z","iopub.status.idle":"2024-09-23T22:06:54.024485Z","shell.execute_reply":"2024-09-23T22:06:54.023164Z","shell.execute_reply.started":"2024-09-23T22:06:54.010177Z"},"jupyter":{"outputs_hidden":true},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["WEIGHTS: [0. 0.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.1\n","\tOUT[1]: 0, Weight[1] updated: 0.1\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 1 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.1 0.1]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.2\n","\tOUT[1]: 0, Weight[1] updated: 0.2\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 2 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.2 0.2]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.30000000000000004\n","\tOUT[1]: 0, Weight[1] updated: 0.30000000000000004\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 3 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.3 0.3]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.4\n","\tOUT[1]: 0, Weight[1] updated: 0.4\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 4 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.4 0.4]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.5\n","\tOUT[1]: 0, Weight[1] updated: 0.5\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 5 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.5 0.5]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 6 Total error 0 ---------------------------- \n","\n",">>> Training complete!\n","Final weights: W0=0.5 and W1=0.5\n"]}],"source":["train('AND')"]},{"cell_type":"code","execution_count":9,"metadata":{"collapsed":true,"execution":{"iopub.execute_input":"2024-09-23T22:07:15.312100Z","iopub.status.busy":"2024-09-23T22:07:15.311614Z","iopub.status.idle":"2024-09-23T22:07:15.330061Z","shell.execute_reply":"2024-09-23T22:07:15.328726Z","shell.execute_reply.started":"2024-09-23T22:07:15.312052Z"},"jupyter":{"outputs_hidden":true},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["WEIGHTS: [0. 0.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.1\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.1\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.2\n","\tOUT[1]: 0, Weight[1] updated: 0.2\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 1 Total error 3 ---------------------------- \n","\n","WEIGHTS: [0.2 0.2]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.30000000000000004\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.30000000000000004\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.4\n","\tOUT[1]: 0, Weight[1] updated: 0.4\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 2 Total error 3 ---------------------------- \n","\n","WEIGHTS: [0.4 0.4]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.5\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.5\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 3 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.5 0.5]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.6\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.6\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 4 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.6 0.6]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.7\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.7\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 5 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.7 0.7]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.7999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.7999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 6 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.8 0.8]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.8999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.8999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 7 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.9 0.9]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.9999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.9999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 8 Total error 2 ---------------------------- \n","\n","WEIGHTS: [1. 1.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 1.0999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 1.0999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 9 Total error 2 ---------------------------- \n","\n","WEIGHTS: [1.1 1.1]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 10 Total error 0 ---------------------------- \n","\n",">>> Training complete!\n","Final weights: W0=1.0999999999999999 and W1=1.0999999999999999\n"]}],"source":["train('OR')"]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[],"source":["# WONT STOP BECAUSE OF STOP CONDITION\n","# train('NAND') - Keeps increasing weights\n","# train('NOR') - Never updates weights"]},{"cell_type":"markdown","metadata":{},"source":["## 3. Multi-layer perceptron"]},{"cell_type":"markdown","metadata":{},"source":["# "]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[],"source":["# TODO"]}],"metadata":{"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30761,"isGpuEnabled":false,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"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.12.4"}},"nbformat":4,"nbformat_minor":4} +{"cells":[{"cell_type":"markdown","metadata":{},"source":["# Perceptron Networks\n","1. Single Layer\n","2. Single Layer and Training\n","3. Multi Layer\n","\n","## 1. Single Layer perceptron"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.183684Z","iopub.status.busy":"2024-09-23T22:05:27.181778Z","iopub.status.idle":"2024-09-23T22:05:27.191100Z","shell.execute_reply":"2024-09-23T22:05:27.189735Z","shell.execute_reply.started":"2024-09-23T22:05:27.183607Z"},"trusted":true},"outputs":[],"source":["import numpy as np"]},{"cell_type":"code","execution_count":3,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.194942Z","iopub.status.busy":"2024-09-23T22:05:27.194269Z","iopub.status.idle":"2024-09-23T22:05:27.202310Z","shell.execute_reply":"2024-09-23T22:05:27.201067Z","shell.execute_reply.started":"2024-09-23T22:05:27.194868Z"},"trusted":true},"outputs":[],"source":["def sum_function(inputs, weights):\n"," return inputs.dot(weights)\n"," \n","def step_function(raw_output):\n"," return int(raw_output >= 1)"]},{"cell_type":"code","execution_count":4,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.204206Z","iopub.status.busy":"2024-09-23T22:05:27.203839Z","iopub.status.idle":"2024-09-23T22:05:27.214666Z","shell.execute_reply":"2024-09-23T22:05:27.213260Z","shell.execute_reply.started":"2024-09-23T22:05:27.204165Z"},"trusted":true},"outputs":[],"source":["inputs = np.array([-1, 7, 5])\n","weights = np.array([.8, .1, 0])"]},{"cell_type":"code","execution_count":5,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.217168Z","iopub.status.busy":"2024-09-23T22:05:27.216617Z","iopub.status.idle":"2024-09-23T22:05:27.227334Z","shell.execute_reply":"2024-09-23T22:05:27.226101Z","shell.execute_reply.started":"2024-09-23T22:05:27.217088Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["Summation: -0.09999999999999998\n","Step Func: 0\n"]}],"source":["s = sum_function(inputs, weights)\n","r = step_function(s)\n","\n","print(f'Summation: {s}\\nStep Func: {r}')"]},{"cell_type":"markdown","metadata":{},"source":["## 2. Single layer with Training\n","> Now a ANN to predict logic gate (AND, NAND, OR, NOR) outputs based on two inputs"]},{"cell_type":"code","execution_count":6,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:05:27.230633Z","iopub.status.busy":"2024-09-23T22:05:27.230175Z","iopub.status.idle":"2024-09-23T22:05:27.240407Z","shell.execute_reply":"2024-09-23T22:05:27.239036Z","shell.execute_reply.started":"2024-09-23T22:05:27.230590Z"},"trusted":true},"outputs":[],"source":["inputs = np.array([\n"," [0,0], # 0\n"," [0,1], # 1\n"," [1,0], # 2\n"," [1,1], # 3\n","])\n","\n","outputs = {\n"," 'AND': np.array([0,0,0,1]),\n"," 'NAND': np.array([1,1,1,0]),\n"," 'OR': np.array([0,1,1,1]),\n"," 'NOR': np.array([1,0,0,0]),\n","}\n","\n","LEARN_RATE = 0.1"]},{"cell_type":"code","execution_count":7,"metadata":{"execution":{"iopub.execute_input":"2024-09-23T22:06:51.503104Z","iopub.status.busy":"2024-09-23T22:06:51.502560Z","iopub.status.idle":"2024-09-23T22:06:51.518048Z","shell.execute_reply":"2024-09-23T22:06:51.516346Z","shell.execute_reply.started":"2024-09-23T22:06:51.503057Z"},"trusted":true},"outputs":[],"source":["def get_output(inputs, weights):\n"," s = sum_function(inputs, weights)\n"," return step_function(s)\n","\n","def train(gate):\n"," total_error = 1\n"," weights = np.array([0.0, 0.0]) # manually set\n"," GATE = outputs[gate]\n"," epoch = 0\n"," \n"," while(total_error != 0): # STOP CONDITION\n"," total_error = 0\n"," epoch += 1\n"," \n"," print(f'WEIGHTS: {weights}')\n"," for i in range(len(GATE)): # ERROR UPDATE\n"," current_output = get_output(np.asarray(inputs[i]), weights)\n"," current_error = abs(GATE[i] - current_output)\n"," total_error += current_error\n"," \n","\n"," print(f'IN{[i]}: ________ {inputs[i]}')\n"," for j in range(len(weights)): # LEARNING PHASE\n"," tmp_weight = weights[j]\n"," weights[j] = weights[j] + (LEARN_RATE * inputs[i][j] * current_error)\n","\n"," print(f'\\tOUT[{j}]: {current_output}', end='')\n"," \n"," if weights[j] != tmp_weight:\n"," print(f', Weight[{j}] updated: {weights[j]}')\n"," else:\n"," print()\n"," \n"," print(f'\\tcurrent_error: {current_error}')\n"," \n"," print(f'\\n --- END OF EPOCH {epoch} Total error {total_error}', '----------------------------', '\\n')\n"," print(f'>>> Training complete!')\n"," print(f'Final weights: W0={weights[0]} and W1={weights[1]}')"]},{"cell_type":"code","execution_count":8,"metadata":{"collapsed":true,"execution":{"iopub.execute_input":"2024-09-23T22:06:54.010218Z","iopub.status.busy":"2024-09-23T22:06:54.009791Z","iopub.status.idle":"2024-09-23T22:06:54.024485Z","shell.execute_reply":"2024-09-23T22:06:54.023164Z","shell.execute_reply.started":"2024-09-23T22:06:54.010177Z"},"jupyter":{"outputs_hidden":true},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["WEIGHTS: [0. 0.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.1\n","\tOUT[1]: 0, Weight[1] updated: 0.1\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 1 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.1 0.1]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.2\n","\tOUT[1]: 0, Weight[1] updated: 0.2\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 2 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.2 0.2]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.30000000000000004\n","\tOUT[1]: 0, Weight[1] updated: 0.30000000000000004\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 3 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.3 0.3]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.4\n","\tOUT[1]: 0, Weight[1] updated: 0.4\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 4 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.4 0.4]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.5\n","\tOUT[1]: 0, Weight[1] updated: 0.5\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 5 Total error 1 ---------------------------- \n","\n","WEIGHTS: [0.5 0.5]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 6 Total error 0 ---------------------------- \n","\n",">>> Training complete!\n","Final weights: W0=0.5 and W1=0.5\n"]}],"source":["train('AND')"]},{"cell_type":"code","execution_count":9,"metadata":{"collapsed":true,"execution":{"iopub.execute_input":"2024-09-23T22:07:15.312100Z","iopub.status.busy":"2024-09-23T22:07:15.311614Z","iopub.status.idle":"2024-09-23T22:07:15.330061Z","shell.execute_reply":"2024-09-23T22:07:15.328726Z","shell.execute_reply.started":"2024-09-23T22:07:15.312052Z"},"jupyter":{"outputs_hidden":true},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["WEIGHTS: [0. 0.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.1\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.1\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.2\n","\tOUT[1]: 0, Weight[1] updated: 0.2\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 1 Total error 3 ---------------------------- \n","\n","WEIGHTS: [0.2 0.2]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.30000000000000004\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.30000000000000004\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 0, Weight[0] updated: 0.4\n","\tOUT[1]: 0, Weight[1] updated: 0.4\n","\tcurrent_error: 1\n","\n"," --- END OF EPOCH 2 Total error 3 ---------------------------- \n","\n","WEIGHTS: [0.4 0.4]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.5\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.5\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 3 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.5 0.5]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.6\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.6\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 4 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.6 0.6]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.7\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.7\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 5 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.7 0.7]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.7999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.7999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 6 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.8 0.8]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.8999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.8999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 7 Total error 2 ---------------------------- \n","\n","WEIGHTS: [0.9 0.9]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 0.9999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 0.9999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 8 Total error 2 ---------------------------- \n","\n","WEIGHTS: [1. 1.]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 0\n","\tOUT[1]: 0, Weight[1] updated: 1.0999999999999999\n","\tcurrent_error: 1\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 0, Weight[0] updated: 1.0999999999999999\n","\tOUT[1]: 0\n","\tcurrent_error: 1\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 9 Total error 2 ---------------------------- \n","\n","WEIGHTS: [1.1 1.1]\n","IN[0]: ________ [0 0]\n","\tOUT[0]: 0\n","\tOUT[1]: 0\n","\tcurrent_error: 0\n","IN[1]: ________ [0 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","IN[2]: ________ [1 0]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","IN[3]: ________ [1 1]\n","\tOUT[0]: 1\n","\tOUT[1]: 1\n","\tcurrent_error: 0\n","\n"," --- END OF EPOCH 10 Total error 0 ---------------------------- \n","\n",">>> Training complete!\n","Final weights: W0=1.0999999999999999 and W1=1.0999999999999999\n"]}],"source":["train('OR')"]},{"cell_type":"code","execution_count":12,"metadata":{},"outputs":[],"source":["# WONT STOP BECAUSE OF STOP CONDITION\n","# train('NAND') - Keeps increasing weights\n","# train('NOR') - Never updates weights"]},{"cell_type":"markdown","metadata":{},"source":["## 3. Multi-layer perceptron"]},{"cell_type":"code","execution_count":13,"metadata":{},"outputs":[],"source":["# TODO"]}],"metadata":{"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":30761,"isGpuEnabled":false,"isInternetEnabled":true,"language":"python","sourceType":"notebook"},"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.12.4"}},"nbformat":4,"nbformat_minor":4}