-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
57 lines (44 loc) · 972 Bytes
/
perceptron.py
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
# -*- coding: utf-8 -*-
"""Perceptron.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1anegrfBEHVvbbJwHHNmom0Od1jfXx2lL
"""
import numpy as np
import pandas as pd
X=np.array([[-1,-1],[-1,1],[1,-1],[1,1]])
X
O=np.array([-1,-1,-1,1])
O=np.transpose(O)
O
# W=np.random.random([X.shape[1]])
# b=np.random.random(1)
W=np.array([1,0])
b=0
print(W,b)
sum=np.zeros(O.shape)
sum
def step(sum):
if (sum>0.5):
return 1
elif(sum<-0.5):
return -1
else:
return 0
n_epoch=5
learning_rate=1
for k in range (n_epoch):
c=0
for i in range(4):
sum[i] = step(X[i][0]*W[0] + X[i][1]*W[1] +b)
print(sum[i])
print(X[i])
print(O[i])
if(sum[i]!=O[i]):
print("HI")
W[0]=W[0]+learning_rate*O[i]*X[i][0]
W[1]=W[1]+learning_rate*O[i]*X[i][1]
b=b+learning_rate*O[i]
print(b)
W
b