I’m trying to run a regression on a dataframe with categorical and continuous columns using the get_tabular_learner()
helper. My code is as follows:
df = pd.read_csv(DATA_PATH)
dep_var = 'actual'
train_df, valid_df = df[:10000].copy(), df[10000:].copy()
data = tabular_data_from_df('.', train_df, valid_df, dep_var, tfms=[Categorify], cat_names=CAT_NAMES, cont_names=CONT_NAMES)
learn = get_tabular_learner(data, layers=[200,1])
learn.loss_fn = F.mse_loss
learn.fit(1)
Running this gives me an error with the loss function:
File "/home/harvitronix/.local/lib/python3.6/site-packages/fastai/basic_train.py", line 25, in loss_batch
loss = loss_fn(out, *yb)
File "/home/harvitronix/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1808, in mse_loss
return _pointwise_loss(lambda a, b: (a - b) ** 2, torch._C._nn.mse_loss, input, target, reduction)
File "/home/harvitronix/.local/lib/python3.6/site-packages/torch/nn/functional.py", line 1770, in _pointwise_loss
return lambd_optimized(input, target, _Reduction.get_enum(reduction))
RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #2 'target'
I posted this as a GitHub issue as well, and jandremarais provided a workaround:
def myloss(input, target): return F.mse_loss(input, target.float())
learn.loss_fn = myloss
This works, in that my model now trains. But is there a way to use loss functions directly, or is there generally a more “right” way to do this?
Thank you!