深度学习——02、深度学习入门 15-18
生活随笔
收集整理的這篇文章主要介紹了
深度学习——02、深度学习入门 15-18
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
15Python環(huán)境搭建(推薦Anaconda方法)
軟件萬能安裝起點
python官網(wǎng)
Anaconda官網(wǎng)
16Eclipse搭建python環(huán)境
軟件萬能安裝起點
python官網(wǎng)
Eclipse官網(wǎng)
17動手完成簡單神經(jīng)網(wǎng)絡(luò)
import numpy as npdef sigmoid (x,deriv=False):if(deriv==True):return x*(1-x)return 1/(1+np.exp(-x))x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1],[0,0,1] ]) print(x.shape) y=np.array([[0],[1],[1],[0],[0] ]) print(y.shape)np.random.seed(1)w0=2*np.random.random((3,4))-1 w1=2*np.random.random((4,1))-1for j in range(100000):l0=xl1=sigmoid(np.dot(l0,w0))l2=sigmoid(np.dot(l1,w1))l2_error=y-l2if(j%10000)==0:print('Error'+str(np.mean(np.abs(l2_error))))l2_delta=l2_error*sigmoid(l2,deriv=True)l1_error=l2_delta.dot(w1.T)l1_delta=l1_error*sigmoid(l1,deriv=True)w1 += l1.T.dot(l2_delta)w0 += l0.T.dot(l1_delta)代碼分析
激活函數(shù)
def sigmoid (x,deriv=False):'''激活函數(shù):param x::param deriv:標(biāo)志位,當(dāng)derive為假時,不計算導(dǎo)數(shù),反之計算,控制傳播方向。:return:'''if(deriv==True):# 求導(dǎo)return x*(1-x)return 1/(1+np.exp(-x))激活函數(shù)參見:機(jī)器學(xué)習(xí)——01、機(jī)器學(xué)習(xí)的數(shù)學(xué)基礎(chǔ)1 - 數(shù)學(xué)分析——Sigmoid/Logistic函數(shù)的引入
定義input和lable值
x=np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1],[0,0,1] ]) y=np.array([[0],[1],[1],[0],[0] ])可以看一下x和y的維度:
print(x.shape) print(y.shape)指定隨機(jī)種子
np.random.seed(1)保證數(shù)據(jù)一致,方便對比結(jié)果。
初始化權(quán)重值
#定義w0和w1在-1到1之間 w0=2*np.random.random((3,4))-1 #三行對應(yīng)x的三個特征,四列對應(yīng)L1層的四個神經(jīng)元 w1=2*np.random.random((4,1))-1 #四行對應(yīng)L1的四個神經(jīng)元,一列對應(yīng)輸出0或1定義三層神經(jīng)網(wǎng)絡(luò)
l0=x#L0層為輸入層#進(jìn)行梯度傳播操作:l1=sigmoid(np.dot(l0,w0))#L0與W0進(jìn)行矩陣運(yùn)算得到L1l2=sigmoid(np.dot(l1,w1))#L1與W1進(jìn)行矩陣運(yùn)算得到L2根據(jù)誤差更新權(quán)重
l2_error=y-l2#預(yù)測值與真實值之間的差異if(j%10000)==0:print('Error'+str(np.mean(np.abs(l2_error))))#打印誤差值l2_delta=l2_error*sigmoid(l2,deriv=True)#反向傳播求導(dǎo),差異值越大,需要更新的越大l1_error=l2_delta.dot(w1.T)#w1進(jìn)行轉(zhuǎn)置之后與l2_delta進(jìn)行矩陣運(yùn)算l1_delta=l1_error*sigmoid(l1,deriv=True)#更新w1和w2w1 += l1.T.dot(l2_delta)w0 += l0.T.dot(l1_delta)18感受神經(jīng)網(wǎng)絡(luò)的強(qiáng)大
假設(shè)有如下數(shù)據(jù)要將其分類
代碼
import numpy as np import matplotlib.pyplot as plt#ubuntu 16.04 sudo pip instal matplotlibplt.rcParams['figure.figsize'] = (10.0, 8.0) # set default size of plots plt.rcParams['image.interpolation'] = 'nearest' plt.rcParams['image.cmap'] = 'gray'np.random.seed(0) N = 100 # number of points per class D = 2 # dimensionality K = 3 # number of classes X = np.zeros((N*K,D)) y = np.zeros(N*K, dtype='uint8') for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = j fig = plt.figure() plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim([-1,1]) plt.ylim([-1,1]) plt.show()傳統(tǒng)AI算法
#Train a Linear Classifier import numpy as np import matplotlib.pyplot as pltnp.random.seed(0) N = 100 # number of points per class D = 2 # dimensionality K = 3 # number of classes X = np.zeros((N*K,D)) y = np.zeros(N*K, dtype='uint8') for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = jW = 0.01 * np.random.randn(D,K) b = np.zeros((1,K))# some hyperparameters step_size = 1e-0 reg = 1e-3 # regularization strength# gradient descent loop num_examples = X.shape[0] for i in range(1000):#print X.shape# evaluate class scores, [N x K]scores = np.dot(X, W) + b #x:300*2 scores:300*3#print scores.shape # compute the class probabilitiesexp_scores = np.exp(scores)probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K] probs:300*3print (probs.shape )# compute the loss: average cross-entropy loss and regularizationcorect_logprobs = -np.log(probs[range(num_examples),y]) #corect_logprobs:300*1print (corect_logprobs.shape)data_loss = np.sum(corect_logprobs)/num_examplesreg_loss = 0.5*reg*np.sum(W*W)loss = data_loss + reg_lossif i % 100 == 0:print ("iteration %d: loss %f" % (i, loss))# compute the gradient on scoresdscores = probsdscores[range(num_examples),y] -= 1dscores /= num_examples# backpropate the gradient to the parameters (W,b)dW = np.dot(X.T, dscores)db = np.sum(dscores, axis=0, keepdims=True)dW += reg*W # regularization gradient# perform a parameter updateW += -step_size * dWb += -step_size * dbscores = np.dot(X, W) + b predicted_class = np.argmax(scores, axis=1) print ('training accuracy: %.2f' % (np.mean(predicted_class == y)))h = 0.02 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h)) Z = np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b Z = np.argmax(Z, axis=1) Z = Z.reshape(xx.shape) fig = plt.figure() plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show()神經(jīng)網(wǎng)絡(luò)算法
import numpy as np import matplotlib.pyplot as pltnp.random.seed(0) N = 100 # number of points per class D = 2 # dimensionality K = 3 # number of classes X = np.zeros((N*K,D)) y = np.zeros(N*K, dtype='uint8') for j in range(K):ix = range(N*j,N*(j+1))r = np.linspace(0.0,1,N) # radiust = np.linspace(j*4,(j+1)*4,N) + np.random.randn(N)*0.2 # thetaX[ix] = np.c_[r*np.sin(t), r*np.cos(t)]y[ix] = jh = 100 # size of hidden layer W = 0.01 * np.random.randn(D,h)# x:300*2 2*100 b = np.zeros((1,h)) W2 = 0.01 * np.random.randn(h,K) b2 = np.zeros((1,K))# some hyperparameters step_size = 1e-0 reg = 1e-3 # regularization strength# gradient descent loop num_examples = X.shape[0] for i in range(2000):# evaluate class scores, [N x K]hidden_layer = np.maximum(0, np.dot(X, W) + b) # note, ReLU activation hidden_layer:300*100#print hidden_layer.shapescores = np.dot(hidden_layer, W2) + b2 #scores:300*3#print scores.shape# compute the class probabilitiesexp_scores = np.exp(scores)probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K]#print probs.shape# compute the loss: average cross-entropy loss and regularizationcorect_logprobs = -np.log(probs[range(num_examples),y])data_loss = np.sum(corect_logprobs)/num_examplesreg_loss = 0.5*reg*np.sum(W*W) + 0.5*reg*np.sum(W2*W2)loss = data_loss + reg_lossif i % 100 == 0:print ("iteration %d: loss %f" % (i, loss))# compute the gradient on scoresdscores = probsdscores[range(num_examples),y] -= 1dscores /= num_examples# backpropate the gradient to the parameters# first backprop into parameters W2 and b2dW2 = np.dot(hidden_layer.T, dscores)db2 = np.sum(dscores, axis=0, keepdims=True)# next backprop into hidden layerdhidden = np.dot(dscores, W2.T)# backprop the ReLU non-linearitydhidden[hidden_layer <= 0] = 0# finally into W,bdW = np.dot(X.T, dhidden)db = np.sum(dhidden, axis=0, keepdims=True)# add regularization gradient contributiondW2 += reg * W2dW += reg * W# perform a parameter updateW += -step_size * dWb += -step_size * dbW2 += -step_size * dW2b2 += -step_size * db2 hidden_layer = np.maximum(0, np.dot(X, W) + b) scores = np.dot(hidden_layer, W2) + b2 predicted_class = np.argmax(scores, axis=1) print ('training accuracy: %.2f' % (np.mean(predicted_class == y)))h = 0.02 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h)) Z = np.dot(np.maximum(0, np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b), W2) + b2 Z = np.argmax(Z, axis=1) Z = Z.reshape(xx.shape) fig = plt.figure() plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.show()總結(jié)
以上是生活随笔為你收集整理的深度学习——02、深度学习入门 15-18的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PaddlePaddle训练营——公开课
- 下一篇: 人脸识别算法不可置疑?真相需要多重验证!