Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- author: Cory Kitchens
- assignment: CS356 Lab 7
- '''
- from __future__ import print_function
- import os
- import sys
- import argparse
- import csv
- import random
- import numpy
- import math
- '''Helper Functions'''
- def create2DList(rows, cols):
- tmp_list = list()
- for i in range(0,rows):
- tmp_list.append([0]*cols)
- return tmp_list
- def sigmoid(x):
- return 1/1+math.exp(-x)
- class Main(object):
- __shared_state = {}
- def __init__(self):
- self.input_data = list()
- self.classification = list()
- self__dict__ = self.__shared_state
- '''CSV Schema'''
- '''
- sepal laength, sepal width, petal length, petal width,
- iris setosa, iris versicolor, iris virginica,
- '''
- def read_csv(self, file_to_read):
- self.csv_path = os.path.join(os.getcwd() + '/data/')
- self.filename = self.csv_path + file_to_read
- with open(self.filename, 'r') as csvfile:
- filereader = csv.reader(csvfile)
- for row in filereader:
- self.input_data.append(row[0:4])
- self.classification.append(row[4:])
- csvfile.close()
- def __str__(self):
- return str(self.input_data) + " " + str(self.classification)
- class Neuron(object):
- def __init__(self):
- self.theta = random.uniform(-1.00,1.00)
- self.alpha = 0.05
- self.error = None
- self.delta = None
- self.connected_to = list()
- self.__data = None
- @staticmethod
- def factory(neuron_type, flower_type=None):
- if neuron_type == 0 : return InputNeuron()
- if neuron_type == 1 : return HiddenNeuron()
- if neuron_type == 2 : return OutputNeuron(flower_type)
- def set_data(self, data):
- self.__data = data
- def get_data(self):
- return self.__data
- def activate(self, node, weight):
- data = node.get_data()
- return (float(data) * float(weight))
- def sigmoid(x):
- return 1/1+math.exp(-x)
- #return tmp
- # def update_weight(self, idx):
- # if self.error != 0:
- # vals = list(self.flower_list[idx].values())
- # for i in range(0, len(self.weights)):
- # self.weights[i] = self.weights[i] + (vals[i] * self.alpha * self.error)
- # self.theta = self.alpha * self.error
- # def train(self):
- # for i in range(0, 100):
- # for i in range(0, len(self.label_list)):
- # self.feed_forward(i)
- # print("Iteration : ", i, "Weights are :", self.weights, "Error: ", self.error)
- # self.error = 0
- # def testing(self):
- # correct = 0
- # for i in range(0, 100):
- # self.feed_forward(i)
- # if self.y == self.label_list[i]:
- # correct += 1
- class InputNeuron(Neuron):
- '''Input nodes are passive nodes that receive a single input value and pass it along
- to each hidden layer node in the following layer'''
- def __init__(self):
- Neuron.__init__(self)
- class HiddenNeuron(Neuron):
- def __init__(self):
- Neuron.__init__(self)
- self.data_from_input = list()
- class OutputNeuron(Neuron):
- def __init__(self, flower_type):
- Neuron.__init__(self)
- self.flower_type = flower_type
- self.__data = None
- def set_data(self, data):
- self.__data = data
- def get_data(self):
- return self.__data
- def test_for_error(self):
- # y = self.get_data() - self.theta
- # flower = sum(self.flower_type)
- # self.delta = y*(1-y)*(flower-y)
- y = self.get_data()
- #Question - flower type is a list of 3 values?
- flower = sum(self.flower_type)
- self.delta = y*(1-y)*(flower-y)
- return self.delta
- class NeuralNetwork(object):
- def __init__(self):
- pass
- def instantiate_nodes(self, num_input, num_hidden, num_out):
- flower_types = [
- [0,0,1],
- [0,1,0],
- [1,0,0]
- ]
- self.input_neurons = [Neuron().factory(0) for i in range(0, num_input)]
- self.hidden_neurons = [Neuron().factory(1) for i in range(0, num_hidden)]
- self.output_neurons = [Neuron().factory(2, flower_types[i]) \
- for i in range(0, num_out)]
- def instantiate_weights(self, num_input, num_hidden, num_out):
- self.in_weights = create2DList(num_input, num_hidden)
- self.out_weights = create2DList(num_hidden, num_out)
- for i in range(0, num_input):
- for j in range(0, num_hidden):
- self.in_weights[i][j] = random.uniform(-1.00,1.00)
- for i in range(0, num_hidden):
- for j in range(0, num_out):
- self.out_weights[i][j] = random.uniform(-1.00, 1.00)
- def connect_nodes(self):
- for input_neuron in self.input_neurons:
- for hidden_neron in self.hidden_neurons:
- input_neuron.connected_to.append(hidden_neron)
- for hidden_neuron in self.hidden_neurons:
- for output_neuron in self.output_neurons:
- hidden_neuron.connected_to.append(output_neuron)
- def feed_forward(self):
- #1. Assign input to Input Nodes
- for row in main.input_data:
- for input_neuron in self.input_neurons:
- #Assign data to each Input Node
- input_neuron.set_data(row.pop(0))
- #For each hidden node, receive Input Node's data and weight
- sigma_sum = 0.00
- for i in range(0, len(self.hidden_neurons)):
- for j in range(0, len(self.input_neurons)):
- input_neuron = self.input_neurons[j]
- weights = self.in_weights[j][i]
- print("Weights", weights)
- sigma_sum += self.hidden_neurons[i].activate(input_neuron,weights)
- #Call sigmoid function and assign it to Hidden Neuron's data
- self.hidden_neurons[i].set_data(sigma_sum)
- #For each output node, receive Hidden NOde's data and weight
- sigma_sum = 0.00
- for i in range(0, len(self.output_neurons)):
- for j in range(0, len(self.hidden_neurons)):
- hidden_neuron = self.hidden_neurons[j]
- weights = self.out_weights[j][i]
- sigma_sum += self.output_neurons[i].activate(hidden_neuron, weights)
- self.output_neurons[i].set_data(sigma_sum)
- def back_propagation(self):
- #Error detection for Output
- for i in range(len(self.output_neurons)):
- error = self.output_neurons[i].test_for_error()
- #?
- #Error detection for Hidden
- #Update Input
- #Update Weights
- if __name__=="__main__":
- #Save CSV data to Main class
- main = Main()
- main.read_csv('lab7_iris.csv')
- #Instantiate a Neural Network
- nn = NeuralNetwork()
- nn.instantiate_nodes(4,3,3)
- nn.instantiate_weights(4,3,3)
- nn.connect_nodes()
- #Feed forward
- nn.feed_forward()
- nn.back_propagation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement