float(string[0]) / 10.0, float(string[1]) / 10.0, float(string[2]) / 10.0, float(string[3]) / 10.0 ]] if string[4].rstrip() == "Iris-setosa": x += [[1]] else: x += [[0]] testSet += [x] trainingFile.close() testFile.close() #TRAINING nn.backPropagation(trainingSet, n, 4, nhidden, 1) #TESTING p = 0 for x, t in testSet: O = nn.evaluate(x) if ((1 - O[0]) < 0.1): if (t[0] == 1): p += 1 else: if (t[0] == 0): p += 1 print(str(p) + " out of " + str(len(testSet)) + " tests")
else: x += [[0]] testSet += [x] trainingFile.close() testFile.close() errorPlot = [] mediaX = [] mediaY = [] #TRAINING if len(sys.argv) > 6 and sys.argv[6] == ".": nvalues =[0.5, 0.3, 0.1, 0.05, 0.01] for i in range(5): for j in range(5): nn.backPropagation(trainingSet, nvalues[i] , 2, nhidden, 1) if (mediaY == []): mediaY = numpy.zeros(len(nn.errorX)) mediaX = nn.errorX mediaY = sumaVector(mediaY, nn.errorY) mediaY = [y/5 for y in mediaY] errorPlot += [[mediaX,mediaY]] print(">>"+str(len(errorPlot))) i = 0 for x,y in errorPlot: print(">>") print (x) print (y) plt.plot(x, y, '-', label=str(nvalues[i])) i+=1
from nn import NN nn = NN() train = [[[0, 0], [0]], [[0, 1], [1]], [[1, 0], [1]], [[1, 1], [0]]] for x in train: print(x) nn.backPropagation(train, 0.5, 2, 3, 1) print(nn.evaluate([0, 0])) print(nn.evaluate([0, 1])) print(nn.evaluate([1, 0])) print(nn.evaluate([1, 1]))
else: x += [[0]] testSet += [x] trainingFile.close() testFile.close() errorPlot = [] mediaX = [] mediaY = [] #TRAINING if len(sys.argv) > 6 and sys.argv[6] == ".": nvalues = [0.5, 0.3, 0.1, 0.05, 0.01] for i in range(5): for j in range(5): nn.backPropagation(trainingSet, nvalues[i], 2, nhidden, 1) if (mediaY == []): mediaY = numpy.zeros(len(nn.errorX)) mediaX = nn.errorX mediaY = sumaVector(mediaY, nn.errorY) mediaY = [y / 5 for y in mediaY] errorPlot += [[mediaX, mediaY]] print(">>" + str(len(errorPlot))) i = 0 for x, y in errorPlot: print(">>") print(x) print(y) plt.plot(x, y, '-', label=str(nvalues[i])) i += 1
#TEST SET for line in testFile: string = line.split(",", 5) x = [[float(string[0])/10.0,float(string[1])/10.0,float(string[2])/10.0,float(string[3])/10.0]] if string[4].rstrip() == "Iris-setosa": x += [[1]] else: x += [[0]] testSet += [x] trainingFile.close() testFile.close() #TRAINING nn.backPropagation(trainingSet, n, 4, nhidden, 1) #TESTING p = 0 for x,t in testSet: O = nn.evaluate(x) if((1 - O[0]) < 0.1): if (t[0] == 1): p+=1 else: if (t[0] == 0): p+=1 print(str(p)+" out of "+str(len(testSet))+" tests")