PT21-22-Reseau-Neurones/tests/testLearningNAND.py

68 lines
1.4 KiB
Python
Raw Normal View History

2021-12-18 20:57:44 +01:00
import numpy as np
import random
2021-12-22 21:35:06 +01:00
import time
2021-12-22 22:08:20 +01:00
from sys import path
path.insert(1, "..")
from sobek.network import network
2021-12-18 20:57:44 +01:00
random.seed()
2021-12-22 21:35:06 +01:00
myNetwork = network(2, 2, 1)
2021-12-18 20:57:44 +01:00
learningRate = 3
test = []
result = []
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test[1][1] = 1.0
test[2][0] = 1.0
test[3][0] = 1.0
test[3][1] = 1.0
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.zeros(1))
2021-12-22 21:35:06 +01:00
learningTime = 0
2021-12-18 20:57:44 +01:00
2021-12-22 21:35:06 +01:00
nbRep = 1
2021-12-18 20:57:44 +01:00
2021-12-22 21:35:06 +01:00
for i in range(nbRep):
if (i%(nbRep/10) == 0): print(i)
2021-12-18 20:57:44 +01:00
2021-12-22 21:35:06 +01:00
startTime = time.perf_counter()
myNetwork.train(test, result, learningRate, len(test), 10000, visualize=False)
endTime = time.perf_counter()
learningTime += endTime - startTime
learningTime = learningTime / nbRep
2021-12-22 22:08:20 +01:00
2021-12-18 20:57:44 +01:00
test = []
result = []
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test.append(np.zeros(2))
test[1][1] = 1.0
test[2][0] = 1.0
test[3][0] = 1.0
test[3][1] = 1.0
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.ones(1))
result.append(np.zeros(1))
2021-12-22 21:35:06 +01:00
#print(myNetwork.weights)
#print(myNetwork.biases)
2021-12-18 20:57:44 +01:00
print("0 0 : " + str(myNetwork.process(test[0])) + " == 1 ?")
print("0 1 : " + str(myNetwork.process(test[1])) + " == 1 ?")
print("1 0 : " + str(myNetwork.process(test[2])) + " == 1 ?")
2021-12-22 21:35:06 +01:00
print("1 1 : " + str(myNetwork.process(test[3])) + " == 0 ?")
myNetwork.saveToFile("NAND")
print("Learning time : " + str(endTime - startTime))