import torch
import torch.nn as nn
# set up loss function as mean square error
def loss(y, y_predicted):
return ((y_predicted-y) ** 2).mean()
# MSE in pytorch
loss = nn.MSELoss()
# set stochastic gradient descent as optimizer
optimizer = torch.optim.SGD([w], lr=learning_rate)
import torch
import torch.nn as nn
# f = w * x
# f = 2 * x, we set w as 2
x = torch.tensor([1, 2, 3, 4, 5, 6], dtype=torch.float32)
y = torch.tensor([2, 4, 6, 8, 10, 12], dtype=torch.float32)
# init weight
# 这边要注意,我们希望 Pytorch 帮我们计算更新的 Gradient 变数是 w,所以一定要开 requires_grad 在这个变数上
w = torch.tensor(0.0, dtype=torch.float32, requires_grad=True)
# model prediction
def forward(x):
return w * x
# Training
learning_rate = 0.01
n_iters = 10
print(f'Prediction before training: f(5) = {forward(5): .3f}')
# MSE in pytorch
loss = nn.MSELoss()
# set stochastic gradient descent as optimizer
optimizer = torch.optim.SGD([w], lr=learning_rate)
for epoch in range(n_iters):
# perdiction = forward pass
y_pred = forward(x)
# loss
l = loss(y, y_pred)
# gradient descent is where calculate gradient and update parameters
# so gradient descent here includes gradients and update weights
# 原本在 Python 的 example 还需要自己建立 Gradient 函式
# gradients = backward pass
l.backward() # calculate dl/dw
# update weights
optimizer.step()
# zero gradients
optimizer.zero_grad()
if epoch % 1 == 0:
print(f'epoch {epoch + 1}: w = {w:.3f}, loss = {l:.8f}')
print(f'Prediction after training: f(5) = {forward(5): .3f}')
# Prediction before training: f(5) = 0.000
# epoch 1: w = 0.607, loss = 60.66666794
# epoch 2: w = 1.029, loss = 29.44422913
# epoch 3: w = 1.324, loss = 14.29059505
# epoch 4: w = 1.529, loss = 6.93586159
# epoch 5: w = 1.672, loss = 3.36628079
# epoch 6: w = 1.771, loss = 1.63380527
# epoch 7: w = 1.841, loss = 0.79295844
# epoch 8: w = 1.889, loss = 0.38485837
# epoch 9: w = 1.923, loss = 0.18678898
# epoch 10: w = 1.946, loss = 0.09065709
# Prediction after training: f(5) = 9.731
# init weight
w = torch.tensor(0.0, dtype=torch.float32, requires_grad=True)
# model prediction
def forward(x):
return w * x
# change data look
x = torch.tensor([[1], [2], [3], [4], [5], [6]], dtype=torch.float32)
y = torch.tensor([[2], [4], [6], [8], [10], [12]], 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)
...
# optimizer 更新就是 model 的 parameters
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(n_iters):
y_pred = model(x)
...
if epoch % 1 == 0:
[w, b] = model.parameters()
print(f'epoch {epoch + 1}: w = {w[0][0]:.3f}, loss = {l:.8f}')
import torch
import torch.nn as nn
# f = w * x + b
# f = 2 * x + 0, we set w as 2, b as 0
x = torch.tensor([[1], [2], [3], [4], [5], [6]], dtype=torch.float32)
y = torch.tensor([[2], [4], [6], [8], [10], [12]], 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)
# Training
learning_rate = 0.01
n_iters = 10
print(f'Prediction before training: f(5) = {model(x_test).item(): .3f}')
# MSE in pytorch
loss = nn.MSELoss()
# set stochastic gradient descent as optimizer
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
for epoch in range(n_iters):
# perdiction = forward pass
y_pred = model(x)
# loss
l = loss(y, y_pred)
# gradient descent is where calculate gradient and update parameters
# so gradient descent here includes gradients and update weights
# 原本在 Python 的 example 还需要自己建立 Gradient 函式
# gradients = backward pass
l.backward() # calculate dl/dw
# update weights
optimizer.step()
# zero gradients
optimizer.zero_grad()
if epoch % 1 == 0:
[w, b] = model.parameters()
print(f'epoch {epoch + 1}: w = {w[0][0]:.3f}, loss = {l:.8f}')
print(f'Prediction after training: f(5) = {model(x_test).item(): .3f}')
<<: android studio 30天学习笔记-day 16-databinding Recyclerview
>>: 【心得】不同 gradient 使用方式-- linear-gradient()
版本与支援 Hooks 是 React 16.8 版本推出的语法,React Native 则是在...
改成使用targetCapital这个变数来控制总部位大小,不用每天开机器人的时候还要算加减多少钱。...
昨天已经学会把ImageView图片设为Android内建的图片 但说实话 内建的图片ICON我到目...
由於 Open-Match 在 service 与 service 之间,是建议使用 gRPC 进行...
前言 不管你是不是学程序的,常常都会接触到网页,常常会听到网页就是HTML、CSS和JavaScri...