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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[pytorch、学习] - 3.9 多重感知机的从零开始实现

發布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [pytorch、学习] - 3.9 多重感知机的从零开始实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考

3.9 多重感知機的從零開始實現

import torch import numpy as np import sys sys.path.append("..") import d2lzh_pytorch as d2l

3.9.1. 獲取和讀取數據

batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

3.9.2. 定義模型參數

num_inputs, num_outputs, num_hiddens = 784, 10, 256W1 = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_hiddens)), dtype=torch.float) b1 = torch.zeros(num_hiddens, dtype=torch.float) W2 = torch.tensor(np.random.normal(0, 0.01, (num_hiddens, num_outputs)), dtype=torch.float) b2 = torch.zeros(num_outputs, dtype=torch.float)params = [W1, b1, W2, b2] for param in params:param.requires_grad_(requires_grad=True)

3.9.3. 定義激活函數

def relu(X):return torch.max(input=X, other=torch.tensor(0.0))

3.9.4. 定義模型

def net(X):X = X.view((-1, num_inputs))H = relu(torch.matmul(X, W1) + b1)return torch.matmul(H, W2) + b2

3.9.5. 定義損失函數

loss = torch.nn.CrossEntropyLoss()

3.9.6. 訓練模型

num_epochs, lr = 5, 100.0 d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)

3.9.7. 預測

X, y = iter(test_iter).next()true_labels = d2l.get_fashion_mnist_labels(y.numpy()) pred_labels = d2l.get_fashion_mnist_labels(net(X).argmax(dim=1).numpy()) titles = [true + '\n' + pred for true, pred in zip(true_labels, pred_labels)]d2l.show_fashion_mnist(X[0:9], titles[0:9])

總結

以上是生活随笔為你收集整理的[pytorch、学习] - 3.9 多重感知机的从零开始实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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