PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測_第1頁
PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測_第2頁
PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測_第3頁
PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測_第4頁
PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第PyTorch搭建ANN實(shí)現(xiàn)時(shí)間序列風(fēng)速預(yù)測目錄數(shù)據(jù)集特征構(gòu)造數(shù)據(jù)處理1.數(shù)據(jù)預(yù)處理2.數(shù)據(jù)集構(gòu)造ANN模型1.模型訓(xùn)練2.模型預(yù)測及表現(xiàn)

數(shù)據(jù)集

數(shù)據(jù)集為Barcelona某段時(shí)間內(nèi)的氣象數(shù)據(jù),其中包括溫度、濕度以及風(fēng)速等。本文將簡單搭建來對風(fēng)速進(jìn)行預(yù)測。

特征構(gòu)造

對于風(fēng)速的預(yù)測,除了考慮歷史風(fēng)速數(shù)據(jù)外,還應(yīng)該充分考慮其余氣象因素的影響。因此,我們根據(jù)前24個(gè)時(shí)刻的風(fēng)速+下一時(shí)刻的其余氣象數(shù)據(jù)來預(yù)測下一時(shí)刻的風(fēng)速。

數(shù)據(jù)處理

1.數(shù)據(jù)預(yù)處理

數(shù)據(jù)預(yù)處理階段,主要將某些列上的文本數(shù)據(jù)轉(zhuǎn)為數(shù)值型數(shù)據(jù),同時(shí)對原始數(shù)據(jù)進(jìn)行歸一化處理。文本數(shù)據(jù)如下所示:

經(jīng)過轉(zhuǎn)換后,上述各個(gè)類別分別被賦予不同的數(shù)值,比如skyisclear為0,fewclouds為1。

defload_data():

globalMax,Min

df=pd.read_csv('Barcelona/Barcelona.csv')

df.drop_duplicates(subset=[df.columns[0]],inplace=True)

#weather_main

listType=df['weather_main'].unique()

df.fillna(method='ffill',inplace=True)

dic=dict.fromkeys(listType)

foriinrange(len(listType)):

dic[listType[i]]=i

df['weather_main']=df['weather_main'].map(dic)

#weather_description

listType=df['weather_description'].unique()

dic=dict.fromkeys(listType)

foriinrange(len(listType)):

dic[listType[i]]=i

df['weather_description']=df['weather_description'].map(dic)

#weather_icon

listType=df['weather_icon'].unique()

dic=dict.fromkeys(listType)

foriinrange(len(listType)):

dic[listType[i]]=i

df['weather_icon']=df['weather_icon'].map(dic)

#print(df)

columns=df.columns

Max=np.max(df['wind_speed'])#歸一化

Min=np.min(df['wind_speed'])

foriinrange(2,17):

column=columns[i]

ifcolumn=='wind_speed':

continue

df[column]=df[column].astype('float64')

iflen(df[df[column]==0])==len(df):#全0

continue

mx=np.max(df[column])

mn=np.min(df[column])

df[column]=(df[column]-mn)/(mx-mn)

#print(df.isna().sum())

returndf

2.數(shù)據(jù)集構(gòu)造

利用當(dāng)前時(shí)刻的氣象數(shù)據(jù)和前24個(gè)小時(shí)的風(fēng)速數(shù)據(jù)來預(yù)測當(dāng)前時(shí)刻的風(fēng)速:

defnn_seq():

:paramflag:

:paramdata:待處理的數(shù)據(jù)

:return:X和Y兩個(gè)數(shù)據(jù)集,X=[當(dāng)前時(shí)刻的year,month,hour,day,lowtemp,hightemp,前一天當(dāng)前時(shí)刻的負(fù)荷以及前23小時(shí)負(fù)荷]

Y=[當(dāng)前時(shí)刻負(fù)荷]

print('處理數(shù)據(jù):')

data=load_data()

speed=data['wind_speed']

speed=speed.tolist()

speed=torch.FloatTensor(speed).view(-1)

data=data.values.tolist()

seq=[]

foriinrange(len(data)-30):

train_seq=[]

train_label=[]

forjinrange(i,i+24):

train_seq.append(speed[j])

#添加溫度、濕度、氣壓等信息

forcinrange(2,7):

train_seq.append(data[i+24][c])

forcinrange(8,17):

train_seq.append(data[i+24][c])

train_label.append(speed[i+24])

train_seq=torch.FloatTensor(train_seq).view(-1)

train_label=torch.FloatTensor(train_label).view(-1)

seq.append((train_seq,train_label))

#print(seq[:5])

Dtr=seq[0:int(len(seq)*0.5)]

Den=seq[int(len(seq)*0.50):int(len(seq)*0.75)]

Dte=seq[int(len(seq)*0.75):len(seq)]

returnDtr,Den,Dte

任意輸出其中一條數(shù)據(jù):

(tensor([1.0000e+00,1.0000e+00,2.0000e+00,1.0000e+00,1.0000e+00,1.0000e+00,

1.0000e+00,1.0000e+00,0.0000e+00,1.0000e+00,5.0000e+00,0.0000e+00,

2.0000e+00,0.0000e+00,0.0000e+00,5.0000e+00,0.0000e+00,2.0000e+00,

2.0000e+00,5.0000e+00,6.0000e+00,5.0000e+00,5.0000e+00,5.0000e+00,

5.3102e-01,5.5466e-01,4.6885e-01,1.0066e-03,5.8000e-01,6.6667e-01,

0.0000e+00,0.0000e+00,0.0000e+00,0.0000e+00,9.9338e-01,0.0000e+00,

0.0000e+00,0.0000e+00]),tensor([5.]))

數(shù)據(jù)被劃分為三部分:Dtr、Den以及Dte,Dtr用作訓(xùn)練集,Dte用作測試集。

ANN模型

1.模型訓(xùn)練

ANN模型搭建如下:

defANN():

Dtr,Den,Dte=nn_seq()

my_nn=torch.nn.Sequential(

torch.nn.Linear(38,64),

torch.nn.ReLU(),

torch.nn.Linear(64,128),

torch.nn.ReLU(),

torch.nn.Linear(128,1),

model=my_nn.to(device)

loss_function=nn.MSELoss().to(device)

optimizer=torch.optim.Adam(model.parameters(),lr=0.001)

train_inout_seq=Dtr

#訓(xùn)練

epochs=50

foriinrange(epochs):

print('當(dāng)前',i)

forseq,labelsintrain_inout_seq:

seq=seq.to(device)

labels=labels.to(device)

y_pred=model(seq)

single_loss=loss_function(y_pred,labels)

optimizer.zero_grad()

single_loss.backward()

optimizer.step()

#ifi%2==1:

print(f'epoch:{i:3}loss:{single_loss.item():10.8f}')

print(f'epoch:{i:3}loss:{single_loss.item():10.10f}')

state={'model':model.state_dict(),'optimizer':optimizer.state_dict(),'epoch':epochs}

torch.save(state,'Barcelona'+ANN_PATH)

可以看到,模型定義的代碼段為:

my_nn=torch.nn.Sequential(

torch.nn.Linear(38,64),

torch.nn.ReLU(),

torch.nn.Linear(64,128),

torch.nn.ReLU(),

torch.nn.Linear(128,1),

第一層全連接層輸入維度為38(前24小時(shí)風(fēng)速+14種氣象數(shù)據(jù)),輸出維度為64;第二層輸入為64,輸出128;第三層輸入為128,輸出為1。

2.模型預(yù)測及表現(xiàn)

defANN_predict(ann,test_seq):

pred=[]

forseq,labelsintest_seq:

seq=seq.to(device)

withtorch.no_grad():

pred.append(ann(seq).item())

pred=np.array([pred])

returnpred

測試:

deftest():

Dtr,Den,Dte=nn_seq()

ann=torch.nn.Sequential(

torch.nn.Linear(38,64),

torch.nn.ReLU(),

torch.nn.Linear(64,128),

torch.nn.ReLU(),

torch.nn.Linear(128,1),

ann=ann.to(device)

ann.load_state_dict(torch.load('Barcelona'+

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論