Created
May 18, 2015 09:48
-
-
Save Moddus/356af259c26e460a8f80 to your computer and use it in GitHub Desktop.
Revisions
-
Moddus created this gist
May 18, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ import numpy as np class Perzeptron: def __init__(self, t, my, d, maxiter, sign): self.t = t self.my = my self.maxiter = maxiter self.sign = sign self.w = np.mat([[-self.t],[1],[1]]) self.d = d def delta(self, x, d): return self.my*(d-self.calculate(x)) def calculate(self, x): return self.sign(float(self.w.T * x)) def training(self, data): itercount = 0 while(self.maxiter != itercount): itercount += 1 for i in range(data.shape[0]): xi = data[i].T delta = self.delta(xi, self.d[i]) self.w += delta * xi def lineare(x): c = 2 r = 0 if x > c: r = 1 elif x < -c: r = 0 else: r = 1/(2*c)*(x+c) return r def fermi(x): return 1 / (1 + np.exp(-x)) def tanh(x): return (1 + np.tanh(x))/2 def hart(x): if x >= 0: return 1 return 0 This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ #!/usr/bin/env python3 from perzeptron import * data = np.mat([[1, 0, 0],[1, 0, 1], [1, 1, 0], [1, 1, 1]]) result_OR = np.array([0, 1, 1, 1]) result_AND = np.array([0, 0, 0, 1]) result_XOR = np.array([0, 1, 1, 0]) p = Perzeptron(0.5, 0.1, result_OR, 10000, lineare) p.training(data) print(p.calculate(data[0].T)) print(p.calculate(data[1].T)) print(p.calculate(data[2].T)) print(p.calculate(data[3].T)) print(p.w) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,34 @@ import numpy as py from perzeptron import * data = np.mat([[1, 0, 0],[1, 0, 1], [1, 1, 0], [1, 1, 1]]) result_OR = np.array([0, 1, 1, 1]) result_AND = np.array([0, 0, 0, 1]) result_XOR = np.array([0, 1, 1, 0]) t = 0.5 my = 0.1 iter = 1000 eps = 0.01 def isBetween(v,r): return v >= r - eps and v <= r + eps def test_or_perzeptron(): p = Perzeptron(t, my, result_OR, iter, lineare) p.training(data) assert isBetween(p.calculate(data[0].T), 0) == True assert isBetween(p.calculate(data[1].T), 1) == True assert isBetween(p.calculate(data[2].T), 1) == True assert isBetween(p.calculate(data[3].T), 1) == True def test_and_perzeptron(): p = Perzeptron(t, my, result_AND, iter, lineare) p.training(data) assert isBetween(p.calculate(data[0].T), 0) == True assert isBetween(p.calculate(data[1].T), 0) == True assert isBetween(p.calculate(data[2].T), 0) == True assert isBetween(p.calculate(data[3].T), 1) == True