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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

DL之DNN:自定义2层神经网络TwoLayerNet模型(封装为层级结构)利用MNIST数据集进行训练、GC对比

發(fā)布時(shí)間:2025/3/21 编程问答 53 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DL之DNN:自定义2层神经网络TwoLayerNet模型(封装为层级结构)利用MNIST数据集进行训练、GC对比 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

DL之DNN:自定義2層神經(jīng)網(wǎng)絡(luò)TwoLayerNet模型(封裝為層級結(jié)構(gòu))利用MNIST數(shù)據(jù)集進(jìn)行訓(xùn)練、GC對比

導(dǎo)讀
? ? ? ? ? 神經(jīng)網(wǎng)絡(luò)算法封裝為層級結(jié)構(gòu)的作用。在神經(jīng)網(wǎng)絡(luò)算法中,通過將神經(jīng)網(wǎng)絡(luò)的組成元素實(shí)現(xiàn)為層,可以高效地計(jì)算梯度(反向傳播法)。通過比較數(shù)值微分和誤差反向傳播法的結(jié)果,可以確認(rèn)誤差反向傳播法的實(shí)現(xiàn)是否正確(梯度確認(rèn))。

?

?

目錄

輸出結(jié)果

設(shè)計(jì)思路

核心代碼

代碼實(shí)現(xiàn)過程錯(cuò)誤記錄


?

?

?

輸出結(jié)果

?

設(shè)計(jì)思路

?

核心代碼

class TwoLayerNet:def __init__(self, input_size, hidden_size, output_size, weight_init_std = 0.01):self.params = {}self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)self.params['b1'] = np.zeros(hidden_size)self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size) self.params['b2'] = np.zeros(output_size)self.layers = OrderedDict()self.layers['Affine1'] = Affine(self.params['W1'], self.params['b1'])self.layers['Relu1'] = Relu()self.layers['Affine2'] = Affine(self.params['W2'], self.params['b2'])self.lastLayer = SoftmaxWithLoss()def predict(self, x):for layer in self.layers.values():x = layer.forward(x)return x# x:輸入數(shù)據(jù), t:監(jiān)督數(shù)據(jù)def loss(self, x, t):y = self.predict(x)return self.lastLayer.forward(y, t)def accuracy(self, x, t):y = self.predict(x)y = np.argmax(y, axis=1)if t.ndim != 1 : t = np.argmax(t, axis=1)accuracy = np.sum(y == t) / float(x.shape[0])return accuracydef gradient(self, x, t):self.loss(x, t)dout = 1dout = self.lastLayer.backward(dout)layers = list(self.layers.values())layers.reverse()for layer in layers:dout = layer.backward(dout)grads = {}grads['W1'], grads['b1'] = self.layers['Affine1'].dW, self.layers['Affine1'].dbgrads['W2'], grads['b2'] = self.layers['Affine2'].dW, self.layers['Affine2'].dbreturn gradsnetwork_batch = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)grad_numerical = network_batch.numerical_gradient_api(x_batch, t_batch) grad_backprop = network_batch.gradient(x_batch, t_batch)

?

?

?

代碼實(shí)現(xiàn)過程錯(cuò)誤記錄

出現(xiàn)錯(cuò)誤,待解決!!!

Traceback (most recent call last):
? File "F:\File_Python\Python_daydayup\190316.py", line 281, in <module>
? ? grad = network.gradient(x_batch, t_batch) ? ? ? ? ? ??
? File "F:\File_Python\Python_daydayup\190316.py", line 222, in gradient
? ? self.loss(x, t)
? File "F:\File_Python\Python_daydayup\190316.py", line 193, in loss
? ? return self.lastLayer.forward(y, t) # ? ? ? ? ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆ ? ------部分更改
? File "F:\File_Python\Python_daydayup\190316.py", line 132, in forward
? ? self.loss = cross_entropy_error(self.y, self.t)
? File "F:\File_Python\Python_daydayup\190316.py", line 39, in cross_entropy_error
? ? return -np.sum(np.log(y[np.arange(batch_size), t.astype('int64')] + 1e-7)) / batch_size ? ? ? ? ?#t.astype('int64')
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (100,) (100,10)?

?

?

?

相關(guān)文章
DL之DNN:自定義2層神經(jīng)網(wǎng)絡(luò)TwoLayerNet模型(層級結(jié)構(gòu)更高效)算法對MNIST數(shù)據(jù)集進(jìn)行訓(xùn)練、預(yù)測

?

總結(jié)

以上是生活随笔為你收集整理的DL之DNN:自定义2层神经网络TwoLayerNet模型(封装为层级结构)利用MNIST数据集进行训练、GC对比的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。