Made the two changes you’ve mentioned but getting the same error:
    all_loss[phase] = torch.cat((all_loss[phase], loss.detach().view(-1)))
TypeError: expected Tensor as element 0 in argument 0, but got numpy.float32

Change/diff:
# all_loss = {key: torch.zeros(1).to(device) for key in phases}
    all_loss = {'train': torch.tensor([0.], device='cuda:0'),
                'valid': torch.tensor([0.], device='cuda:0')}