def try_view(tensor, shape):
    """Fall back to reshape (more expensive) if viewing does not work."""
    try:
        return tensor.view(shape)
    except RuntimeError:
        return tensor.reshape(shape)