3 10 多層感知機的簡潔實現

2021-10-07 07:52:02 字數 1408 閱讀 9244

學習**:

import torch

from torch import nn

from torch.nn import init

import numpy as np

import sys

".."

)import d2lzh_pytorch as d2l

加了乙個全連線層作為隱藏層。它的隱藏單元個數為256,並使用relu函式作為啟用函式。

num_inputs, num_outputs, num_hiddens =

784,10,

256net = nn.sequential(

d2l.flattenlayer(),

nn.linear(num_inputs, num_hiddens)

, nn.relu(),

nn.linear(num_hiddens, num_outputs),)

for params in net.parameters():

init.normal_(params, mean=

0, std=

0.01

)

使用的是pytorch的sgd而不是d2lzh_pytorch裡面的sgd,所以就不存在3.9節那樣學習率看起來很大的問題了。

batch_size =

256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

loss = torch.nn.crossentropyloss(

)optimizer = torch.optim.sgd(net.parameters(

), lr=

0.5)

num_epochs =

5d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,

none

,none

, optimizer)

epoch 1, loss 0.0031, train acc 0.699, test acc 0.748

epoch 2, loss 0.0019, train acc 0.822, test acc 0.772

epoch 3, loss 0.0017, train acc 0.842, test acc 0.834

epoch 4, loss 0.0015, train acc 0.857, test acc 0.851

epoch 5, loss 0.0014, train acc 0.865, test acc 0.852

多層感知機的簡潔實現

動手學深度學習pytorch 部分學習筆記,僅用作自己複習。使用gluon來實現上一節中的多層感知機。import torch from torch import nn from torch.nn import init import numpy as np import sys import d2...

tensorflow實現多層感知機

tensorflow 多層感知機識別手寫數字 匯入資料 import tensorflow as tf import tensorflow.tutorials.minist.input data as input data minist input data.read datasets mnist ...

多層感知機

1 單輸出多層感知機 單輸出多層感知機 圖中各變數滿足公式 假如現在有乙個樣本 x 1,x2 t 用該樣本訓練網路時,網路優化的目標為lossfun最小。lossfun是乙個關於變數w和v多元函式。網路的訓練又變成多元函式求極值的問題。求v1的梯度 同理可得v i的梯度 求w11的梯度 同理,可得w...