Since Chainer announced the transition to PyTorch, I think that the flow from TensorFlow to PyTorch will be strengthened in Japan as well, so I will do the tutorial.
See Official.
Try it as per Official.
The writing style is quite Numpy-like.
$ x = torch.empty(5, 3)
$ print(x)
tensor([[2.4835e+27, 2.5428e+30, 1.0877e-19],
[1.5163e+23, 2.2012e+12, 3.7899e+22],
[5.2480e+05, 1.0175e+31, 9.7056e+24],
[1.6283e+32, 3.7913e+22, 3.9653e+28],
[1.0876e-19, 6.2027e+26, 2.3685e+21]])
Suffixing a method with _ (underscore) is a destructive process.
$ x = torch.tensor([5.5, 3])
$ y = torch.tensor([2.5, 5])
$ y.add_(x)
$ print(y)
tensor([8., 8.])
Very easy to exchange tensors between CPU and GPU.
if torch.cuda.is_available():
device = torch.device("cuda")
y = torch.ones_like(x, device=device)
x = x.to(device)
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
Recommended Posts