commit cfd8f892ed0f6a0ae57f61adcf02d8463b943010
parent c8d6d003615f9d8ad10ed6c51758b01fc9cfc861
Author: Cody Lewis <codymlewis@protonmail.com>
Date: Thu, 7 Mar 2019 10:58:10 +1100
Made perceptron use csv for logic
Diffstat:
8 files changed, 79 insertions(+), 15 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1,3 @@
+tags
+*.pyc
+Session.vim
diff --git a/AND2.csv b/AND2.csv
@@ -0,0 +1,4 @@
+1,0,0,0
+1,0,1,0
+1,1,0,0
+1,1,1,1
diff --git a/AND3.csv b/AND3.csv
@@ -0,0 +1,8 @@
+1,0,0,0,0
+1,0,0,1,0
+1,0,1,0,0
+1,0,1,1,0
+1,1,0,0,0
+1,1,0,1,0
+1,1,1,0,0
+1,1,1,1,1
diff --git a/OR2.csv b/OR2.csv
@@ -0,0 +1,4 @@
+1,0,0,0
+1,0,1,1
+1,1,0,1
+1,1,1,1
diff --git a/OR3.csv b/OR3.csv
@@ -0,0 +1,8 @@
+1,0,0,0,0
+1,0,0,1,1
+1,0,1,0,1
+1,0,1,1,1
+1,1,0,0,1
+1,1,0,1,1
+1,1,1,0,1
+1,1,1,1,1
diff --git a/Perceptron.py b/Perceptron.py
@@ -7,10 +7,48 @@ Author: Cody Lewis
Date: 2019-03-06
'''
+import sys
import numpy as np
import matplotlib.pyplot as plt
+def read_csv(filename):
+ '''
+ Read a csv detailing the inputs and outputs and return them in a np array format.
+ '''
+ inputs = []
+ responses = []
+
+ with open(filename) as csv:
+ for line in csv:
+ input_line = []
+ while line.find(",") > -1:
+ input_line.append(int(line[:line.find(",")]))
+ line = line[line.find(",") + 1:]
+ inputs.append(input_line)
+ responses.append(int(line))
+
+ return np.array(inputs), np.array(responses)
+
+def create_connected_matrix(input_len):
+ '''
+ Create the connectedness matrix for a single layer neural network perceptron.
+ '''
+ connected_matrix = []
+
+ for _ in range(input_len):
+ connected_row = [0 for _ in range(input_len)]
+ connected_row.append(1)
+ connected_matrix.append(connected_row)
+ final_row = [1 for _ in range(input_len)]
+ final_row.append(0)
+ connected_matrix.append(final_row)
+
+ return np.array(connected_matrix)
+
def activate(x_value, coefficient=1, constant=0):
+ '''
+ Perform the sigmoid function to determine whether a neuron is activated.
+ '''
return 1 / (1 + np.exp(-coefficient * x_value - constant))
def activation_strength(weight, node_state):
@@ -83,23 +121,12 @@ def hill_climb(error_goal, inputs, target_responses, weights, connected_matrix,
return weights, error_champ, errors
-
if __name__ == '__main__':
ERROR_GOAL = 0.1
- INPUTS = np.array([
- [1, 0, 0],
- [1, 0, 1],
- [1, 1, 0],
- [1, 1, 1]
- ])
- TARGET_RESPONSES = np.array([1, 1, 1, 0])
- CONNECTED_MATRIX = np.array([
- [0, 0, 0, 1],
- [0, 0, 0, 1],
- [0, 0, 0, 1],
- [1, 1, 1, 0]
- ])
- OUTPUT_NEURON = 3
+ ARGS = sys.argv[1:]
+ INPUTS, TARGET_RESPONSES = read_csv(ARGS[0] if ARGS else "AND2.csv")
+ CONNECTED_MATRIX = create_connected_matrix(len(INPUTS[0]))
+ OUTPUT_NEURON = len(CONNECTED_MATRIX) - 1
WEIGHTS = np.array([np.random.normal() for _ in range(len(CONNECTED_MATRIX) - 1)])
print(WEIGHTS)
WEIGHTS, ERROR_CHAMP, ERRORS = hill_climb(ERROR_GOAL, INPUTS, TARGET_RESPONSES, WEIGHTS, CONNECTED_MATRIX, OUTPUT_NEURON)
diff --git a/README.md b/README.md
@@ -1,6 +1,15 @@
# Perceptron
A implementation of a simple neural network
+## Installation
+```
+pip3 -r requirements.txt
+```
## Running
```
python3 Perceptron.py
```
+
+You can also specify a different logic statment with
+```
+python3 Perceptron.py filename.csv
+```
diff --git a/requirements.txt b/requirements.txt
@@ -0,0 +1 @@
+numpy