import numpy as np
import matplotlib.pyplot as plt
x = np.random.uniform(0,2,40)
z = 3*x-7 + np.random.normal(0,0.3,40)
plt.scatter(x,z,c='r',edgecolor='k')
plt.show()
from unagi import Chan,Affin,Relu,Sigmoid,Adam
class Mse(Chan):
def pai(self,h,z):
self.z = z[:,None]
self.h = h
return ((self.z-h)**2).mean()
def yon(self,g):
return g*2*(self.h-self.z)/len(self.z)
class ThotthoiChoengsen:
def rianru(self,X,z,n_thamsam):
self.chan = [Affin(X.shape[1],1,0),Mse()]
self.opt = Adam(self.chan[0].param)
for o in range(n_thamsam):
h = self.chan[0](X)
mse = self.chan[1](h,z)
mse.phraeyon()
self.opt()
def thamnai(self,X):
h = self.chan[0].pai(X)
return h.ravel()
x = np.random.uniform(0,2,30)
X = x[:,None]
z = 2*x-3 + np.random.normal(0,0.4,30)
tc = ThotthoiChoengsen()
tc.rianru(X,z,10000)
x_ = np.linspace(-0.1,2.1,101)
X_ = x_[:,None]
z_ = tc.thamnai(X_)
plt.scatter(x,z,c='c',edgecolor='k')
plt.plot(x_,z_,'r')
plt.show()
from mpl_toolkits.mplot3d import Axes3D
X = np.random.uniform(-1,1,[100,2])
x,y = X.T
z = np.random.normal(x*2+y*3+1,1)
tc = ThotthoiChoengsen()
tc.rianru(X,z,10000)
plt.figure(figsize=[7,7])
ax = plt.axes([0,0,1,1],projection='3d')
ax.scatter(x,y,z,c=z,edgecolor='k',cmap='winter')
mx,my = np.meshgrid(np.linspace(-1,1,21),np.linspace(-1,1,21))
mX = np.array([mx.ravel(),my.ravel()]).T
mz = tc.thamnai(mX).reshape(21,-1)
ax.plot_surface(mx,my,mz,rstride=1,cstride=1,alpha=0.2,color='r',edgecolor='k')
plt.show()
class PrasatThotthoi:
def __init__(self,m1,m2,eta=0.001):
self.m1 = m1
self.m2 = m2
self.eta = eta
self.chan = [None,
Relu(),
Affin(m1,m2,np.sqrt(2./m1)),
Relu(),
Affin(m2,1,0),
Mse()]
def rianru(self,X,z,n_thamsam):
m0 = X.shape[1]
self.chan[0] = Affin(m0,self.m1,np.sqrt(2./m0))
self.opt = Adam(self.param(),eta=self.eta)
for o in range(n_thamsam):
mse = self.ha_mse(X,z)
mse.phraeyon()
self.opt()
def ha_mse(self,X,z):
for c in self.chan[:-1]:
X = c(X)
return self.chan[-1](X,z)
def param(self):
p = []
for c in self.chan:
if(hasattr(c,'param')):
p.extend(c.param)
return p
def thamnai(self,X):
for c in self.chan[:-1]:
X = c(X)
return X.kha.ravel()
np.random.seed(0)
x = np.random.uniform(-1,1,60)
X = x[:,None]
z = np.sin(x*3)+np.random.normal(0,0.2,60)
m1,m2 = 20,30
ps = PrasatThotthoi(m1,m2,eta=0.005)
ps.rianru(X,z,1000)
x_ = np.linspace(-1.2,1.2,101)
X_ = x_[:,None]
z_ = ps.thamnai(X_)
plt.scatter(x,z,c='c',edgecolor='k')
plt.plot(x_,z_,'r')
plt.show()
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ