-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbai15_Overfitting_WeightDecay.py
More file actions
161 lines (124 loc) · 4.05 KB
/
bai15_Overfitting_WeightDecay.py
File metadata and controls
161 lines (124 loc) · 4.05 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 3 23:41:50 2020
@author: phamk
"""
# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(4)
means = [[-1, -1], [1, -1], [0, 1]]
cov = [[1, 0], [0, 1]]
N = 20
X0 = np.random.multivariate_normal(means[0], cov, N)
X1 = np.random.multivariate_normal(means[1], cov, N)
X2 = np.random.multivariate_normal(means[2], cov, N)
X = np.concatenate((X0, X1, X2), axis = 0)
K = 3
original_label = np.asarray([0]*N + [1]*N + [2]*N).T
def kmeans_display(X, label):
K = np.amax(label) + 1
X0 = X[label == 0, :]
X1 = X[label == 1, :]
X2 = X[label == 2, :]
plt.plot(X0[:, 0], X0[:, 1], 'b^', markersize = 6, alpha = .8)
plt.plot(X1[:, 0], X1[:, 1], 'go', markersize = 6, alpha = .8)
plt.plot(X2[:, 0], X2[:, 1], 'rs', markersize = 6, alpha = .8)
# plt.axis('equal')
plt.plot()
# plt.show()
kmeans_display(X, original_label)
plt.show()
y = original_label.T
X = X.T
def softmax(V):
e_V = np.exp(V - np.max(V, axis = 0, keepdims = True))
Z = e_V / e_V.sum(axis = 0)
return Z
## One-hot coding
from scipy import sparse
def convert_labels(y, C = 3):
Y = sparse.coo_matrix((np.ones_like(y),
(y, np.arange(len(y)))), shape = (C, len(y))).toarray()
return Y
# cost or loss function
lam = 0.001 # regularization parameter
def cost(Y, Yhat, W1, W2, lam):
return -np.sum(Y*np.log(Yhat))/Y.shape[1] + \
lam*(np.linalg.norm(W1)**2 + np.linalg.norm(W2)**2)
d0 = 2
d1 = h = 100 # size of hidden layer
d2 = C = 3
def mynet(lam):
# initialize parameters randomely
W1 = 0.01*np.random.randn(d0, d1)
b1 = np.zeros((d1, 1))
W2 = 0.01*np.random.randn(d1, d2)
b2 = np.zeros((d2, 1))
# X = X.T # each column of X is a data point
Y = convert_labels(y, C)
N = X.shape[1]
eta = 1 # learning rate
for i in range(10000):
## Feedforward
Z1 = np.dot(W1.T, X) + b1
A1 = np.maximum(Z1, 0)
Z2 = np.dot(W2.T, A1) + b2
# import pdb; pdb.set_trace() # breakpoint 035ab9b5 //
Yhat = softmax(Z2)
# compute the loss: average cross-entropy loss
# print loss after each 1000 iterations
if i %1000 == 0:
loss = cost(Y, Yhat, W1, W2, lam)
print("iter %d, loss: %f" %(i, loss))
# backpropagation
E2 = (Yhat - Y )/N
dW2 = np.dot(A1, E2.T) + lam*W2
db2 = np.sum(E2, axis = 1, keepdims = True)
E1 = np.dot(W2, E2)
E1[Z1 <= 0] = 0 # gradient of ReLU
dW1 = np.dot(X, E1.T) + lam*W1
db1 = np.sum(E1, axis = 1, keepdims = True)
# Gradient Descent update
# import pdb; pdb.set_trace() # breakpoint 47741f63 //
W1 += -eta*dW1
b1 += -eta*db1
W2 += -eta*dW2
b2 += -eta*db2
# return (W1, W2, b1, b2)
Z1 = np.dot(W1.T, X) + b1
A1 = np.maximum(Z1, 0)
Z2 = np.dot(W2.T, A1) + b2
predicted_class = np.argmax(Z2, axis=0)
acc = (100*np.mean(predicted_class == y))
print('training accuracy: %.2f %%' % acc)
xm = np.arange(-3, 4, 0.025)
xlen = len(xm)
ym = np.arange(-4, 4, 0.025)
ylen = len(ym)
xx, yy = np.meshgrid(xm, ym)
print(np.ones((1, xx.size)).shape)
xx1 = xx.ravel().reshape(1, xx.size)
yy1 = yy.ravel().reshape(1, yy.size)
X0 = np.vstack((xx1, yy1))
Z1 = np.dot(W1.T, X0) + b1
A1 = np.maximum(Z1, 0)
Z2 = np.dot(W2.T, A1) + b2
# predicted class
Z = np.argmax(Z2, axis=0)
Z = Z.reshape(xx.shape)
CS = plt.contourf(xx, yy, Z, 200, cmap='jet', alpha = .1)
kmeans_display(X.T, original_label.T)
cur_axes = plt.gca()
cur_axes.axes.get_xaxis().set_ticks([])
cur_axes.axes.get_yaxis().set_ticks([])
plt.title('$\lambda =$' + str(lam), fontsize = 20)
fn = 'nnet_reg'+ str(lam) + '.png'
plt.savefig(fn, bbox_inches='tight', dpi = 600)
plt.show()
# mynet(0)
# mynet(0.1)
# mynet(0.01)
mynet(0.001)