本次任務(wù)是進(jìn)行氣溫預(yù)測(cè),數(shù)據(jù)集鏈接https://www.kaggle.com/datasets/ns0720/tempscsv,數(shù)據(jù)集下載有困難的評(píng)論區(qū)留言,作為全面學(xué)習(xí)PyTorch實(shí)戰(zhàn)的第一章,我們會(huì)使用比較原始的方法寫(xiě)整個(gè)訓(xùn)練過(guò)程,除了反向傳播由PyTorch代碼調(diào)用自行計(jì)算。
數(shù)據(jù)集介紹數(shù)據(jù)集是csv文件,他飽含9列,按順序分別是year,month,day,week,temp_1,temp_2,average,actual,friend。我們的訓(xùn)練數(shù)據(jù)集為除了actual的所有列,訓(xùn)練數(shù)據(jù)集的標(biāo)簽為actual。數(shù)據(jù)的預(yù)處理我們展示在代碼中。
注意代碼執(zhí)行環(huán)境要在PredictionTemps目錄下,否則會(huì)報(bào)temps.csv文件找不到。
from ast import increment_lineno
from audioop import bias
from calendar import month
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.optim as optim
import warnings
warnings.filterwarnings("ignore")
# %matplotlib inline
#數(shù)據(jù)讀取
features = pd.read_csv('temps.csv')
#查看數(shù)據(jù)
print(features.head())
print("數(shù)據(jù)維度", features.shape)
#處理數(shù)據(jù),轉(zhuǎn)換時(shí)間類(lèi)型
import datetime
#年,月,日
years = features['year']
months = features['month']
days = features['day']
# datetime格式
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]
# 查看數(shù)據(jù)格式
print(dates[:5])
# week列為字符串不是數(shù)值,利用獨(dú)熱編碼,將數(shù)據(jù)中非字符串轉(zhuǎn)換為數(shù)值,并拼接到數(shù)據(jù)中
features = pd.get_dummies(features)
# 看獨(dú)熱編碼的效果
print(features.head(5))
# 標(biāo)簽
labels = np.array(features['actual'])
# 去掉標(biāo)簽用作特征
features = features.drop('actual', axis=1)
# 保存列名用于展示
features_list = list(features.columns)
# 轉(zhuǎn)換為合適的格式
features = np.array(features)
print(features.shape)
# 數(shù)據(jù)標(biāo)準(zhǔn)化
from sklearn import preprocessing
input_features = preprocessing.StandardScaler().fit_transform(features)
# 看一下數(shù)字標(biāo)準(zhǔn)化的效果
print(input_features[0])
接下來(lái)構(gòu)建神經(jīng)網(wǎng)絡(luò)模型,首先使用原始的方法
# 將輸入和預(yù)測(cè)轉(zhuǎn)為tensor
x = torch.tensor(input_features, dtype=float)
y = torch.tensor(labels,dtype=float)
# 權(quán)重參數(shù)初始化
weights = torch.randn((14, 128), dtype= float, requires_grad= True)
biases = torch.randn(128, dtype=float, requires_grad= True)
weights2 = torch.randn((128, 1), dtype=float, requires_grad= True)
biases2 = torch.randn(1, dtype=float, requires_grad=True)
learning_rate = 0.001
losses = []
for i in range(1000):
# 前向傳播
# 計(jì)算隱藏層
hidden = x.mm(weights) + biases
# 加入激活函數(shù)
hidden = torch.relu(hidden)
# 預(yù)測(cè)結(jié)果
predictions = hidden.mm(weights2) + biases2
# 計(jì)算損失
loss = torch.mean((predictions - y)**2)
losses.append(loss.data.numpy())
# 打印損失
if i % 100 == 0:
print('loss:', loss)
# 反向傳播
loss.backward()
# 更新參數(shù)
weights.data.add_(- learning_rate * weights.grad.data)
biases.data.add_(- learning_rate * biases.grad.data)
weights2.data.add_(- learning_rate * weights2.grad.data)
biases2.data.add_(- learning_rate * biases2.grad.data)
# 梯度清零
weights.grad.data.zero_()
biases.grad.data.zero_()
weights2.grad.data.zero_()
biases2.grad.data.zero_()
訓(xùn)練結(jié)果
loss: tensor(3511.3141, dtype=torch.float64, grad_fn=)
loss: tensor(154.7521, dtype=torch.float64, grad_fn=)
loss: tensor(146.5845, dtype=torch.float64, grad_fn=)
loss: tensor(144.1342, dtype=torch.float64, grad_fn=)
loss: tensor(142.9047, dtype=torch.float64, grad_fn=)
loss: tensor(142.1384, dtype=torch.float64, grad_fn=)
loss: tensor(141.5937, dtype=torch.float64, grad_fn=)
loss: tensor(141.1904, dtype=torch.float64, grad_fn=)
loss: tensor(140.8811, dtype=torch.float64, grad_fn=)
loss: tensor(140.6381, dtype=torch.float64, grad_fn=)
loss穩(wěn)步下降
或者我們使用簡(jiǎn)化的方法
input_size = input_features.shape[1]
hidden_size = 128
output_size = 1
batch_size = 16
my_nn = torch.nn.Sequential(
torch.nn.Linear(input_size, hidden_size),
torch.nn.Sigmoid(),
torch.nn.Linear(hidden_size, output_size),
)
# 指定損失函數(shù)
cost = torch.nn.MSELoss(reduction='mean')
# 指定優(yōu)化器
optimizer = torch.optim.Adam(my_nn.parameters(), lr=0.001)
# 訓(xùn)練網(wǎng)絡(luò)
losses = []
for i in range(1000):
batch_loss = []
for start in range(0, len(input_features), batch_size):
end = start + batch_size if start + batch_size< len(input_features) else len(input_features)
xx = torch.tensor(input_features[start:end], dtype=torch.float, requires_grad=True)
yy = torch.tensor(labels[start:end], dtype=torch.float, requires_grad=True)
prediction = my_nn(xx)
loss = cost(prediction, yy)
optimizer.zero_grad()
loss.backward(retain_graph=True)
optimizer.step()
batch_loss.append(loss.data.numpy())
if i % 100 == 0:
losses.append(np.mean(batch_loss))
print(i, np.mean(batch_loss))
最終我們進(jìn)行預(yù)測(cè),并以圖片的形式展示
# 預(yù)測(cè)結(jié)果
x = torch.tensor(input_features, dtype=torch.float)
predict = my_nn(x).data.numpy() # 轉(zhuǎn)化為numpy格式,tensor格式畫(huà)不了圖
# 轉(zhuǎn)換日期格式
dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
dates = [datetime.datetime.strptime(date, '%Y-%m-%d') for date in dates]
# 創(chuàng)建一個(gè)表格來(lái)保存日期和其對(duì)應(yīng)的標(biāo)簽數(shù)值
true_data = pd.DataFrame(data={'date': dates, 'actual': labels})
# 再創(chuàng)建一個(gè)來(lái)存日期和其對(duì)應(yīng)的模型預(yù)測(cè)值
months = features[:, features_list.index('month')]
days = features[:, features_list.index('day')]
years = features[:, features_list.index('year')]
test_dates = [str(int(year)) + '-' + str(int(month)) + '-' + str(int(day)) for year, month, day in zip(years, months, days)]
test_dates = dates
predictions_data = pd.DataFrame(data={'date': test_dates, 'prediction': predict.reshape(-1)})
# 真實(shí)值
plt.plot(true_data['date'], true_data['actual'], 'b-', label = 'actual')
# 預(yù)測(cè)值
plt.plot(predictions_data['date'], predictions_data['prediction'], 'ro', label='prediction')
plt.xticks(rotation='vertical');
plt.legend()
# 圖名
plt.xlabel('Date')
plt.ylabel('Maximum Temperature (F)')
plt.title('Actual and Predicted Values')
plt.show()
結(jié)果展示:
說(shuō)明:代碼執(zhí)行中所需要的包請(qǐng)自行pip install xx下載
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧