forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogistic_train.py
63 lines (48 loc) · 1.44 KB
/
logistic_train.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
57
58
59
60
61
import numpy as np
import matplotlib.pyplot as plt
from sklearn.utils import shuffle
from process import get_binary_data
X, Y = get_binary_data()
X, Y = shuffle(X, Y)
# create train and test sets
Xtrain = X[:-100]
Ytrain = Y[:-100]
Xtest = X[-100:]
Ytest = Y[-100:]
# randomly initialize weights
D = X.shape[1]
W = np.random.randn(D)
b = 0 # bias term
# make predictions
def sigmoid(a):
return 1 / (1 + np.exp(-a))
def forward(X, W, b):
return sigmoid(X.dot(W) + b)
# calculate the accuracy
def classification_rate(Y, P):
return np.mean(Y == P)
# cross entropy
def cross_entropy(T, pY):
return -np.mean(T*np.log(pY) + (1 - T)*np.log(1 - pY))
# train loop
train_costs = []
test_costs = []
learning_rate = 0.001
for i in xrange(10000):
pYtrain = forward(Xtrain, W, b)
pYtest = forward(Xtest, W, b)
ctrain = cross_entropy(Ytrain, pYtrain)
ctest = cross_entropy(Ytest, pYtest)
train_costs.append(ctrain)
test_costs.append(ctest)
# gradient descent
W -= learning_rate*Xtrain.T.dot(pYtrain - Ytrain)
b -= learning_rate*(pYtrain - Ytrain).sum()
if i % 1000 == 0:
print i, ctrain, ctest
print "Final train classification_rate:", classification_rate(Ytrain, np.round(pYtrain))
print "Final test classification_rate:", classification_rate(Ytest, np.round(pYtest))
legend1, = plt.plot(train_costs, label='train cost')
legend2, = plt.plot(test_costs, label='test cost')
plt.legend([legend1, legend2])
plt.show()