If your loss is not a scalar value, then you should certainly use either loss.mean() or loss.sum() to convert it to a scalar before calling the backward. Otherwise, it will cause an error as in the following example:
>>> loss = criterion(m(input), target)
>>> loss
tensor([1.2377, 0.6949, 0.6477], grad_fn=<BinaryCrossEntropyBackward>)
>>> loss.backward()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vahid/anaconda3/envs/py37/lib/python3.7/site-packages/torch/tensor.py", line 102, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph)
  File "/home/vahid/anaconda3/envs/py37/lib/python3.7/site-packages/torch/autograd/__init__.py", line 84, in backward
    grad_tensors = _make_grads(tensors, grad_tensors)
  File "/home/vahid/anaconda3/envs/py37/lib/python3.7/site-packages/torch/autograd/__init__.py", line 28, in _make_grads
    raise RuntimeError("grad can be implicitly created only for scalar outputs")
RuntimeError: grad can be implicitly created only for scalar outputs