日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

深度学习:神经网络,softmax + cross entropy,非tensorflow方式

發布時間:2024/9/15 pytorch 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 深度学习:神经网络,softmax + cross entropy,非tensorflow方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • softmax
  • softmax 的損失函數:交叉熵
  • 交叉熵求偏導
  • 代碼實現
  • 代碼測試:
  • 附錄:交叉熵求偏導推導:

softmax

softmax函數所表示的可以看成為對分類結果的概率分布。

softmax 的損失函數:交叉熵


他可以規避sigmoid函數梯度消失的問題。

交叉熵求偏導

可以看出和MSE是一模一樣的

代碼實現

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 的結果,所以這里直接 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("準確率:{:8.6} %".format((nn.predict(x) == label).mean() * 100))plt.figure() plt.plot(np.arange(1, len(losses)+1), losses) plt.show()

準確率: 74.5 %

附錄:交叉熵求偏導推導:



總結

以上是生活随笔為你收集整理的深度学习:神经网络,softmax + cross entropy,非tensorflow方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。