Advertisement
Guest User

neural network

a guest
Jan 7th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.62 KB | None | 0 0
  1.  
  2. import random
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import matplotlib.colors as pltclr
  6. import pandas as pd
  7.  
  8. #TODO remove this when finished
  9. random.seed(3)
  10.  
  11. #test: some linear function
  12. T_DATA_IN  = [[1]]
  13. T_DATA_OUT = [[0.8]]
  14.  
  15. #classification: positive or negative number
  16. #T_DATA_IN  = [[1]  ,[0.5],[-0.8],[-1] ,[-0.2] ,[0.1]]
  17. #T_DATA_OUT = [[1,0],[1,0],[0,1] ,[0,1],[0,1]  ,[0,1] ]
  18.  
  19. #classification: large or small vector
  20. #T_DATA_IN  = [[1,2],[0.5,1],[10,20],[5,10]]
  21. #T_DATA_OUT = [[1,0],[1,0]  ,[0,1]  ,[0,1] ]
  22.  
  23. #configuration:
  24. HIDDEN_LAYERS = [4] #count of nodes for hidden layers. e.g. [2,3] or [9,2,2,2]
  25. SIGMOID_EXPONENT = 5  #for the activation function
  26. MAX_ITERATIONS = 300  #to prevent non-converging ininite loops
  27. ERROR_THRESHOLD = 0.01 #when the error is small enough
  28. LEARNING_RATE = 0.1
  29.  
  30. #this defines the shape of the network. Training data will match the shape
  31. LAYER_SIZES = [len(T_DATA_IN[0])]+HIDDEN_LAYERS+[len(T_DATA_OUT[0])]
  32. #some usefull stuff to make it simpler
  33. LAYER_COUNT = len(LAYER_SIZES)
  34.  
  35. plotcolor = [1,0,0] #first hsv color should be black
  36.  
  37. #log some information
  38. print "training data:"
  39. for i in range(len(T_DATA_IN)):
  40.     print "input:",str(T_DATA_IN[i]),"output:",str(T_DATA_OUT[i])
  41.  
  42. #this for-loop creates for each layer a weight matrix, initialised with random values from -1 to 1
  43. #weights is all weights. weights[i] is the weights for one layer.
  44. weights = []
  45. #iterate through all layers
  46. for layer_nr in range(LAYER_COUNT)[1:LAYER_COUNT]:
  47.  
  48.     #initialize weightmatrix for a given layer
  49.     weights.append([])
  50.  
  51.     #number of nodes from the previous layer is the rowcount of the weight matrix
  52.     #number of nodes from the current layer is the colcount
  53.     row_count = LAYER_SIZES[layer_nr-1]
  54.     col_count = LAYER_SIZES[layer_nr]
  55.  
  56.     layer_index = layer_nr-1
  57.  
  58.     #create weight matrix
  59.     for row_index in range(row_count):
  60.         weights[layer_index].append([])
  61.         for col_index in range(col_count):
  62.             #random_value can also be a vector
  63.             random_value = random.randint(-100,100)/100.0
  64.             weights[layer_index][row_index].append(random_value)
  65.  
  66.  
  67.  
  68. #forward_propagate. v is a vector from the input layer
  69. def compute(v):
  70.  
  71.     #start at index 1, because index 0 is the input v and therefore no need to calculate index 0
  72.     #that's why put v in interims
  73.     interims.append(v) #here v is a default non-numpy list at first
  74.     for layer_nr in range(LAYER_COUNT)[1:LAYER_COUNT]:
  75.  
  76.         #calculate; multiply the layers with the weights one by one
  77.         v = np.array(v).dot(weights[layer_nr-1]).tolist()
  78.         #v is the matrix of interims/output-results
  79.         interims.append(v)
  80.  
  81.         #now transform the computed_value according to the activation function
  82.         #iterate over all values in the matrix.
  83.         for j in range(len(v)):
  84.                 #sigmoid; produces soft curve
  85.                 exponent = -SIGMOID_EXPONENT*v[j]
  86.                 exponent = min(exponent,50)
  87.                 v[j] = 1/(1+2.72**(exponent))
  88.                 #linear
  89.                 #v[j] = min(max(v[j]+0.5,0),1)
  90.  
  91.     #v will now has the form of the output layer
  92.     #because the last matrix-multiplication (.dot()) with the last
  93.     #weight matrix will result in it
  94.     if len(v) != LAYER_SIZES[-1]:
  95.         print "ERROR, len(v) !=",LAYER_SIZES[-1]
  96.     return v
  97.  
  98.  
  99. def plot():
  100.  
  101.     plotcolor[0] = (plotcolor[0]+0.005)%1.0
  102.     lower_bound = min(np.array(T_DATA_IN).flat[:])-0.5
  103.     upper_bound = max(np.array(T_DATA_IN).flat[:])+0.5
  104.     step = 0.03 #set this to a lower value to get a smoother curve
  105.     input_value = lower_bound
  106.     x1 = []
  107.     x2 = []
  108.     while input_value < upper_bound:
  109.         computed_value = compute([input_value])
  110.         x2.append(computed_value)
  111.         x1.append(input_value)
  112.         input_value += step
  113.  
  114.     #result
  115.     plt.plot(x1,x2,
  116.             marker = '',
  117.             linestyle = '-',
  118.             color = pltclr.hsv_to_rgb(plotcolor))
  119.  
  120.     #after the first line has been plottet in black, put some color in for the next lines
  121.     plotcolor[1] = 1
  122.     plotcolor[2] = 1
  123.     #trainingdata
  124.  
  125.  
  126. #train the neural network
  127. max_iterations = MAX_ITERATIONS
  128. #initialize the error in a way which does not prevent the loop from running
  129. error = ERROR_THRESHOLD+1
  130. interims = [] #needed for the backpropagation algorithm
  131.  
  132. def root(a,b):
  133.     while b-1 > 0:
  134.         a = np.sqrt(a)
  135.         b -= 1
  136.     return a
  137.  
  138. print "training..."
  139. #stop when error is small enough or the computation takes too long
  140. while max_iterations > 0 and abs(error) > ERROR_THRESHOLD:
  141.     max_iterations -= 1
  142.     #iterate over the training data
  143.     error = 0
  144.     for i in range(len(T_DATA_IN)):
  145.  
  146.         #backpropagate
  147.  
  148.         target_value = T_DATA_OUT[i] #target for the NN to calculate
  149.         computed_value = T_DATA_IN[i] #initialize with the input, nothing computed yet
  150.         computed_value = compute(computed_value)
  151.  
  152.         for j in range(LAYER_SIZES[-1]):
  153.             error = computed_value[j]-target_value[j]
  154.  
  155.         #create shape of network in delta
  156.         delta = LAYER_SIZES[:] #[:]: make copy of content
  157.         for l in range(len(delta)):
  158.             delta[l] = np.zeros(delta[l]).tolist()
  159.  
  160.         #for each output node compute
  161.         j = -1
  162.         for k in range(LAYER_SIZES[j]):
  163.             old_output = interims[j][k]
  164.             delta[j][k] = (old_output * (1 - old_output) * (old_output - target_value[k]))
  165.             delta[j][k] = root(delta[j][k],1)
  166.  
  167.         #for each hidden node calculate
  168.         summ = 0
  169.         for i in range(LAYER_COUNT)[0:-1][::-1]: #delta for last layer is already calculated; start at the end
  170.             #i points to the current layer (index)
  171.             j = i+1 #point to the next layer
  172.             for n in range(LAYER_SIZES[i]): #iterate over nodes in layer i
  173.                 delta[i][n] = 1-interims[i][n]*(1-interims[i][n]) #the following sum is going to be applied to this:
  174.                 for k in range(LAYER_SIZES[j]): #iterate over nodes in layer i+1 (k in j)
  175.                     a = weights[i][n][k] #get weight that points from n to k
  176.                     b = delta[j][k]
  177.                     print b
  178.                     summ += a * b
  179.                 delta[i][n] *= summ #finish calculation of delta for this node, go to next node in layer i
  180.  
  181.         #now that i (hopefully) have the deltas, update the weights
  182.         for i in range(len(weights)):
  183.             for n in range(len(weights[i])):
  184.                 #weights[i][n] is an array. each element in it points to a single node in l+1
  185.                 dweight = LEARNING_RATE * delta[i][n] * interims[i][n]
  186.                 weights[i][n] = (np.array(weights[i][n]) - dweight).tolist() #substract dweight from every single weight
  187.  
  188.  
  189.         if i == 0: #set this to some number to log maybe useful information
  190.             print "output:",computed_value,"error:",error
  191.  
  192.     plot()
  193.  
  194. if max_iterations < 1:
  195.     print "training aborted because max_iterations was reached. Error did not converge to 0"
  196.  
  197. #show how NN classifies the training input now
  198. print "result:"
  199. for i in range(len(T_DATA_IN)):
  200.     print "input:",str(T_DATA_IN[i]),"output:",str(compute(T_DATA_IN[i]))
  201.  
  202. #training finished
  203. #do some matplotlib stuff to show the neural-network's function
  204.  
  205.  
  206. plot()
  207.  
  208. #plot the training-datapoints
  209. plt.plot(np.array(T_DATA_IN).flat[:],np.array(T_DATA_OUT).flat[:],
  210.         marker = 'o',
  211.         linestyle = '')
  212. plt.xlabel("input")
  213. plt.ylabel("forward-propagated")
  214. plt.title("training result graph")
  215. plt.margins(0.1)
  216. plt.show()
  217.  
  218. quit()
  219.  
  220. #now let the user use the trained network
  221.  
  222. print
  223. print "use 'q' to quit"
  224. print "input example:",str(np.ones(len(T_DATA_IN[0])).tolist())[1:-1]
  225.  
  226. while 1:
  227.     user_input = raw_input("your input: ")
  228.     if user_input == "q":
  229.         quit()
  230.  
  231.     #parse
  232.     input_value = np.fromstring(user_input,dtype=float,sep=',')
  233.     if len(input_value) != LAYER_SIZES[0]:
  234.         print "wrong input-format. This is probably because you entered a character that is not a number or ',' or your input vector has the wrong length"
  235.         continue
  236.  
  237.     #calculate. the computed_value has to be a vector []
  238.     computed_value = compute(input_value)
  239.     #for layer_nr in range(LAYER_COUNT)[1:LAYER_COUNT]:
  240.     #   computed_value = np.array(computed_value).dot(weights[layer_nr-1])
  241.  
  242.     print "result: "+str(computed_value)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement