-
Notifications
You must be signed in to change notification settings - Fork 0
/
or_gate.py
45 lines (33 loc) · 1.29 KB
/
or_gate.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
# -*- coding: utf-8 -*-
"""Untitled0.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Ug-CoBTvYQ3sm2h9yBDcI8S_C_lm-NZU
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
NUM_FEATURES = 2
NUM_ITER = 2000
learning_rate = 0.01
x = np.array([[0, 0], [1, 0], [1, 1], [0, 1]], np.float32) # 4x2, input
y = np.array([0, 1, 1, 1], np.float32) # 4, correct output, OR operation
W = np.zeros(NUM_FEATURES, np.float32) # 2x1, weight
b = np.zeros(1, np.float32) # 1x1
for k in range(NUM_ITER):
yHat = x.dot(W) + b
yHat = 1.0 / (1.0 + np.exp(-yHat))
err = y - yHat
deltaW = np.transpose(x).dot(err) # have to 2x1
deltaB = np.sum(err) # have to 1x1. collect error from all the 4 samples
W = W + learning_rate * deltaW # if err = y - yHat, then W = W + lRate * deltW
b = b + learning_rate * deltaB
plot_x = np.array([np.min(x[:, 0] - 0.2), np.max(x[:, 1]+0.2)])
plot_y = - 1 / W[1] * (W[0] * plot_x + b) # comes from, w0*x + w1*y + b = 0 then y = (-1/w1) (w0*x + b)
print('W:' + str(W))
print('b:' + str(b))
print('plot_y: '+ str(plot_y))
plt.scatter(x[:, 0], x[:, 1], c=y, s=100, cmap='viridis')
plt.plot(plot_x, plot_y, color='k', linewidth=2)
plt.xlim([-0.2, 1.2]); plt.ylim([-0.2, 1.25]);
plt.show()