Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def sigmoid(x):
- return 1 / (1+np.exp(-x))
- tr_in = np.array([[0,0,1,0,0],
- [1,1,1,0,1],
- [1,0,1,1,1],
- [0,1,0,0,1],
- [1,1,1,1,1],
- [1,0,0,0,0],
- [0,0,0,0,0],
- [0,0,0,0,0],
- [0,1,0,0,0]])
- tr_out = np.array([[0,1,1,0,1,0,0,0,0]]).T
- np.random.seed(1)
- syn_w = 2 * np.random.random((5,1)) - 1
- print('Random weights')
- print(syn_w)
- output_err = open('err.txt','w')
- output_adj = open('adj.txt','w')
- try:
- out_w = open('weights2.nrl')
- print('The weights is already exist!')
- except:
- out_w = open('weights2.nrl','w')
- for i in range(40000):
- in_lyr = tr_in
- out = sigmoid( np.dot(in_lyr, syn_w) )
- err = tr_out - out
- adj = np.dot( in_lyr.T, err * (out * (1- out)) )
- syn_w += adj
- output_err.write(str(err))
- output_adj.write(str(adj))
- out_w.write(str(syn_w))
- finally:
- output_err.close()
- output_adj.close()
- print( 'W after thinkin:' )
- print(syn_w)
- print('Result after thinkin:')
- print('non') #add later
- #test
- while True:
- 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): '))])
- x = [] # массив
- out_w_r = open('weights1.nrl','r')
- # заполняем массив из файла с весами
- for s in out_w_r.readlines():
- x.append([float(s.replace('[', '').replace(']', ''))])
- # передаем массив с весами в функцию
- output = sigmoid(np.dot(user_in, x))
- out_w.close()
- out_w_r.close()
- print('Result:')
- print(output)
- if __name__ == '__main__':
- print('main()')
- restart = str(input('Wanna restart?(any button/n)'))
- if not restart == 'n' or 'N':
- continue
- else:
- print('Goodbye')
- break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement