Hello,
I’m trying to create a simple Multilayer Perceptron using fastai library to classify IMDB reviews.
Here is the code I’m using. However, at the moment I’m stuck with the size of the input output to each linear layer. I’m not sure I really understand why. Grateful if someone could give me some hints.
from fastai.text import *
path = untar_data(URLs.IMDB_SAMPLE)
path.ls()
data = TextClasDataBunch.from_csv(path, 'texts.csv')
x,y = next(iter(data.train_dl))
x.shape, y.shape
# Multilayer perceptron
class MultilayerPerceptron(nn.Module):
def __init__(self):
super().__init__()
self.lin1 = nn.Linear(1503, 250, bias=True)
self.lin2 = nn.Linear(250, 120, bias=True)
self.lin3 = nn.Linear(120, 2, bias=True)
def forward(self, xb):
x = xb.float()
#x = xb.view(250, -1)
x = F.relu(self.lin1(x))
x = F.relu(self.lin2(x))
return self.lin3(x)
mlp_learner = Learner(data=data, model=MultilayerPerceptron(), loss_func=nn.CrossEntropyLoss(),metrics=accuracy)
mlp_learner.fit_one_cycle(5,1e-2)
However, the code return the error: RuntimeError: size mismatch, m1: [64 x 178], m2: [1503 x 250] at /opt/conda/conda-bld/pytorch_1565272271120/work/aten/src/THC/generic/THCTensorMathBlas.cu:273
It seems to me that it has something to do with input and out of hidden layers but not clear why. The application of MLP should be quiet straightforward.
Thank you.