forked from EricSchles/neuralnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirst.py
More file actions
63 lines (52 loc) · 1.63 KB
/
first.py
File metadata and controls
63 lines (52 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#This comes from http://natureofcode.com/book/chapter-10-neural-networks/
import random
class Perceptron:
def __init__(self,num_inputs):
self.weight_range = self.prange(-1,1,0.001)
self.weights = self.weight_gen(num_inputs)
def weight_gen(self,n):
result = []
for i in xrange(n):
result.append(self.weight_range[random.randint(0,len(self.weight_range)-1)])
return result
def prange(self,start,stop,step):
vals = []
r = start
while r < stop:
r = round(r,5)
vals.append(r)
r += step
return vals
#activation function
def sign_filter(self,n):
if n >0:
return 1
else:
return 0
def feedforward(self,inputs):
summa = 0
for ind,val in enumerate(inputs):
summa += self.weights[ind]*val
return self.sign_filter(summa)
def train(self,inputs,desired):
constant = 0.01
guess = self.feedforward(inputs)
error = desired - guess
for ind,val in enumerate(inputs):
self.weights[ind] += constant * error * val
def f(x):
return 2*x+1
p = Perceptron(3)
training = []
for i in xrange(2000):
training.append([random.randint(-100,100),random.randint(-100,100),1,1])
if training[i][1] < f(training[i][0]):
training[i][3] = -1
index = random.randint(0,len(training)-1)
p.train(training[index][:3],training[index][3])
for ind,val in enumerate(training):
guess = p.feedforward(val[:3])
if guess == 0:
print "less than"0
else:
print "greater than"