简单的一层DNN:
import numpy as npdef nonlin(x,deriv=False): if deriv==True: return x*(1-x) return 1/(1+np.exp(-x)) X=np.array([[0,1,1],[1,1,0],[1,0,1],[1,1,1]])y=np.array([[0,0,1,1]]).Tnp.random.seed(1)w=2*np.random.random((3,1))-1for iter in range(10000): l0=X l1=nonlin(np.dot(l0,w)) l1_error=y-l1 #实际值与预测值之间的差值,差值越大,调整力度越大 l1_grad=l1_error*nonlin(l1,True) #用差值当权重,差值越小,梯度相应较小,调整力度较小 w=w+np.dot(l0.T,l1_grad)print('result')print(l1)#print(l1_grad)print(y)
简单的两层DNN:
import numpy as npdef nonlin(x,deriv=False): if deriv==True: return x*(1-x) return 1/(1+np.exp(-x))X=np.array([[1,0,0],[0,1,0],[1,0,1],[1,1,0]]) #输入数据y=np.array([[0,0,1,1]]).T #label值w0=2*np.random.random((3,3))-1w1=2*np.random.random((3,1))-1for iter in range(10000): l0=X l1=nonlin(np.dot(l0,w0)) l2=nonlin(np.dot(l1,w1)) l2_error=y-l2 l2_grad=l2_error*nonlin(l2,True) l1_error=l2_grad.dot(w1.T) #不懂 l1_grad=l1_error*nonlin(l1,True) w1=w1+l1.T.dot(l2_grad) w0=w0+l0.T.dot(l1_grad)print('result')print(l2)print(y)
线性分类对三种螺旋形数据:
import numpy as npimport matplotlib.pyplot as plt#%matplotlib inlineplt.rcParams['figure.figsize']=(10.0,8.0) #set default size of plotsplt.rcParams['image.interpolation']='nearest'plt.rcParams['image.cmap']='gray'#%load_ext autoreload#%autoreload 2np.random.seed(0)N=100D=2K=3X=np.zeros((N*K,D))y=np.zeros(N*K,dtype='uint8')for j in range(K): ix=list(range(N*j,N*(j+1))) r=np.linspace(0.0,1,N) t=np.linspace(j*4,(j+1)*4,N)+np.random.randn(N)*0.2 X[ix]=np.c_[r*np.sin(t),t*np.cos(t)] y[ix]=jfig=plt.figure()plt.scatter(X[:,0],X[:,1],c=y,s=40,cmap=plt.cm.Spectral)plt.xlim([-1,1])plt.ylim([-1,1])W=0.01*np.random.randn(D,K)b=np.zeros((1,K))step_size=1e-0 #学习率为1时,准确率0.47step_size=1e-1 #学习率为0.1时,准确率0.75#step_size=1e-2 #学习率为0.01时,准确率0.63#step_size=1e-6 #学习率为1e-6时,准确率0.38reg=1e-3num_examples=X.shape[0]for i in range(2000): scores= np.dot(X,W)+b exp_score=np.exp(scores) probs=exp_score/np.sum(exp_score,axis=1,keepdims=True) coret_logprobs=-np.log(probs[list(range(num_examples)),y]) data_loss=np.sum(coret_logprobs)/num_examples reg_loss=0.5*reg*np.sum(W*W) loss=data_loss+reg_loss if i%10 == 0: print("iteration %d:loss %f"%(i,loss)) dscores=probs dscores[list(range(num_examples)),y]=dscores[list(range(num_examples)),y]-1 dscores/=num_examples #此处是/号,要细心!!! dW=np.dot(X.T,dscores) db=np.sum(dscores,axis=0,keepdims=True) dW=dW+reg*W W+=-step_size*dW b+=-step_size*dbscores= np.dot(X,W)+bpredicted_class=np.argmax(scores,axis=1)print('training accuracy:%.2f'%(np.mean(predicted_class==y))) #计算分类准确率
简单的神经网络对三种螺旋形数据分类:
import numpy as npimport matplotlib.pyplot as plt#%matplotlib inlineplt.rcParams['figure.figsize']=(10.0,8.0) #set default size of plotsplt.rcParams['image.interpolation']='nearest'plt.rcParams['image.cmap']='gray'#%load_ext autoreload#%autoreload 2np.random.seed(0)N=100D=2K=3X=np.zeros((N*K,D))y=np.zeros(N*K,dtype='uint8')for j in range(K): ix=list(range(N*j,N*(j+1))) r=np.linspace(0.0,1,N) t=np.linspace(j*4,(j+1)*4,N)+np.random.randn(N)*0.2 X[ix]=np.c_[r*np.sin(t),t*np.cos(t)] y[ix]=jfig=plt.figure()plt.scatter(X[:,0],X[:,1],c=y,s=40,cmap=plt.cm.Spectral)plt.xlim([-1,1])plt.ylim([-1,1])h=100W=0.01*np.random.randn(D,h)b=np.zeros((1,h))W2=0.01*np.random.randn(h,K)b2=np.zeros((1,K))step_size=1 #学习率为1时,准确率0.94reg=1e-3num_examples=X.shape[0]for i in range(1000): hidden_layer=np.maximum(0,np.dot(X,W)+b) scores= np.dot(hidden_layer,W2)+b2 exp_score=np.exp(scores) probs=exp_score/np.sum(exp_score,axis=1,keepdims=True) coret_logprobs=-np.log(probs[list(range(num_examples)),y]) data_loss=np.sum(coret_logprobs)/num_examples reg_loss=0.5*reg*np.sum(W*W)+0.5*reg*np.sum(W*W) loss=data_loss+reg_loss if i%10 == 0: print("iteration %d:loss %f"%(i,loss)) dscores=probs dscores[list(range(num_examples)),y]=dscores[list(range(num_examples)),y]-1 dscores/=num_examples #此处是/号,要细心!!! dW2=np.dot(hidden_layer.T,dscores) db2=np.sum(dscores,axis=0,keepdims=True) dhidden=np.dot(dscores,W2.T) dhidden[hidden_layer <=0] = 0 dW=np.dot(X.T,dhidden) db=np.sum(dhidden,axis=0,keepdims=True) dW2+=reg*W2 dW+=reg*W W+=-step_size*dW b+=-step_size*db W2+=-step_size*dW2 b2+=-step_size*db2hidden_layer=np.maximum(0,np.dot(X,W)+b)scores= np.dot(hidden_layer,W2)+b2predicted_class=np.argmax(scores,axis=1)print('training accuracy:%.2f'%(np.mean(predicted_class==y))) #计算分类准确率