Advertisement
Guest User

Untitled

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