PyTorchを使用して、複数のLSTM層を持つモデルの構築の仕方がわからないです。
以下のようにコードを書いてみたのですが、num_layersを3などに設定すると、エラーとなってしまう状態です。
RuntimeError: mat1 and mat2 shapes cannot be multiplied (300x300 and 100x1)
恐らく、データの構造が誤っているのかと思うのですが、どのように修正したら良いのでしょうか。
import torch import torch.nn as nn from torch.utils.data import DataLoader import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler def getDataset(): df = pd.read_csv("test.csv", index_col=0, parse_dates=True) df = df.sort_index() df = df.dropna() dataset = df['2021-01-01 00:00':'2021-01-31 23:59'].values scaler = MinMaxScaler() train_data_normalized = scaler.fit_transform(dataset.reshape(-1, 1)) test_dataset = df['2021-02-01 00:00':'2021-02-28 23:59'].values test_dataset = scaler.fit_transform(test_dataset) test_data_normalized = scaler.fit_transform(test_dataset.reshape(-1, 1)) return torch.FloatTensor(train_data_normalized).view(-1), torch.FloatTensor(test_data_normalized) #return train_data_normalized, test_data_normalized def createInOutSequences(input_data, lookback): #inout_seq = [] L = len(input_data) X_train, y_train = [], [] for i in range(L - lookback): #train_seq = input_data[i:i+lookback] #train_label = input_data[i+lookback:i+lookback+1] #inout_seq.append((train_seq, train_label)) X_train.append(input_data[i:i+lookback]) y_train.append(input_data[i+lookback]) return X_train, y_train def prepareFeatureData(batch_idx, time_steps, X_data, feature_num): feats = torch.zeros((len(batch_idx), time_steps, feature_num), dtype=torch.float) for b_i, b_idx in enumerate(batch_idx): b_slc = slice(b_idx + 1 - time_steps, b_idx + 1) feats[b_i, :, :] = X_data[b_slc, :] return feats class MyDataset(torch.utils.data.Dataset): def __init__(self, X, y): self.data = X self.teacher = y def __len__(self): return len(self.teacher) def __getitem__(self, idx): out_data = self.data[idx] out_label = self.teacher[idx] return out_data, out_label class LSTM(nn.Module): def __init__(self, input_size=1, hidden_layer_size=100, output_size=1, batch_size=32, num_layers=3, nonlinearity='relu'): super().__init__() self.input_size = input_size self.hidden_layer_size = hidden_layer_size self.batch_size = batch_size self.num_layers = num_layers self.nonliniearity = nonlinearity self.lstm = nn.LSTM(input_size, hidden_layer_size, num_layers) self.linear = nn.Linear(hidden_layer_size, output_size) self.hidden_cell = ( torch.zeros(self.num_layers, self.batch_size, self.hidden_layer_size), torch.zeros(self.num_layers, self.batch_size, self.hidden_layer_size) ) def forward(self, input_seq): print(input_seq.shape) batch_size, seq_len = input_seq.shape[0], input_seq.shape[1] lstm_out, self.hidden_cell = self.lstm( input_seq.view(seq_len, batch_size, 1), self.hidden_cell ) predictions = self.linear(self.hidden_cell[0].view(batch_size, -1)) return predictions[:, 0] def doPrediction(model_path, lookback, train_data): model = LSTM(hidden_layer_size=100, batch_size=1, num_layers=1) model.load_state_dict(torch.load(model_path)) results = model.eval() print(results) print(model) future_pred = 20 * 24 test_inputs = train_data[-lookback:].tolist() for i in range(future_pred): seq = torch.FloatTensor(test_inputs[-lookback:]).view(-1, 480) with torch.no_grad(): model.hidden = ( torch.zeros(1, len(seq), model.hidden_layer_size), torch.zeros(1, len(seq), model.hidden_layer_size) ) test_inputs.append(model(seq).item()) plt.figure() plt.plot(test_inputs) plt.legend() plt.show() def main(): lookback = 20 * 24 batch_size = 300 train_data, test_data = getDataset() #train_seq = createInOutSequences(train_data, lookback) train_X, train_y = createInOutSequences(train_data, lookback) dataset = MyDataset(train_X, train_y) dataloader = DataLoader(dataset, batch_size=batch_size) model = LSTM(input_size=1, batch_size=300, num_layers=3, output_size=1) loss_function = nn.MSELoss() optimizer = torch.optim.Adam(model.parameters(), lr=0.01) epochs = 150 for i in range(epochs): #for seq, labels in train_seq: for b, tup in enumerate(dataloader): X, y = tup optimizer.zero_grad() model.hidden_cell = ( torch.zeros(model.num_layers, len(X), model.hidden_layer_size), torch.zeros(model.num_layers, len(X), model.hidden_layer_size)) print("X.shape: ", X.shape) y_pred = model(X) single_loss = loss_function(y_pred, y) single_loss.backward() optimizer.step() if i % 25 == 1: print(f'epoch: {i:3} loss: {single_loss.item():10.8f}') print(f'epoch: {i:3} loss: {single_loss.item():10.10f}') results = model.eval() print(results) torch.save(model.state_dict(), 'torch_disk_model_2.pt') doPrediction( 'torch_disk_model_2.pt', lookback, train_data) if __name__ == '__main__': main()
あなたの回答
tips
プレビュー