So ez~
這裡我們使用我做實驗(相關實驗請見)做的資料如下:
間距/第n次實驗 | EXP1 | EXP2 | EXP3 | EXP4 | EXP5 | |||
---|---|---|---|---|---|---|---|---|
5cm | 0.85 | 1.00 | 1.00 | 0.95 | 1.05 | 0.97 | 0.14 | 0.06 |
6cm | 0.80 | 0.95 | 0.90 | 1.05 | 1.15 | 0.97 | 0.25 | 0.11 |
7cm | 1.00 | 0.95 | 0.75 | 0.75 | 0.60 | 0.81 | 0.28 | 0.13 |
8cm | 0.85 | 0.71 | 0.71 | 0.65 | 0.65 | 0.72 | 0.14 | 0.06 |
9cm | 0.85 | 1.00 | 1.00 | 0.60 | 1.00 | 0.88 | 0.11 | 0.05 |
其中,我們就挑間距(5cm,6cm,7cm…)當作自變數(
下code!
import torch
x = torch.tensor([[5],[6],[7],[8],[9]],dtype=torch.float32)
y = torch.tensor([[0.97],[0.97],[0.81],[0.72],[0.88]],dtype=torch.float32)
在這裡,我們要使用到
import torch.nn as nn
這個模組、要用class來進行模型建置
直接看code
class linearRegression(nn.Module):
def __init__(self):
super(linearRegression,self).__init__()
self.linear = nn.Linear(1,1)
def forward(self,x):
x = self.linear(x)
return x
Linalg = linearRegression()
解釋一下:
首先先從nn.Module繼承用法下來,將self.linear設為nn.Linear(1 IN 1 OUT)
首先,要先選擇誤差函數,在這裡,我們選用了MSELOSS。 MSELOSS可以想像成最小平方法
loss_function = nn.MSELoss()
接著在選擇優化器,這邊我們用了隨機梯度下降法(SGD)
import torch.optim as optim
optimizer = torch.optim.SGD(Linalg.parameters(), lr=0.1)
開始訓練!
epochs = 10
for epoch in range(epochs):
# 前向傳播
y_hat = Linalg(x)
# 計算損失
loss = loss_function(y_hat, y)
# 清零梯度
optimizer.zero_grad()
# 反向傳播
loss.backward()
# 更新參數
optimizer.step()
torch.save(Linalg,"linalg.pt")
訓練完成!
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:00<00:00, 316.48it/s]
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import trange
import matplotlib.pyplot as plt
x = torch.tensor([[5], [6], [7], [8], [9]], dtype=torch.float32)
y = torch.tensor([[0.97], [0.97], [0.81], [0.72], [0.88]], dtype=torch.float32)
class LinearRegression(nn.Module):
def __init__(self):
super(LinearRegression, self).__init__()
self.linear = nn.Linear(1, 1)
def forward(self, x):
return self.linear(x)
model = LinearRegression()
loss_function = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)
epochs = 100
losses = []
for epoch in trange(epochs):
# 前向傳播
y_pred = model(x)
loss = loss_function(y_pred, y)
losses.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
torch.save(model, "linear_regression_model.pt")
plt.figure(figsize=(10, 5))
plt.plot(range(epochs), losses)
plt.xlabel('Epoch')
plt.ylabel('MSE Loss')
plt.title('Loss Over Time')
plt.show()
with torch.no_grad():
predictions = model(x)
plt.scatter(x.numpy(), y.numpy(), label='Actual Data', color='red')
plt.plot(x.numpy(), predictions.numpy(), label='Linear Regression Line', color='blue')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression Model')
plt.legend()
plt.show()