Noron Thi
Noron Thi
Noron Thi
__future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(0)
X = np.random.rand(5000, 1)*20-10
y = 2 + 5 * X +.2*np.random.randn(5000,1)
one = np.ones((X.shape[0],1))
Xbar = np.concatenate((one,X),axis=1)
def grad(w):
N=Xbar.shape[0]
return 1/N*Xbar.T.dot(Xbar.dot(w)-y)
def cost(w):
N=Xbar.shape[0]
return .5/N*np.linalg.norm(y-Xbar.dot(w),2)**2
def numerical_grad(w, cost):
eps=1e-4
g=np.zeros_like(w)
for i in range(len(w)):
w_p=w.copy()
w_n=w.copy()
w_p[i]+=eps
w_n[i]-=eps
g[i]=(cost(w_p)-cost(w_n))/(2*eps)
return g
def check_grad(w,cost,grad):
w=np.random.rand(w.shape[0],w.shape[1])
grad1=grad(w)
grad2=numerical_grad(w,cost)
return True if np.linalg.norm(grad1-grad2)<1e-6 else False
print('Checking gradient...',check_grad(np.random.rand(2,1),cost,grad))
def myGD(w_init,grad,eta):
w=[w_init]
for it in range(810):
w_new=w[-1]-eta*grad(w[-1])
if np.linalg.norm(grad(w_new))/len(w_new)<1e-3:
break
w.append(w_new)
return(w,it)
w_init=np.array([[5],[2]])
(w1,it1)=myGD(w_init,grad,.01)
print('Solution found by GD=',w1[-1].T,',\nafter %d iterations.'%(it1+1))
w=w1[-1]
w_0=w[0][0]
w_1=w[1][0]
x0=np.linspace(-5,5,2,endpoint=True)
y0=w_0+w_1*x0
f1=plt.figure(figsize=(40,12))
plt.title('y=%.3fx + %.3f'%(w_1,w_0))
plt.plot(X.T,y.T,'b.')
plt.plot(x0,y0,'y',linewidth=2)
plt.axis([-10,10,-5,5])
plt.show()
f1.savefig('linear_regression_2.pdf', bbox_inches='tight', dpi = 600)
from __future__ import division, print_function, unicode_literals
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
np.random.seed(2)
np.random.seed(2)
X = np.random.rand(10000, 1)*16-8
y = 5 + 4 * X +.2*np.random.randn(10000,1)
one = np.ones((X.shape[0],1))
Xbar = np.concatenate((one,X),axis=1)
A = np.dot(Xbar.T,Xbar)
b = np.dot(Xbar.T,y)
w_lr = np.dot(np.linalg.pinv(A),b)
print('Solution found by formula: w= ',w_lr.T)
w=w_lr
w_0=w[0][0]
w_1=w[1][0]
x0=np.linspace(-8,8,2,endpoint=True)
y0=w_0+w_1*x0
f1=plt.figure(figsize=(10,12))
plt.title('y=%.3fx + %.3f'%(w_1,w_0))
plt.plot(X.T,y.T,'b.')
plt.plot(x0,y0,'y',linewidth=2)
plt.axis([-8,8,-10,10])
plt.show()
f1.savefig('linear_regression2.pdf', bbox_inches='tight', dpi = 600)
Cách1:
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def grad(x):#by NTĐ
return 4*x-8 - 3*np.sin(x) + 3*np.cos(x)
def cost(x):
return 2*(x-2)**2 + 3*x*np.cos(x)
def komomentum(eta, x0):
x = [x0]
for it in range(100):
x_new = x[-1] - eta*grad(x[-1])
if abs(grad(x_new)) < 1e-3:
break
x.append(x_new)
return (x,it)
(x1, it1) = komomentum(.03, 0.9)
print('Solution x1 = %f, cost =%f, obtained after %d iterations'%(x1[-1],
cost(x1[-1]), it1))
def draw_gdld(x1, ids, filename, nrows = 2, ncols = 3, start= -5.5):
x0 = np.linspace(start, 5.5, 1000)
y0 = cost(x0)
width = 4*ncols
height = 4*nrows
plt.close('all')
fig,axs=plt.subplots(nrows, ncols, figsize=(width, height))
with PdfPages(filename) as pdf:
for i, k in enumerate (ids):
r = i//ncols
c = i%ncols
x = x1[ids[i]]
y = cost (x)
str0 = 'iter {}/{}, grad = {:.3f}'.format(ids[i], len(x1)-1, g
rad(x))
axs[r, c].plot(x0, y0,'b')
axs[r, c].set_xlabel (str0, fontsize = 13)
axs[r, c].plot(x, y, 'r*', markersize = 8, markeredgecolor = '
k')
axs[r, c].plot()
axs[r, c].tick_params(axis='both', which='major', labelsize=13
)
pdf.savefig(bbox_inches='tight' )
plt.show()
filename = '12.pdf'
ids = [1, 3, 5, 7, 9, 15]
draw_gdld(x1, ids, filename, nrows = 2, ncols = 3, start= -5.5)
Cách2:
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
s=(1,3,5,7,9,15)
def grad(x):
return 4*x-8 - 3*np.sin(x) + 3*np.cos(x)
def cost(x):
return 2*(x-2)**2 + 3*x*np.cos(x)
def myGD1(eta, x0):
x=[x0]
for it in range(100):
x_new=x[-1]-eta*grad(x[-1])
if abs(grad(x_new))<1e-3:
break
x.append(x_new)
return (x,it)
(x1,it1)=myGD1(.03,6)
print('Solution x1 = %f, cost = %f, obtained after %d iterations'%(x1[-
1],cost(x1[-1]),it1))
x=np.arange(-10.0,10.0,0.1)
y=cost(x)
f1=plt.figure(figsize=(12,12))
for i in range(len(s)):
plt.subplot(2,3,i+1)
plt.title(r'$f(x)=2(x-2)^2+3xcos(x);x_0=6;\eta=0.03;Vong lap %d/%d$'%
(s[i],it1))
plt.plot(x,y,'b-')
plt.plot(x1[s[i]],cost(x1[s[i]]),'ro')
plt.show()
f1.savefig('Cau2')
Cách 1:
# from _future_ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def grad(x):
return 5*(2*x-4)*np.sin(x) + 5*(x**2-4*x+4)*np.cos(x) + 2
def cost(x):
return 5*(x-2)**2*np.sin(x) + 2*x
def has_converged(theta_new, grad):
return np.linalg.norm(grad(theta_new))/np.array(theta_new).size < 1e-3
def GD_momentum(theta_init,grad, eta, gamma):
theta = [theta_init]
v_old = np.zeros_like(theta_init)
for it in range(100):
v_new = gamma*v_old + eta*grad(theta[-1])
theta_new = theta[-1] - v_new
theta.append(theta_new)
if has_converged(theta_new, grad):
break
v_old = v_new
return theta
theta = GD_momentum(5,grad, 0.01, 0.8)
def draw_gdld(theta, ids, filename, nrows = 2, ncols = 3, start= -5.5):
x0 = np.linspace(start, 5.5, 1000)
y0 = cost(x0)
width = 4*ncols
height = 4*nrows
plt.close('all')
fig,axs=plt.subplots(nrows, ncols, figsize=(width, height))
with PdfPages(filename) as pdf:#byNTĐ
for i, k in enumerate (ids):
r = i//ncols
c = i%ncols
x = theta[ids[i]]
y = cost (x)
str0 = 'iter {}/{}, grad = {:.3f}'.format(ids[i], len(theta) -
1 , grad(x))
axs[r, c].plot(x0, y0,'b')
axs[r, c].set_xlabel (str0, fontsize = 13)
axs[r, c].plot(x, y, 'r*', markersize = 8, markeredgecolor = '
k')
axs[r, c].plot()
axs[r, c].tick_params(axis='both', which='major', labelsize=13
)
pdf.savefig(bbox_inches='tight' )
plt.show()
filename = '12.pdf'
ids = [2, 3, 5, 8, 10, 13]
draw_gdld(theta, ids, filename, nrows = 2, ncols = 3, start= -5.5)
#byNTĐ
Cách 2:
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
def grad(x):
return 5*(2*x-4)*np.sin(x) + 5*(x**2-4*x+4)*np.cos(x) + 2
def cost(x):
return 5*(x-2)**2*np.sin(x) + 2*x
def GD_momentum(theta_init, grad, eta, gamma):
theta=[theta_init]
v_old=np.zeros_like(theta_init)
for it in range(100):
v_new = gamma*v_old+eta*grad(theta[-1]-gamma*v_old)
theta_new=theta[-1]-v_new
if np.linalg.norm(grad(theta_new))/len(theta_new) < 1e-4:
break
theta.append(theta_new)
v_old=v_new
return theta,it
theta_init = np.array([3])
eta=.01 #toc do hoc
gamma=.8 #momentum
theta1,it1=GD_momentum(theta_init,grad,eta,gamma)
print(theta1[-1])
x=np.arange(-10.0,10.0,0.1)
y=cost(x)
s=(2,3,5,8,10,13)
theta_cost1=np.zeros_like(theta1)
theta_grad1=np.zeros_like(theta1)
for i in range(len(s)):
theta_cost1[s[i]]=cost(theta1[s[i]])
theta_grad1[s[i]]=grad(theta1[s[i]])
f1=plt.figure(figsize=(40,12))
for i in range(len(s)):
plt.subplot(2,3,i+1)
plt.plot(x,y,'b-')
plt.plot(theta1[int(s[i])],theta_cost1[int(s[i])],'ro')
plt.title(r'$f(x)=5(x-2)^2sin(x)+2x;x_0=-8;\eta=%.2f;\gamma=%.1f$'%
(eta,gamma))
plt.xlabel('iter %d/%d, grad=%.3f, cost=%.3f'%
(s[i],it1,theta_grad1[int(s[i])],theta_cost1[int(s[i])]))
plt.axis([-1,10,-50,300])
f1.subplots_adjust(hspace=0.33)
plt.show()
f1.savefig('cau2.pdf')
import numpy as np
import scipy
from scipy import sparse
import matplotlib.pyplot as plt
np.random.seed(3)
means = [[1, 5], [2, 8], [3, 9]]
cov = [[10, 0], [0, 10]]
N = 500
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).T
X = np.concatenate((np.ones((1, 3*N)), X), axis = 0)
C = 3
y = np.asarray([0]*N + [1]*N + [2]*N).T
def convert_labels(y, C = C):
Y = sparse.coo_matrix((np.ones_like(y),
(y, np.arange(len(y)))), shape = (C, len(y))).toarray()
return Y
# Kiem tra dao ham so sánh gradient tính được với numeric gradient
def cost(X, Y, W):
A = softmax(W.T.dot(X))
return -np.sum(Y*np.log(A))
def grad(X, Y, W):
A = softmax((W.T.dot(X)))
E = A - Y
return X.dot(E.T)
def softmax(Z):
e_Z = np.exp(Z)
A = e_Z / e_Z.sum(axis = 0)
return A
def softmax_stable(Z):
e_Z = np.exp(Z - np.max(Z, axis = 0, keepdims = True))
A = e_Z / e_Z.sum(axis = 0)
return A
def numerical_grad(X, Y, W, cost):
eps = 1e-6
g = np.zeros_like(W)
for i in range(W.shape[0]):
for j in range(W.shape[1]):
W_p = W.copy()
W_n = W.copy()
W_p[i, j] += eps
W_n[i, j] -= eps
g[i,j] = (cost(X, Y, W_p) - cost(X, Y, W_n))/(2*eps)
return g
def softmax_regression(X, y, W_init, eta, tol = 1e-4, max_count = 10000):
W = [W_init]
C = W_init.shape[1]
Y = convert_labels(y, C)
it = 0
N = X.shape[1]
d = X.shape[0]
count = 0
check_w_after = 20
while count < max_count:
# mix data
mix_id = np.random.permutation(N)
for i in mix_id:
xi = X[:, i].reshape(d, 1)
yi = Y[:, i].reshape(C, 1)
ai = softmax(np.dot(W[-1].T, xi))
W_new = W[-1] + eta*xi.dot((yi - ai).T)
count += 1
# stopping criteria
if count%check_w_after == 0:
if np.linalg.norm(W_new - W[-check_w_after]) < tol:
return W
W.append(W_new)
return W
def pred(W, X):
A = softmax_stable(W.T.dot(X))
return np.argmax(A, axis = 0)
def display(X, label):
X0 = X[:, label == 0]
X1 = X[:, label == 1]
X2 = X[:, label == 2]
plt.plot(X0[0, :], X0[1, :], 'b^', markersize = 4, alpha = .8)
plt.plot(X1[0, :], X1[1, :], 'go', markersize = 4, alpha = .8)
plt.plot(X2[0, :], X2[1, :], 'rs', markersize = 4, alpha = .8)
plt.axis('off')
Y = convert_labels(y, C)
W_init = np.random.randn(X.shape[0], C)
g1 = grad(X, Y, W_init)
g2 = numerical_grad(X, Y, W_init, cost)
eta = .05
W = softmax_regression(X, y, W_init, eta)
xm = np.arange(min(X[1]-.5), max(X[1]+.5), 0.025)
xlen = len(xm)
ym = np.arange(min(X[2]-.5), max(X[2]+.5), 0.025)
ylen = len(ym)
xx, yy = np.meshgrid(xm, ym)
xx1 = xx.ravel().reshape(1, xx.size)
yy1 = yy.ravel().reshape(1, yy.size)
XX = np.concatenate((np.ones((1, xx.size)), xx1, yy1), axis = 0)
Z = pred(W[-1], XX)
Z = Z.reshape(xx.shape)
plt.figure(figsize=(40,12))
CS = plt.contourf(xx, yy, Z, 200, cmap='jet', alpha = .2)
plt.xlim(min(X[1])-.5, max(X[1])+.5)
plt.ylim(min(X[2])-.5, max(X[2])+.5)
display(X[1:,:],y)
plt.savefig('Cau3.pdf', bbox_inches='tight', dpi = 300)
plt.show()
import numpy as np
import scipy
from scipy import sparse
import matplotlib.pyplot as plt
np.random.seed(3)
means = [[3, 8], [2, 6], [4, 10]]
cov = [[10, 0], [0, 10]]
N = 1000
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).T
X = np.concatenate((np.ones((1, 3*N)), X), axis = 0)
C = 3
y = np.asarray([0]*N + [1]*N + [2]*N).T
def convert_labels(y, C = C):
Y = sparse.coo_matrix((np.ones_like(y),
(y, np.arange(len(y)))), shape = (C, len(y))).toarray()
return Y
# def cost(X, Y, W):
# A = softmax(W.T.dot(X))
# return -np.sum(Y*np.log(A))
# def grad(X, Y, W):
# A = softmax((W.T.dot(X)))
# E = A - Y
# return X.dot(E.T)
def softmax(Z):
e_Z = np.exp(Z)
A = e_Z / e_Z.sum(axis = 0)
return A
def softmax_stable(Z):
e_Z = np.exp(Z - np.max(Z, axis = 0, keepdims = True))
A = e_Z / e_Z.sum(axis = 0)
return A
# def numerical_grad(X, Y, W, cost):
# eps = 1e-6
# g = np.zeros_like(W)
# for i in range(W.shape[0]):
# for j in range(W.shape[1]):
# W_p = W.copy()
# W_n = W.copy()
# W_p[i, j] += eps
# W_n[i, j] -= eps
# g[i,j] = (cost(X, Y, W_p) - cost(X, Y, W_n))/(2*eps)
# return g
def softmax_regression(X, y, W_init, eta, tol = 1e-4, max_count = 10000):
W = [W_init]
C = W_init.shape[1]
Y = convert_labels(y, C)
it = 0
N = X.shape[1]
d = X.shape[0]
count = 0
check_w_after = 20
while count < max_count:
# mix data
mix_id = np.random.permutation(N)
for i in mix_id:
xi = X[:, i].reshape(d, 1)
yi = Y[:, i].reshape(C, 1)
ai = softmax(np.dot(W[-1].T, xi))
W_new = W[-1] + eta*xi.dot((yi - ai).T)
count += 1
# stopping criteria
if count%check_w_after == 0:
if np.linalg.norm(W_new - W[-check_w_after]) < tol:
return W
W.append(W_new)
return W
def pred(W, X):
A = softmax_stable(W.T.dot(X))
return np.argmax(A, axis = 0)
def display(X, label):
X0 = X[:, label == 0]
X1 = X[:, label == 1]
X2 = X[:, label == 2]
plt.plot(X0[0, :], X0[1, :], 'b^', markersize = 4, alpha = .8)
plt.plot(X1[0, :], X1[1, :], 'go', markersize = 4, alpha = .8)
plt.plot(X2[0, :], X2[1, :], 'rs', markersize = 4, alpha = .8)
plt.axis('off')
Y = convert_labels(y, C)
W_init = np.random.randn(X.shape[0], C)
# g1 = grad(X, Y, W_init)
# g2 = numerical_grad(X, Y, W_init, cost)
eta = .05
W = softmax_regression(X, y, W_init, eta)
xm = np.arange(min(X[1]-.5), max(X[1]+.5), 0.025)
xlen = len(xm)
ym = np.arange(min(X[2]-.5), max(X[2]+.5), 0.025)
ylen = len(ym)
xx, yy = np.meshgrid(xm, ym)
xx1 = xx.ravel().reshape(1, xx.size)
yy1 = yy.ravel().reshape(1, yy.size)
XX = np.concatenate((np.ones((1, xx.size)), xx1, yy1), axis = 0)
Z = pred(W[-1], XX)
Z = Z.reshape(xx.shape)
plt.figure(figsize=(40,12))
CS = plt.contourf(xx, yy, Z, 200, cmap='jet', alpha = .2)
plt.xlim(min(X[1])-.5, max(X[1])+.5)
plt.ylim(min(X[2])-.5, max(X[2])+.5)
display(X[1:,:],y)
plt.savefig('Cau3_1.pdf', bbox_inches='tight', dpi = 300)
plt.show()
KHÔNG MOMENTUM
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def grad(x):#by NTĐ
return 2*x + 10*np.cos(x)
def cost(x):
return x**2 + 10*np.sin(x)
def GD_1(eta, x0):
x = [x0]
for it in range(100):
x_new = x[-1] - eta*grad(x[-1])
if abs(grad(x_new)) < 1e-5:
break
x.append(x_new)
return (x,it)
(x1, it1) = GD_1(.1, -5)
(x2, it2) = GD_1(.1, 5)
print('Solution x1 = %f, cost =%f, obtained after %d iterations'%(x1[-1],
cost(x1[-1]), it1))
print('Solution x2 = %f, cost =%f, obtained after %d iterations'%(x2[-1],
cost(x2[-1]), it2))
def draw_gdld(x2, ids, filename, nrows = 2, ncols = 4, start= -5.5):
x0 = np.linspace(start, 5.5, 1000)
y0 = cost(x0)
width = 4*ncols
height = 4*nrows
plt.close('all')
fig,axs=plt.subplots(nrows, ncols, figsize=(width, height))
with PdfPages(filename) as pdf:
for i, k in enumerate (ids):
r = i//ncols
c = i%ncols
x = x2[ids[i]]
y = cost (x)
str0 = 'iter {}/{}, grad = {:.3f}'.format(ids[i], len(x2)-1, g
rad(x))
axs[r, c].plot(x0, y0,'b')
axs[r, c].set_xlabel (str0, fontsize = 13)
axs[r, c].plot(x, y, 'r*', markersize = 8, markeredgecolor = '
k')
axs[r, c].plot()
axs[r, c].tick_params(axis='both', which='major', labelsize=13
)
pdf.savefig(bbox_inches='tight' )
plt.show()
filename = '12.pdf'
ids = [0, 1, 2, 3, 4, 5, 6, 7 ]
draw_gdld(x2, ids, filename, nrows = 2, ncols = 4, start= -5.5)
CÓ MOMENTUM
# from _future_ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def grad(x):
return 5*(2*x-4)*np.sin(x) + 5*(x**2-4*x+4)*np.cos(x) + 2
def cost(x):
return 5*(x-2)**2*np.sin(x) + 2*x
def has_converged(theta_new, grad):
return np.linalg.norm(grad(theta_new))/np.array(theta_new).size < 1e-3
def GD_momentum(theta_init,grad, eta, gamma):
theta = [theta_init]
v_old = np.zeros_like(theta_init)
for it in range(100):
v_new = gamma*v_old + eta*grad(theta[-1])
theta_new = theta[-1] - v_new
theta.append(theta_new)
if has_converged(theta_new, grad):
break
v_old = v_new
return theta
theta = GD_momentum(5,grad, 0.01, 0.8)
def draw_gdld(theta, ids, filename, nrows = 2, ncols = 3, start= -5.5):
x0 = np.linspace(start, 5.5, 1000)
y0 = cost(x0)
width = 4*ncols
height = 4*nrows
plt.close('all')
fig,axs=plt.subplots(nrows, ncols, figsize=(width, height))
with PdfPages(filename) as pdf:#byNTĐ
for i, k in enumerate (ids):
r = i//ncols
c = i%ncols
x = theta[ids[i]]
y = cost (x)
str0 = 'iter {}/{}, grad = {:.3f}'.format(ids[i], len(theta) -
1 , grad(x))
axs[r, c].plot(x0, y0,'b')
axs[r, c].set_xlabel (str0, fontsize = 13)
axs[r, c].plot(x, y, 'r*', markersize = 8, markeredgecolor = '
k')
axs[r, c].plot()
axs[r, c].tick_params(axis='both', which='major', labelsize=13
)
pdf.savefig(bbox_inches='tight' )
plt.show()
filename = '12.pdf'
ids = [2, 3, 5, 8, 10, 13]
draw_gdld(theta, ids, filename, nrows = 2, ncols = 3, start= -5.5)
#byNTĐ
SIGMOID
from __future__ import division, print_function, unicode_literals
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
np.random.seed(2)
X = np.array([[0.50, 0.75, 1.00, 1.25, 1.50, 1.75, 1.75, 2.00, 2.25, 2.50,
2.75, 3.00, 3.25, 3.50, 4.00, 4.25, 4.50, 4.75, 5.00, 5.50]])
y = np.array([0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1])
#mở rộng data
X =np.concatenate((np.ones((1,X.shape[1])),X), axis=0)
def signmoid(s):
return 1/(1 + np.exp(-s))
def logistic_signmoid_regression (X,y,w_init,eta,tol=1e-4,max_count = 100
0):
w=[w_init]
it=0
N = X.shape[1]
d = X.shape[0]
count = 0
check_w_after = 20
while count< max_count:
#trộn dữ liệu
mix_id=np.random.permutation(N)
for i in mix_id:
xi = X[:, i].reshape(d,1)
yi = y[i]
zi = signmoid(np.dot(w[-1].T, xi))
w_new = w[-1] + eta*(yi - zi)*xi
count +=1
# dừng vòng lặp
if count%check_w_after ==0:
if np.linalg.norm(w_new - w[-check_w_after]) < tol:
return w
w.append(w_new)
return w
eta = .05
d = X.shape[0]
w_init = np.random.randn (d,1)
w= logistic_signmoid_regression(X,y,w_init,eta)
print(w[-1])
print (signmoid(np.dot(w[-1].T,X)))
X0 = X[1,np.where(y==0)][0]
y0 = y[np.where(y==0)]
X1 = X[1,np.where(y==1)][0]
y1 = y[np.where(y==1)]
plt.plot(X0,y0, 'rs', markersize = 9)
plt.plot(X1,y1, 'bo', markersize = 9)
xx = np.linspace(0,6,1000)
w0 = w[-1][0][0]
w1 = w[-1][1][0]
threshold = -w0/w1
yy = signmoid(w0 + w1*xx)
plt.axis([-2,8,-1,2])
plt.plot(xx,yy,'g-',linewidth = 2)
plt.plot(threshold, .5, 'y^', markersize=9)
plt.xlabel('số h học')
plt.ylabel('dự đoán kn qua')
plt.show()
NAG
# from _future_ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def grad(x):
return 2*x + 10*np.cos(x)
def cost(x):
return x**2 + 10*np.sin(x)
def GD_NAG(w_init, grad, eta, gamma):
w = [w_init]
v = [np.zeros_like(w_init)]
for it in range(100):
v_new = gamma * v[-1] + eta*grad(w[-1] - gamma*v[-1])
w_new = w[-1] - v_new
if np.linalg.norm(grad(w_new))/np.array(w_new).size < 1e-5:
break
w.append(w_new)
v.append(v_new)
return w
w = GD_NAG(8, grad, 0.01, 0.9)
def draw_gdld(w, ids, filename, nrows = 2, ncols = 4, start= -5.5):
x0 = np.linspace(start, 5.5, 1000)
y0 = cost(x0)
width = 4*ncols
height = 4*nrows
plt.close('all')
fig,axs=plt.subplots(nrows, ncols, figsize=(width, height))
with PdfPages(filename) as pdf:#byNTĐ
for i, k in enumerate (ids):
r = i//ncols
c = i%ncols
x = w[ids[i]] #"respect virtue"
y = cost (x)
str0 = 'iter {}/{}, grad = {:.3f}'.format(ids[i], len(w) - 1 ,
grad(x))
axs[r, c].plot(x0, y0,'b')
axs[r, c].set_xlabel (str0, fontsize = 13)
axs[r, c].plot(x, y, 'r*', markersize = 8, markeredgecolor = '
k')
axs[r, c].plot()
axs[r, c].tick_params(axis='both', which='major', labelsize=13
)
pdf.savefig(bbox_inches='tight' )
plt.show()
filename = '12.pdf'
ids = [10, 20, 30, 40, 50, 60, 70, 99]
draw_gdld(w, ids, filename, nrows = 2, ncols = 4, start = -5.5)
#byNTĐ
SOFMAX
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 18 11:46:33 2022
@author: Admin
"""
from __future__ import division, print_function, unicode_literals
import math
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
np.random.seed(1) # for fixing random values
# randomly generate data
N = 200 # số dl đầu vào , cái đầu vào cần huấn luyện
d = 3 # chiều
C = 5 # lớp
X = d*np.random.rand ( d , N) # giá trị từ -5 - nhỏ hơn 5
#X = np.random.rand ( d , N)
y = np.random.randint (0 , 3 , (N,))
from scipy import sparse
def convert_labels(y, C = C):
Y = sparse.coo_matrix((np.ones_like(y), #ma trận đa số =0
(y, np.arange(len(y)))), shape = (C, len(y))).toarray()
return Y
X = convert_labels(y, d)
Y = convert_labels(y, C)
print(y)
print(Y)
def softmax(Z):
"""
Compute softmax values for each sets of scores in V.
each column of V is a set of score.
"""
e_Z = np.exp(Z)
A = e_Z / e_Z.sum(axis = 0)
return A
def softmax_stable(Z):
"""
Compute softmax values for each sets of scores in V.
each column of V is a set of score.
"""
e_Z = np.exp(Z - np.max(Z, axis = 0, keepdims = True))
A = e_Z / e_Z.sum(axis = 0)
return A
# cost or loss function
def cost(X, Y, W):
A = softmax(W.T.dot(X))
return -np.sum(Y*np.log(A))
W_init = np.random.randn(d, C)
def grad(X, Y, W):
A = softmax((W.T.dot(X)))
E = A - Y
return X.dot(E.T)
def numerical_grad(X, Y, W, cost):
eps = 1e-6
g = np.zeros_like(W)
for i in range(W.shape[0]):
for j in range(W.shape[1]):
W_p = W.copy()
W_n = W.copy()
W_p[i, j] += eps
W_n[i, j] -= eps
g[i,j] = (cost(X, Y, W_p) - cost(X, Y, W_n))/(2*eps)
return g
g1 = grad(X, Y, W_init)
g2 = numerical_grad(X, Y, W_init, cost)
print(np.linalg.norm(g1 - g2))
def softmax_regression(X, y, W_init, eta, tol = 1e-4, max_count = 10000):
W = [W_init]
C = W_init.shape[1]
Y = convert_labels(y, C)
it = 0
N = X.shape[1]
d = X.shape[0]
count = 0
check_w_after = 20
while count < max_count:
# mix data
mix_id = np.random.permutation(N)
for i in mix_id:
xi = X[:, i].reshape(d, 1)
yi = Y[:, i].reshape(C, 1)
ai = softmax(np.dot(W[-1].T, xi))
W_new = W[-1] + eta*xi.dot((yi - ai).T)
count += 1
# stopping criteria
if count%check_w_after == 0:
if np.linalg.norm(W_new - W[-check_w_after]) < tol:
return W
W.append(W_new)
return W
eta = .05
d = X.shape[0]
W_init = np.random.randn(d, C)
W = softmax_regression(X, y, W_init, eta)
print('w= ' , W[-1])
def pred(W, X):
A = softmax_stable(W.T.dot(X))
return np.argmax(A, axis = 0)
means = [[1,1], [9,9], [5,5]] #blue,green,red
cov = [[2, 0], [0, 2]]
N = 200
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).T # each column is a datapoint
X = np.concatenate((np.ones((1, 3*N)), X), axis = 0)
C = 3
original_label = np.asarray([0]*N + [1]*N + [2]*N).T
def 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, :], 'y^', markersize = 5, alpha = 1.0)
plt.plot(X1[0, :], X1[1, :], 'go', markersize = 5, alpha = 1.0)
plt.plot(X2[0, :], X2[1, :], 'rs', markersize = 5, alpha = 1.0)
# plt.axis('equal')
plt.axis('off') # on haowjc off
plt.plot()
#pdf.savefig(bbiox_inches='tight' )
plt.show()
display(X[1:, :], original_label)
plt.savefig('softmaxRegression.png', bbox_inches='tight', dpi = 800)
plt.show()
BACKPROGATION
from __future__ import division,print_function, unicode_literals
from google.colab import drive
drive.mount('/content/gdrive')
import math
import numpy as np
import matplotlib.pyplot as plt
N = 100 # số mẫu
d0 = 2 # dimensionality : kích thước
C = 3 # số lớpl
X = np.zeros((d0, N*C)) # data matrix (each row = single example)
y = np.zeros(N*C, dtype='uint8') # class labels
for j in range(C):
ix = range(N*j,N*(j+1))
r = np.linspace(0.0,1,N) # radius
t = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # theta
X[:,ix] = np.c_[r*np.sin(t), r*np.cos(t)].T
y[ix] = j
# lets visualize the data:
# plt.scatter(X[:N, 0], X[:N, 1], c=y[:N], s=40, cmap=plt.cm.Spectral)
plt.plot(X[0, :N], X[1, :N], 'bs', markersize = 7);
plt.plot(X[0, N:2*N], X[1, N:2*N], 'g^', markersize = 7);
plt.plot(X[0, 2*N:], X[1, 2*N:], 'ro', markersize = 7);
# plt.axis('off')
plt.xlim([-1.5, 1.5])
plt.ylim([-1.5, 1.5])
cur_axes = plt.gca()
cur_axes.axes.get_xaxis().set_ticks([])
cur_axes.axes.get_yaxis().set_ticks([])
plt.savefig('EX.png', bbox_inches='tight', dpi = 600)
plt.show()
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
def cost(Y, Yhat):
return -np.sum(Y*np.log(Yhat))/Y.shape[1]
d0 = 2 # số chiều đầu vào (số hàng)
d1 = h = 200 # size of hidden layer : số unit (số cột)
d2 = C = 3 # số lớp
# 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 # đầu vào WT*X
A1 = np.maximum(Z1, 0)
Z2 = np.dot(W2.T, A1) + b2 # (W2T * A1)+b2
# import pdb; pdb.set_trace() # breakpoint 035ab9b5 //
Yhat = softmax(Z2)
# compute the loss: average cross-entropy loss
loss = cost(Y, Yhat)
# print loss after each 1000 iterations
if i %1000 == 0:
print("iter %d, mất mát: %f" %(i, loss))
# backpropagation
E2 = (Yhat - Y )/N
dW2 = np.dot(A1, E2.T)
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)
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
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('đào tạo chính xác: %.2f %%' % acc)
# Visualize results
#Visualize
xm = np.arange(-1.5, 1.5, 0.025)
xlen = len(xm)
ym = np.arange(-1.5, 1.5, 0.025)
ylen = len(ym)
xx, yy = np.meshgrid(xm, ym)
# xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max,
h))
# xx.ravel(), yy.ravel()
print(np.ones((1, xx.size)).shape)
xx1 = xx.ravel().reshape(1, xx.size)
yy1 = yy.ravel().reshape(1, yy.size)
# print(xx.shape, yy.shape)
# XX = np.concatenate((np.ones((1, xx.size)), xx1, yy1), axis = 0)
X0 = np.vstack((xx1, yy1))
# print(X.shape)
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)
# Plot also the training points
# plt.scatter(X[:, 1], X[:, 2], c=Y, edgecolors='k', cmap=plt.cm.Paired)
# plt.xlabel('Sepal length')
# plt.ylabel('Sepal width')
# X = X.T
N = 100
print(N)
plt.plot(X[0, :N], X[1, :N], 'bs', markersize = 7);
plt.plot(X[0, N:2*N], X[1, N:2*N], 'g^', markersize = 7);
plt.plot(X[0, 2*N:], X[1, 2*N:], 'ro', markersize = 7);
# plt.axis('off')
plt.xlim([-1.5, 1.5])
plt.ylim([-1.5, 1.5])
cur_axes = plt.gca()
cur_axes.axes.get_xaxis().set_ticks([])
cur_axes.axes.get_yaxis().set_ticks([])
plt.xlim(-1.5, 1.5)
plt.ylim(-1.5, 1.5)
plt.xticks(())
plt.yticks(())
plt.title('#hidden units = %d, accuracy = %.2f %%' %(d1, acc))
# plt.axis('equal')
# display(X[1:, :], original_label)
fn = 'ex_res'+ str(d1) + '.png'
# plt.savefig(fn, bbox_inches='tight', dpi = 600)
plt.show()