深度学习:神经网络,softmax + cross entropy,非tensorflow方式
生活随笔
收集整理的這篇文章主要介紹了
深度学习:神经网络,softmax + cross entropy,非tensorflow方式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
文章目錄
- softmax
- softmax 的損失函數(shù):交叉熵
- 交叉熵求偏導(dǎo)
- 代碼實(shí)現(xiàn)
- 代碼測試:
- 附錄:交叉熵求偏導(dǎo)推導(dǎo):
softmax
softmax函數(shù)所表示的可以看成為對分類結(jié)果的概率分布。
softmax 的損失函數(shù):交叉熵
他可以規(guī)避sigmoid函數(shù)梯度消失的問題。
交叉熵求偏導(dǎo)
可以看出和MSE是一模一樣的
代碼實(shí)現(xiàn)
class NN:def __init__(self, ws=None):self._ws = ws@staticmethoddef relu(x):return np.maximum(0,x)@staticmethoddef softmax(x):# exp_x ranges from 0 to 1,for OverflowErrorexp_x = np.exp(x - np.max(x, axis=1, keepdims=True))return exp_x / np.sum(exp_x, axis=1, keepdims=True)@staticmethoddef corss_entropy(y_pred, y_true):return -np.average(y*np.log(np.maximum(y_pred, 1e-12)) +(1-y) * np.log(np.maximum(1-y_pred, 1e-12)))# hidden_dim is the hidden units mdef fit(self, x, y, hidden_dim=4, lr=1e-3, epoch=1000):input_dim, output_dim = x.shape[1], y.shape[1]if self._ws is None:self._ws = [np.random.random([input_dim, hidden_dim]),np.random.random([hidden_dim, output_dim])]losses = []for _ in range(epoch):# forward passh = x.dot(self._ws[0])h_relu = NN.relu(h)y_pred = NN.softmax(h_relu.dot(self._ws[1]))losses.append(NN.corss_entropy(y_pred, y))# backford pass# ?L/?y_ ,Y_ is h_relu.dot(self._ws[1]),the input of softmax# this is the key, the different between softmax-cross-entropy # and msed1 = y_pred-y# ?L/?w2 = ?y_pred/?w2* ?L/?y_pred# ?y_pred/?w2= h_relu.Tdw2 = h_relu.T.dot(d1)# ?L/?w2 = ?H/?w2* ?L/?H# ?L/?H = ?L/?y_pred * w2^T * relu'dw1 = x.T.dot(d1.dot(self._ws[1].T)*(h_relu != 0))# uodate wself._ws[0] -= lr*dw1self._ws[1] -= lr*dw2return lossesdef predict(self,x):h = x.dot(self._ws[0])h_relu = NN.relu(h)# 由于 Softmax 不影響 argmax 的結(jié)果,所以這里直接 argmax h_relu.dot(self._ws[1])即可y_pred = NN.softmax(h_relu.dot(self._ws[1]))return np.argmax(y_pred, axis=1)代碼測試:
x, y = gen_five_clusters() label = np.argmax(y, axis=1) nn = NN() losses = nn.fit(x, y, 32, 1e-4) visualize2d(nn, x, label, draw_background=True) print("準(zhǔn)確率:{:8.6} %".format((nn.predict(x) == label).mean() * 100))plt.figure() plt.plot(np.arange(1, len(losses)+1), losses) plt.show()準(zhǔn)確率: 74.5 %
附錄:交叉熵求偏導(dǎo)推導(dǎo):
總結(jié)
以上是生活随笔為你收集整理的深度学习:神经网络,softmax + cross entropy,非tensorflow方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习:从MLP到朴素的神经网络
- 下一篇: 深度学习:tensorflow的简单用法