Advertisement
Guest User

neural

a guest
Nov 22nd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2.  
  3. def sigmoid(x):
  4.     return 1 / (1+np.exp(-x))
  5.  
  6. tr_in = np.array([[0,0,1,0,0],
  7.                   [1,1,1,0,1],
  8.                   [1,0,1,1,1],
  9.                   [0,1,0,0,1],
  10.                   [1,1,1,1,1],
  11.                   [1,0,0,0,0],
  12.                   [0,0,0,0,0],
  13.                   [0,0,0,0,0],
  14.                   [0,1,0,0,0]])
  15.  
  16. tr_out = np.array([[0,1,1,0,1,0,0,0,0]]).T
  17.  
  18. np.random.seed(1)
  19.  
  20. syn_w = 2 * np.random.random((5,1)) - 1
  21.  
  22. print('Random weights')
  23. print(syn_w)
  24.  
  25. output_err = open('err.txt','w')
  26. output_adj = open('adj.txt','w')
  27.              
  28.  
  29. try:
  30.     out_w = open('weights2.nrl')
  31.     print('The weights is already exist!')
  32. except:
  33.     out_w = open('weights2.nrl','w')
  34.     for i in range(40000):
  35.         in_lyr = tr_in
  36.         out = sigmoid( np.dot(in_lyr, syn_w) )
  37.        
  38.         err = tr_out - out
  39.         adj = np.dot( in_lyr.T, err * (out * (1- out)) )
  40.        
  41.         syn_w += adj
  42.        
  43.         output_err.write(str(err))
  44.         output_adj.write(str(adj))
  45.     out_w.write(str(syn_w))
  46. finally:
  47.     output_err.close()
  48.     output_adj.close()
  49.                                      
  50. print( 'W after thinkin:' )
  51. print(syn_w)
  52.  
  53. print('Result after thinkin:')
  54. print('non') #add later
  55.  
  56. #test
  57. while True:
  58.     user_in = np.array([bool(input('First(0,1): ')),int(input('Second(0,1): ')),int(input()),int(input('Fourth(0,1): ')),int(input('Fifth(0,1): '))])
  59.     x = [] # массив
  60.     out_w_r = open('weights1.nrl','r')
  61.         # заполняем массив из файла с весами
  62.     for s in out_w_r.readlines():
  63.         x.append([float(s.replace('[', '').replace(']', ''))])
  64.      
  65.         # передаем массив с весами в функцию    
  66.     output = sigmoid(np.dot(user_in, x))
  67.     out_w.close()
  68.     out_w_r.close()
  69.     print('Result:')
  70.     print(output)
  71.      
  72.     if __name__ == '__main__':
  73.         print('main()')    
  74.     restart = str(input('Wanna restart?(any button/n)'))
  75.     if not restart == 'n' or 'N':
  76.         continue
  77.     else:
  78.         print('Goodbye')
  79.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement