cuda() and to('cuda') are going to do the same thing, but the later is more flexible. As you can see in your example code, you can specify a device that might be ‘cpu’ if cuda is unavailable.
If you attempt to call cuda() on a system that doesn’t have a GPU, you’ll get:
AssertionError: Torch not compiled with CUDA enabled.
With the explicit call, you can also use multiple cuda devices – e.g. to('cuda:0') is different from to('cuda:1'). The simpler cuda() call will just use the default cuda device.