Type error when using get_tabular_learner for regression

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

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!


The data processing shouldn’t change your dependent variable, so it sounds like your depvar Series is not float32? So I’d guess you just need to change your input data to make it the correct type. Then it should just be a case of adding loss_fn = F.mse_loss as a param to your Learner. (NB: you’ll need to git pull with a dev version of fastai, since I just added support for this).

Thanks, Jeremy. You’re right… my depvar was float 64. :roll_eyes:

Casting it to float32 fixes it for me. Cheers.

I’ve just pushed a change which will do this casting for you.


So changing the loss to MSE hints the learner that this is a regression problem?

Hi, i tried the same using Diabetes dataset, and tried to predict the Glucose level (i.e setting the dependent variable as Glucose) but the model isn’t learning anything.
Why is this happening ?

As it is seen, the times spent to learn is 0!