Advertisement
Guest User

properneuralnet.py

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. import numpy as np
  2. from scipy.stats import truncnorm
  3.  
  4. @np.vectorize
  5. def ReLU(diff, x):
  6.     if diff:
  7.         out = 0 if x <= 0 else 1   
  8.     else:
  9.         out = np.maximum(0.0, x)
  10.     return out
  11.  
  12. @np.vectorize
  13.    
  14. def sigmoid(diff, x):
  15.     func = lambda a : 1/(1 + np.e ** -a)
  16.     inv_func = lambda a : np.log( a / (1 - a) )
  17.     if diff:
  18.         y = inv_func(x)
  19.         out = func(y) * (1 - func(y))
  20.     else:
  21.         out = func(x)
  22.     return out
  23.    
  24.  
  25. def truncated_normal(mean, sd, low, upp):
  26.     return truncnorm(
  27.         (low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)
  28.  
  29. class NeuralNet():
  30.     def __init__(self, nodes, learning_rate, act_func, bias):
  31.        
  32.         self.nodes = nodes
  33.         self.learning_rate = learning_rate
  34.         self.act_func = act_func
  35.         self.bias = bias
  36.         self.gen_rand_weight_matricies()
  37.        
  38.     def gen_rand_weight_matricies(self):
  39.         bias_node = 1 if self.bias else 0
  40.         self.weight_dict = {}
  41.         for i in range(0, (self.nodes.size - 1)):
  42.             self.weight_dict[i] = self.rnd_weights(self.nodes[i], self.nodes[i + 1], bias_node)
  43.        
  44.        
  45.    
  46.     def rnd_weights(self, nd_1, nd_2, bias_node):
  47.         ran = 1/np.sqrt(nd_1 + bias_node)
  48.         dist = truncated_normal(mean=0, sd=1, low=-ran, upp = ran )
  49.         return dist.rvs((nd_2, nd_1 + bias_node))
  50.    
  51.     def convert_vec(self, vec):
  52.         return np.array(vec, ndmin=2).T
  53.    
  54.     def train(self, input_vec, target_vec):
  55.         if self.bias:
  56.             bias_node = 1
  57.             input_vec = np.concatenate( (input_vec, [1]) )
  58.         else:
  59.             bias_node = 0
  60.         input_vec, target_vec = self.convert_vec(input_vec), self.convert_vec(target_vec)
  61.         self.train_dict = {}
  62.         val = self.nodes.size - 1 #3
  63.         self.train_dict[val]= input_vec
  64.         for key in self.weight_dict:
  65.             i = val - (key + 1)
  66.             self.train_dict[i] = self.act_func(False, np.dot(self.weight_dict[key], self.train_dict[i+1]))
  67.             if self.bias:
  68.                 self.train_dict[i] = np.concatenate( (self.train_dict[i], [1] ) )
  69.         output_errors = target_vec - self.train_dict[0]
  70.         val = self.nodes.size - 1
  71.         for key in self.weight_dict:
  72.             i = val - (key + 1)
  73.             """
  74.             print("layer {0}".format(i))
  75.             print("output errors: {0}".format(output_errors))
  76.             print("input vectors {0}".format(self.train_dict[key]))
  77.             a = input("")
  78.             """
  79.             tmp = output_errors * self.act_func(True, self.train_dict[key])
  80.             if (self.bias and key > 0):
  81.                 x = self.learning_rate * np.dot(tmp, self.train_dict[key+1].T)[:-1,:]
  82.             else:
  83.                 x = self.learning_rate * np.dot(tmp,  self.train_dict[key+1].T)
  84.             self.weight_dict[i] += x
  85.             #print(self.weight_dict[i])
  86.             #print(output_errors)
  87.             #a = input("")
  88.             output_errors = np.dot(self.weight_dict[i].T, output_errors)
  89.        
  90.     def run(self, input_vec):
  91.         if self.bias:
  92.             input_vec = np.concatenate( (input_vec, [1] ))
  93.         temp_vec = self.convert_vec(input_vec)
  94.         #print(input_vec)
  95.         for key in self.weight_dict:
  96.             #print(self.weight_dict[key])
  97.             temp_vec = self.act_func(False, np.dot(self.weight_dict[key], temp_vec))
  98.             if self.bias:
  99.                 temp_vec = np.concatenate( (temp_vec, [1]) )
  100.             #print(temp_vec)
  101.         return temp_vec
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement