Trying to create a simple LSTM model and work with FastAI framework but failed.
class SimpleLSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, bs):
super().__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.bs = bs
self.lstm = nn.LSTM(self.input_dim, self.hidden_dim)
self.linear = nn.Linear(self.hidden_dim, self.output_dim)
def forward(self, *inputs):
print(f'in[0]: {inputs[0].shape}')
out, self.h = self.lstm(inputs[0].view(1, self.bs, self.input_dim))
y_pred = self.linear(out)
return y_pred[-1]
This module works when I feed the raw data to the model, but it failed when I initialize it with Learner module.
It complaints that the shape of data is wrong. Here the full code
import torch
import torch.nn as nn
import pandas as pd
import fastai
from fastai.tabular import *
size = 200
bs = 64
X = torch.randn(size, 5)
y = (X.T[0] * 1.2 + X.T[1] * 4.2 + X.T[2] * 0.56 + X.T[3] * 0.12 + X.T[4] * 7.2 + torch.randn(size)).view(size, 1)
valid_ratio = 0.2
valid_samples = int(valid_ratio * size)
print(f'X.shape: {X.shape}')
print(f'y.shape: {y.shape}')
df = pd.concat([pd.DataFrame(X.data.numpy()), pd.DataFrame(y.data.numpy())], axis=1)
df.columns = ['a', 'b', 'c', 'd', 'e', 'y']
class SimpleLSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, output_dim, bs):
super().__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.output_dim = output_dim
self.bs = bs
self.lstm = nn.LSTM(self.input_dim, self.hidden_dim)
self.linear = nn.Linear(self.hidden_dim, self.output_dim)
def forward(self, *inputs):
print(f'in[0]: {inputs[0].shape}')
out, self.h = self.lstm(inputs[0].view(1, self.bs, self.input_dim))
y_pred = self.linear(out)
return y_pred[-1]
model = SimpleLSTM(input_dim=5, hidden_dim=100, output_dim=1, bs=bs)
model
out = model(X[:bs])
print(f'out shape: {out.shape}')
out = model(X[:bs])
print(f'out shape: {out.shape}')
procs = [Normalize]
valid_idx = range(len(df)-valid_samples, len(df))
dep_var = 'y'
data = TabularDataBunch.from_df('.', df, dep_var, valid_idx=valid_idx, procs=procs, bs=bs)
learner = Learner(data, model)
learner.fit_one_cycle(1)
My observation is FastAI will feed two parameters one with shape (bs, ), another one with shape (bs, input_dim). Why does it pass the (bs, ) parameter?