RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'other'

username: abhishek_shankar
cooked: <p>Hello ,<br>
When I am fitting my model getting below error .<br>
I am running -&gt;learn.fit_one_cycle(3)</p>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
----&gt; 1 learn.fit_one_cycle(3)</p>
<p>8 frames<br>
/usr/local/lib/python3.6/dist-packages/fastai/train.py in fit_one_cycle(learn, cyc_len, max_lr, moms, div_factor, pct_start, final_div, wd, callbacks, tot_epochs, start_epoch)<br>
20     callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,<br>
21                                        final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))<br>
—&gt; 22     learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks)<br>
23<br>
24 def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None):</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(self, epochs, lr, wd, callbacks)<br>
197         callbacks = [cb(self) for cb in self.callback_fns + listify(defaults.extra_callback_fns)] + listify(callbacks)<br>
198         if defaults.extra_callbacks is not None: callbacks += defaults.extra_callbacks<br>
–&gt; 199         fit(epochs, self, metrics=self.metrics, callbacks=self.callbacks+callbacks)<br>
200<br>
201     def create_opt(self, lr:Floats, wd:Floats=0.)-&gt;None:</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in fit(epochs, learn, callbacks, metrics)<br>
104             if not cb_handler.skip_validate and not learn.data.empty_val:<br>
105                 val_loss = validate(learn.model, learn.data.valid_dl, loss_func=learn.loss_func,<br>
–&gt; 106                                        cb_handler=cb_handler, pbar=pbar)<br>
107             else: val_loss=None<br>
108             if cb_handler.on_epoch_end(val_loss): break</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/basic_train.py in validate(model, dl, loss_func, cb_handler, pbar, average, n_batch)<br>
61             if not is_listy(yb): yb = [yb]<br>
62             nums.append(yb[0].shape[0])<br>
—&gt; 63             if cb_handler and cb_handler.on_batch_end(val_losses[-1]): break<br>
64             if n_batch and (len(nums)&gt;=n_batch): break<br>
65         nums = np.array(nums, dtype=np.float32)</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_batch_end(self, loss)<br>
306         “Handle end of processing one batch with <code>loss</code>.”<br>
307         self.state_dict[‘last_loss’] = loss<br>
–&gt; 308         self(‘batch_end’, call_mets = not self.state_dict[‘train’])<br>
309         if self.state_dict[‘train’]:<br>
310             self.state_dict[‘iteration’] += 1</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/callback.py in <strong>call</strong>(self, cb_name, call_mets, **kwargs)<br>
248         “Call through to all of the <code>CallbakHandler</code> functions.”<br>
249         if call_mets:<br>
–&gt; 250             for met in self.metrics: self._call_and_update(met, cb_name, **kwargs)<br>
251         for cb in self.callbacks: self._call_and_update(cb, cb_name, **kwargs)<br>
252</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/callback.py in _call_and_update(self, cb, cb_name, **kwargs)<br>
239     def <em>call_and_update(self, cb, cb_name, **kwargs)-&gt;None:<br>
240         “Call <code>cb_name</code> on <code>cb</code> and update the inner state.”<br>
–&gt; 241         new = ifnone(getattr(cb, f’on</em>{cb_name}’)(**self.state_dict, **kwargs), dict())<br>
242         for k,v in new.items():<br>
243             if k not in self.state_dict:</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/callback.py in on_batch_end(self, last_output, last_target, **kwargs)<br>
342         if not is_listy(last_target): last_target=[last_target]<br>
343         self.count += last_target[0].size(0)<br>
–&gt; 344         val = self.func(last_output, *last_target)<br>
345         if self.world:<br>
346             val = val.clone()</p>
<p>/usr/local/lib/python3.6/dist-packages/fastai/metrics.py in accuracy(input, targs)<br>
27     input = input.argmax(dim=-1).view(n,-1)<br>
28     targs = targs.view(n,-1)<br>
—&gt; 29     return (input==targs).float().mean()<br>
30<br>
31 def accuracy_thresh(y_pred:Tensor, y_true:Tensor, thresh:float=0.5, sigmoid:bool=True)-&gt;Rank0Tensor:</p>
<p>RuntimeError: Expected object of scalar type Long but got scalar type Float for argument <span class="hashtag">#2</span> ‘other’</p>

username: ptrblck
cooked: <p>Could you check the <code>dtype</code> of <code>input</code>?<br>
It seems it’s a <code>LongTensor</code>, while <code>targs</code> seems to be a <code>FloatTensor</code>.<br>
If that’s the case, just call <code>(input.float()==targs).mean()</code>.</p>