最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

PyTorch Tutorial 06 - Training Pipeli...

2023-02-15 12:05 作者:Mr-南喬  | 我要投稿

教程Python代碼如下:


# 1) Design model(input, output size, forward pass)

# 2) Construct loss and optimizer

# 3) Training loop 訓練循環(huán)

# - forward pass: compute prediction

# - backward pass: gradients

# - update weights

import torch

import torch.nn as nn #導入神經(jīng)網(wǎng)絡模塊


# f = w * x 此處不加偏置


# f = 2 * x

X = torch.tensor([[1],[2],[3],[4]],dtype=torch.float32)

Y = torch.tensor([[2],[4],[6],[8]],dtype=torch.float32)


X_test = torch.tensor([5],dtype=torch.float32)


n_samples, n_features = X.shape

print(n_samples, n_features)


input_size = n_features

output_size = n_features


#model = nn.Linear(input_size, output_size)


class LinearRegression(nn.Module):


def __init__(self, input_dim, output_dim):

super(LinearRegression, self).__init__()

# define layers

self.lin = nn.Linear(input_dim, output_dim)


def forward(self, x):

return self.lin(x)


model = LinearRegression(input_size, output_size)


print(f'Prediction befor training: f(5) = {model(X_test).item():.3f}')


# Training

# 學習率

learning_rate = 0.01


# 多次迭代

n_iters = 100


# loss = 均方誤差

loss = nn.MSELoss()


# 優(yōu)化器

optimizer = torch.optim.SGD(model.parameters(),lr=learning_rate) #SGD代表隨機梯度下降,參數(shù)列表是它將優(yōu)化的參數(shù)


for epoch in range(n_iters):

# prediction = forward pass

y_pred = model(X)


# loss

l = loss(Y,y_pred)


# gradients = backward pass

l.backward() # dl/dw


# update weights 更新公式:權重 = 權重 - (步長或學習速率 * dw)

optimizer.step()


# zero gradients

optimizer.zero_grad()


#打印每一步

if epoch % 10 == 0:

[w, b] = model.parameters()

print(f'epoch {epoch+1}: w = {w[0][0].item():.3f}, loss = {l:.8f}')


print(f'Prediction after training: f(5) = {model(X_test).item():.3f}')

PyTorch Tutorial 06 - Training Pipeli...的評論 (共 條)

使用qq登录你需要登录后才可以评论。
历史| 筠连县| 新疆| 江津市| 衡水市| 大安市| 枣庄市| 瑞安市| 巴马| 怀柔区| 尼勒克县| 眉山市| 西乌珠穆沁旗| 丹阳市| 洮南市| 姚安县| 大厂| 佛坪县| 射阳县| 洪江市| 通榆县| 文安县| 宣城市| 玉环县| 和政县| 福建省| 连州市| 广西| 高唐县| 谷城县| 通辽市| 合川市| 台中市| 华容县| 沐川县| 澜沧| 奉新县| 昭通市| 博爱县| 克拉玛依市| 宣汉县|