参照肖智清老师的《神经网络与PyTorch实战》
import torch.nn
import torch
from pandas_datareader import wb
import torch.optim
import pandas as pd
countries = ['BR', 'CN', 'FR', 'DE', 'IN', 'JP', 'SA', 'GB', 'US']
dat = wb.download(indicator='NY.GDP.PCAP.KD', country=countries, start=1970, end=2016)
df = dat.unstack().T
df.index = df.index.droplevel(0)
class Net(torch.nn.Module):
def __init__(self, input_size, hidden_size):
super(Net, self).__init__()
self.rnn = torch.nn.LSTM(input_size=input_size, hidden_size=hidden_size)
self.fc = torch.nn.Linear(hidden_size, 1)
def forward(self, x):
x = x[:, :, None]
x, _ = self.rnn(x)
x = self.fc(x)
x = x[:, :, 0]
return x
net = Net(input_size=1, hidden_size=5)
df_scaled = df / df.std()
years = df.index
train_seq_len = sum((years >= '1971') & (years <= '2000'))
test_seq_len = sum(years > '2000')
print(f'Train length: {train_seq_len}, Test length: {test_seq_len}.')
inputs = torch.tensor(df_scaled.iloc[:-1].values, dtype=torch.float32)
labels = torch.tensor(df_scaled.iloc[1:].values, dtype=torch.float32)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(net.parameters())
for step in range(10001):
if step:
optimizer.zero_grad()
train_loss.backward()
optimizer.step()
preds = net(inputs)
train_preds = preds[:train_seq_len]
train_labels = labels[:train_seq_len]
train_loss = criterion(train_preds, train_labels)
test_preds = preds[-test_seq_len]
test_labels = labels[-test_seq_len]
test_loss = criterion(test_preds, test_labels)
if step % 500 == 0:
print(f'Step: {step}, Test loss: {test_loss:.001%}, Train loss: {train_loss:.001%}')
- 原文中有两个国家(加拿大和以色列),数据有空缺,运行时,loss函数会一直出现nan
为了避免这种情况,把这两个国家删掉 - 这段程序的input是(46, 11),46应该对应的是batch_size,但作者没有进行修正,直接导入了net,这导致,rnn中的第一项seq_len对应成了46, 第二项batch_size对应11,与输入的含义不符,相当于进行了一次batch_first = True,这样做不影响结果,只是含义不符。有点别扭。可以在37,38行插入下面两句话:
inputs = inputs.T
labels = labels.T
48, 49行修改为:
train_preds = preds[:, :train_seq_len]
train_labels = labels[:, :train_seq_len]
52,53行修改为:
test_preds = preds[:, :-test_seq_len]
test_labels = labels[:, :-test_seq_len]
|