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))
- def main():
- 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]])
- tr_out = np.array([[0,1,1,0,1,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('weights.nrl')
- print('The weights is already exist!')
- except:
- out_w = open('weights.nrl','w')
- for i in range(200000):
- 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
- 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): '))])
- x = [] # массив
- # заполняем массив из файла с весами
- for s in out_w.readlines():
- x.append([float(s.replace('[', '').replace(']', ''))])
- # передаем массив с весами в функцию
- output = sigmoid(np.dot(user_in, x))
- out_w.close()
- print('Result:')
- print(output)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement