pytorch入門2.x構建迴歸模型系列:
pytorch入門2.0構建迴歸模型初體驗(數據生成)
pytorch入門2.1構建迴歸模型初體驗(模型構建)
pytorch入門2.2構建迴歸模型初體驗(開始訓練)html
pytorch對於神經網絡有很好的封裝,使得咱們能夠快速、簡單的實現神經網絡框架的編寫。
0. 準備數據,並對數據集進行劃分。劃分其實有不少方法:見數據集劃分實戰codepython
# 準備數據 import random x = torch.unsqueeze(torch.linspace(0,1,300),dim=1) # 從0到1生成300個數據 y = x*8-5+torch.rand(x.size()) # 生成groundtruth數據 # shuffle your trainning data index = torch.randperm(x.nelement()) index_train = index[:int(index.nelement()*0.8)] index_test = index[int(index.nelement()*0.8):] x_train = x[index_train] y_train = y[index_train] x_test = x[index_test] y_test = y[index_test] assert(x_train.nelement()+x_test.nelement()==x.nelement())
數據不夠直觀,咱們把他們畫出來看看:網絡
import matplotlib.pyplot as plt %matplotlib inline plt.figure(figsize=(10,8)) plt.scatter(x_train,y_train) plt.scatter(x_test,y_test) plt.show()
其中藍色的點表明咱們的訓練數據,紅色的點表明咱們的測試數據。接下來,咱們就要用藍色的點進行訓練一個迴歸模型,而後在紅色的點集合上面去測試結果啦。抓緊來試試吧。框架