In [ ]:
import numpy as np
from utils import make_classification, draw_decision_boundary, sigmoid
from sklearn.metrics import accuracy_score
from theano import tensor as T
from theano import function, shared
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rc('figure', figsize=(8, 6))
%matplotlib inline
In [ ]:
X, Y = make_classification()
W = np.random.rand(2, 1)
B = np.random.rand(1,)
In [ ]:
draw_decision_boundary(W.ravel().tolist() + [B[0]], X, Y)
In [ ]:
# activation
Z = np.dot(X, W) + B
# prediction
Y_pred = sigmoid(Z)
\frac{\partial{\mathbf{E}}}{\partial{\mathbf{y}}} = -(\mathbf{t} - \mathbf{y})$$
In [ ]:
def predict(X, weights, bias=None):
if bias is not None:
z = np.dot(X, weights) + bias
else:
z = np.dot(X, weights)
return sigmoid(z)
def train(X, Y, weights, alpha=0.3):
y_hat = predict(X, weights)
_gw = -1 * (Y - y_hat) * y_hat * (1 - y_hat)
_gw = np.repeat(_gw, X.shape[1], axis=1)
weights -= (alpha * _gw * X).sum(0).reshape(-1, 1)
return weights
def loss(y1, y2):
return (0.5 * ((y1 - y2) ** 2)).sum()
In [ ]:
for i in range(10000):
y_hat = predict(X, W)
W = train(X, Y, W)
if i % 1000 == 0:
print("Loss: ", loss(Y, y_hat))
In [ ]:
draw_decision_boundary(W.ravel().tolist() + [B[0]], X, Y)
In [ ]:
# enter code here