Advertisement
Guest User

Untitled

a guest
May 27th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. '''
  2. author: Cory Kitchens
  3. assignment: CS356 Lab 7
  4.  
  5. '''
  6. from __future__ import print_function
  7. import os
  8. import sys
  9. import argparse
  10. import csv
  11. import random
  12. import numpy
  13. import math
  14.  
  15. '''Helper Functions'''
  16. def create2DList(rows, cols):
  17. tmp_list = list()
  18. for i in range(0,rows):
  19. tmp_list.append([0]*cols)
  20. return tmp_list
  21.  
  22. def sigmoid(x):
  23. return 1/1+math.exp(-x)
  24.  
  25. class Main(object):
  26. __shared_state = {}
  27.  
  28. def __init__(self):
  29. self.input_data = list()
  30. self.classification = list()
  31. self__dict__ = self.__shared_state
  32.  
  33. '''CSV Schema'''
  34. '''
  35. sepal laength, sepal width, petal length, petal width,
  36. iris setosa, iris versicolor, iris virginica,
  37. '''
  38. def read_csv(self, file_to_read):
  39. self.csv_path = os.path.join(os.getcwd() + '/data/')
  40. self.filename = self.csv_path + file_to_read
  41. with open(self.filename, 'r') as csvfile:
  42. filereader = csv.reader(csvfile)
  43. for row in filereader:
  44. self.input_data.append(row[0:4])
  45. self.classification.append(row[4:])
  46. csvfile.close()
  47.  
  48. def __str__(self):
  49. return str(self.input_data) + " " + str(self.classification)
  50.  
  51. class Neuron(object):
  52. def __init__(self):
  53. self.theta = random.uniform(-1.00,1.00)
  54. self.alpha = 0.05
  55. self.error = None
  56. self.delta = None
  57. self.connected_to = list()
  58. self.__data = None
  59.  
  60.  
  61. @staticmethod
  62. def factory(neuron_type, flower_type=None):
  63. if neuron_type == 0 : return InputNeuron()
  64. if neuron_type == 1 : return HiddenNeuron()
  65. if neuron_type == 2 : return OutputNeuron(flower_type)
  66.  
  67. def set_data(self, data):
  68. self.__data = data
  69.  
  70. def get_data(self):
  71. return self.__data
  72.  
  73.  
  74. def activate(self, node, weight):
  75. data = node.get_data()
  76. return (float(data) * float(weight))
  77.  
  78.  
  79. def sigmoid(x):
  80. return 1/1+math.exp(-x)
  81. #return tmp
  82.  
  83. # def update_weight(self, idx):
  84. # if self.error != 0:
  85. # vals = list(self.flower_list[idx].values())
  86. # for i in range(0, len(self.weights)):
  87. # self.weights[i] = self.weights[i] + (vals[i] * self.alpha * self.error)
  88. # self.theta = self.alpha * self.error
  89.  
  90. # def train(self):
  91. # for i in range(0, 100):
  92. # for i in range(0, len(self.label_list)):
  93. # self.feed_forward(i)
  94. # print("Iteration : ", i, "Weights are :", self.weights, "Error: ", self.error)
  95. # self.error = 0
  96.  
  97. # def testing(self):
  98. # correct = 0
  99. # for i in range(0, 100):
  100. # self.feed_forward(i)
  101. # if self.y == self.label_list[i]:
  102. # correct += 1
  103.  
  104. class InputNeuron(Neuron):
  105. '''Input nodes are passive nodes that receive a single input value and pass it along
  106. to each hidden layer node in the following layer'''
  107.  
  108. def __init__(self):
  109. Neuron.__init__(self)
  110.  
  111. class HiddenNeuron(Neuron):
  112. def __init__(self):
  113. Neuron.__init__(self)
  114. self.data_from_input = list()
  115.  
  116.  
  117. class OutputNeuron(Neuron):
  118. def __init__(self, flower_type):
  119. Neuron.__init__(self)
  120. self.flower_type = flower_type
  121. self.__data = None
  122.  
  123.  
  124. def set_data(self, data):
  125. self.__data = data
  126.  
  127. def get_data(self):
  128. return self.__data
  129.  
  130. def test_for_error(self):
  131. # y = self.get_data() - self.theta
  132. # flower = sum(self.flower_type)
  133. # self.delta = y*(1-y)*(flower-y)
  134. y = self.get_data()
  135.  
  136. #Question - flower type is a list of 3 values?
  137. flower = sum(self.flower_type)
  138. self.delta = y*(1-y)*(flower-y)
  139. return self.delta
  140.  
  141. class NeuralNetwork(object):
  142.  
  143. def __init__(self):
  144. pass
  145.  
  146. def instantiate_nodes(self, num_input, num_hidden, num_out):
  147. flower_types = [
  148. [0,0,1],
  149. [0,1,0],
  150. [1,0,0]
  151. ]
  152.  
  153. self.input_neurons = [Neuron().factory(0) for i in range(0, num_input)]
  154. self.hidden_neurons = [Neuron().factory(1) for i in range(0, num_hidden)]
  155. self.output_neurons = [Neuron().factory(2, flower_types[i]) \
  156. for i in range(0, num_out)]
  157.  
  158. def instantiate_weights(self, num_input, num_hidden, num_out):
  159. self.in_weights = create2DList(num_input, num_hidden)
  160. self.out_weights = create2DList(num_hidden, num_out)
  161.  
  162.  
  163. for i in range(0, num_input):
  164. for j in range(0, num_hidden):
  165. self.in_weights[i][j] = random.uniform(-1.00,1.00)
  166.  
  167. for i in range(0, num_hidden):
  168. for j in range(0, num_out):
  169. self.out_weights[i][j] = random.uniform(-1.00, 1.00)
  170.  
  171.  
  172. def connect_nodes(self):
  173. for input_neuron in self.input_neurons:
  174. for hidden_neron in self.hidden_neurons:
  175. input_neuron.connected_to.append(hidden_neron)
  176.  
  177. for hidden_neuron in self.hidden_neurons:
  178. for output_neuron in self.output_neurons:
  179. hidden_neuron.connected_to.append(output_neuron)
  180.  
  181. def feed_forward(self):
  182. #1. Assign input to Input Nodes
  183. for row in main.input_data:
  184. for input_neuron in self.input_neurons:
  185. #Assign data to each Input Node
  186. input_neuron.set_data(row.pop(0))
  187.  
  188. #For each hidden node, receive Input Node's data and weight
  189. sigma_sum = 0.00
  190. for i in range(0, len(self.hidden_neurons)):
  191. for j in range(0, len(self.input_neurons)):
  192. input_neuron = self.input_neurons[j]
  193. weights = self.in_weights[j][i]
  194. print("Weights", weights)
  195. sigma_sum += self.hidden_neurons[i].activate(input_neuron,weights)
  196.  
  197. #Call sigmoid function and assign it to Hidden Neuron's data
  198. self.hidden_neurons[i].set_data(sigma_sum)
  199.  
  200. #For each output node, receive Hidden NOde's data and weight
  201. sigma_sum = 0.00
  202. for i in range(0, len(self.output_neurons)):
  203. for j in range(0, len(self.hidden_neurons)):
  204. hidden_neuron = self.hidden_neurons[j]
  205. weights = self.out_weights[j][i]
  206. sigma_sum += self.output_neurons[i].activate(hidden_neuron, weights)
  207.  
  208. self.output_neurons[i].set_data(sigma_sum)
  209.  
  210. def back_propagation(self):
  211.  
  212. #Error detection for Output
  213. for i in range(len(self.output_neurons)):
  214. error = self.output_neurons[i].test_for_error()
  215. #?
  216.  
  217. #Error detection for Hidden
  218.  
  219. #Update Input
  220.  
  221. #Update Weights
  222.  
  223.  
  224.  
  225. if __name__=="__main__":
  226.  
  227. #Save CSV data to Main class
  228. main = Main()
  229. main.read_csv('lab7_iris.csv')
  230.  
  231. #Instantiate a Neural Network
  232. nn = NeuralNetwork()
  233. nn.instantiate_nodes(4,3,3)
  234. nn.instantiate_weights(4,3,3)
  235. nn.connect_nodes()
  236. #Feed forward
  237. nn.feed_forward()
  238. nn.back_propagation()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement