Getting Runtime error for cross entropy. What should be changed and why it is coming

Ankur_Bassi: <p>I am testing on movie lens data set</p>
<pre><code class="lang-auto">dls=CollabDataLoaders.from_df(ratings,item_name='Title',bs=64)

class deeplearn(Module):
    def __init__(self,user_size,movie_size,n_act=50,y_range=(0,5.5)):
        self.user_factors=create_params(user_size)
        self.movie_factors=create_params(movie_size)
        self.layers=nn.Sequential(
                    nn.Linear(user_size[1]+movie_size[1],n_act),
                    nn.ReLU(),
                    nn.Linear(n_act,5)
        )
        self.y_range=y_range
        
    def forward(self,x):
        embs=self.user_factors[x[:,0]],self.movie_factors[x[:,1]]
        x1=self.layers(torch.cat(embs,dim=1))
        return sigmoid_range(x1,*self.y_range)

embs=get_emb_sz(dls)        
n_users=6041
n_movies=3707

model = deeplearn(*embs)
learn3 = Learner(dls, model, loss_func=CrossEntropyLossFlat)
learn3.fit_one_cycle(5, 5e-3, wd=0.01)
</code></pre>
<p>After executing getting<br>
RuntimeError: Boolean value of Tensor with more than one value is ambiguous</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-145-b697f71a53c1&gt; in &lt;module&gt;
      3 model = deeplearn(*embs)
      4 learn3 = Learner(dls, model, loss_func=CrossEntropyLossFlat)
----&gt; 5 learn3.fit_one_cycle(5, 5e-3, wd=0.01)

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    110     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    111               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--&gt; 112     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    113 
    114 # Cell

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    209             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    210             self.n_epoch = n_epoch
--&gt; 211             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    212 
    213     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--&gt; 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _do_fit(self)
    200         for epoch in range(self.n_epoch):
    201             self.epoch=epoch
--&gt; 202             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    203 
    204     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--&gt; 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _do_epoch(self)
    194 
    195     def _do_epoch(self):
--&gt; 196         self._do_epoch_train()
    197         self._do_epoch_validate()
    198 

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _do_epoch_train(self)
    186     def _do_epoch_train(self):
    187         self.dl = self.dls.train
--&gt; 188         self._with_events(self.all_batches, 'train', CancelTrainException)
    189 
    190     def _do_epoch_validate(self, ds_idx=1, dl=None):

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--&gt; 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in all_batches(self)
    164     def all_batches(self):
    165         self.n_iter = len(self.dl)
--&gt; 166         for o in enumerate(self.dl): self.one_batch(*o)
    167 
    168     def _do_one_batch(self):

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in one_batch(self, i, b)
    182         self.iter = i
    183         self._split(b)
--&gt; 184         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    185 
    186     def _do_epoch_train(self):

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--&gt; 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/learner.py in _do_one_batch(self)
    170         self('after_pred')
    171         if len(self.yb):
--&gt; 172             self.loss_grad = self.loss_func(self.pred, *self.yb)
    173             self.loss = self.loss_grad.clone()
    174         self('after_loss')

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/losses.py in __init__(self, axis, *args, **kwargs)
     41     y_int = True
     42     @use_kwargs_dict(keep=True, weight=None, ignore_index=-100, reduction='mean')
---&gt; 43     def __init__(self, *args, axis=-1, **kwargs): super().__init__(nn.CrossEntropyLoss, *args, axis=axis, **kwargs)
     44     def decodes(self, x):    return x.argmax(dim=self.axis)
     45     def activation(self, x): return F.softmax(x, dim=self.axis)

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/fastai/losses.py in __init__(self, loss_cls, axis, flatten, floatify, is_2d, *args, **kwargs)
     16     def __init__(self, loss_cls, *args, axis=-1, flatten=True, floatify=False, is_2d=True, **kwargs):
     17         store_attr("axis,flatten,floatify,is_2d")
---&gt; 18         self.func = loss_cls(*args,**kwargs)
     19         functools.update_wrapper(self, self.func)
     20 

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/torch/nn/modules/loss.py in __init__(self, weight, size_average, ignore_index, reduce, reduction)
    955     def __init__(self, weight: Optional[Tensor] = None, size_average=None, ignore_index: int = -100,
    956                  reduce=None, reduction: str = 'mean') -&gt; None:
--&gt; 957         super(CrossEntropyLoss, self).__init__(weight, size_average, reduce, reduction)
    958         self.ignore_index = ignore_index
    959 

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/torch/nn/modules/loss.py in __init__(self, weight, size_average, reduce, reduction)
     23 class _WeightedLoss(_Loss):
     24     def __init__(self, weight: Optional[Tensor] = None, size_average=None, reduce=None, reduction: str = 'mean') -&gt; None:
---&gt; 25         super(_WeightedLoss, self).__init__(size_average, reduce, reduction)
     26         self.register_buffer('weight', weight)
     27 

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/torch/nn/modules/loss.py in __init__(self, size_average, reduce, reduction)
     16         super(_Loss, self).__init__()
     17         if size_average is not None or reduce is not None:
---&gt; 18             self.reduction = _Reduction.legacy_get_string(size_average, reduce)
     19         else:
     20             self.reduction = reduction

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/torch/nn/_reduction.py in legacy_get_string(size_average, reduce, emit_warning)
     35         reduce = True
     36 
---&gt; 37     if size_average and reduce:
     38         ret = 'mean'
     39     elif reduce:

RuntimeError: Boolean value of Tensor with more than one value is ambiguous

</code></pre>

ptrblck: <p>Based on the error message it seems you are passing multiple values to <code>size_average</code> or <code>reduction</code> in the creation of the criterion. Could you check its initialization and make sure these arguments get valid values?</p>