Edit: @sgugger pointed out that I needed to call MSELossFlat (I was missing the parens). You can skip to post #18 for the working Dataset.
I updated fastai (I updated the code every couple days, but not every single day) and now neither @wyquek’s code nor yours will run for me. With the same architecture as above, I now get an error about RuntimeError: bool value of Tensor with more than one value is ambiguous
:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-60-ec98ea40e834> in <module>()
5 # loss_func=data.loss_func,
6 callback_fns=ShowGraph)
----> 7 learn2.lr_find(start_lr=1e-7, end_lr=100)
8 learn2.recorder.plot()
/app/fastai/fastai/train.py in lr_find(learn, start_lr, end_lr, num_it, **kwargs)
25 cb = LRFinder(learn, start_lr, end_lr, num_it)
26 a = int(np.ceil(num_it/len(learn.data.train_dl)))
---> 27 learn.fit(a, start_lr, callbacks=[cb], **kwargs)
28
29 def to_fp16(learn:Learner, loss_scale:float=512., flat_master:bool=False)->Learner:
/app/fastai/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)
135 callbacks = [cb(self) for cb in self.callback_fns] + listify(callbacks)
136 fit(epochs, self.model, self.loss_func, opt=self.opt, data=self.data, metrics=self.metrics,
--> 137 callbacks=self.callbacks+callbacks)
138
139 def create_opt(self, lr:Floats, wd:Floats=0.)->None:
/app/fastai/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
87 except Exception as e:
88 exception = e
---> 89 raise e
90 finally: cb_handler.on_train_end(exception)
91
/app/fastai/fastai/basic_train.py in fit(epochs, model, loss_func, opt, data, callbacks, metrics)
77 for xb,yb in progress_bar(data.train_dl, parent=pbar):
78 xb, yb = cb_handler.on_batch_begin(xb, yb)
---> 79 loss = loss_batch(model, xb, yb, loss_func, opt, cb_handler)[0]
80 if cb_handler.on_batch_end(loss): break
81
/app/fastai/fastai/basic_train.py in loss_batch(model, xb, yb, loss_func, opt, cb_handler)
20
21 if not loss_func: return to_detach(out), yb[0].detach()
---> 22 loss = loss_func(out, *yb)
23
24 if opt is not None:
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in __init__(self, size_average, reduce, reduction)
419 """
420 def __init__(self, size_average=None, reduce=None, reduction='elementwise_mean'):
--> 421 super(MSELoss, self).__init__(size_average, reduce, reduction)
422
423 def forward(self, input, target):
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/loss.py in __init__(self, size_average, reduce, reduction)
13 super(_Loss, self).__init__()
14 if size_average is not None or reduce is not None:
---> 15 self.reduction = _Reduction.legacy_get_string(size_average, reduce)
16 else:
17 self.reduction = reduction
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in legacy_get_string(size_average, reduce, emit_warning)
45 reduce = True
46
---> 47 if size_average and reduce:
48 ret = 'elementwise_mean'
49 elif reduce:
RuntimeError: bool value of Tensor with more than one value is ambiguous
I haven’t changed my Dataset (except to experiment with MSELossFlat
) from the original, which is:
class ImageScalarDataset(ImageDataset):
def __init__(self, df:DataFrame, path_column:str='file_path', dependent_variable:str=None):
super().__init__(df[path_column], np.array(df[dependent_variable], dtype=np.float32))
self.loss_func = layers.MSELossFlat
self.classes = [0]
def __len__(self)->int:
return len(self.y)
def __getitem__(self, i):
# return x, y | where x is an image, and y is the scalar
return open_image(self.x[i]), self.y[i]
On the plus side, I get the same error whether I use @jeremy’s or @wyquek’s loss function, so I suspect there was some other internal change that my Dataset is not handling correctly. Perhaps something about how I am manually setting self.classes = [0]
?