PyTorch: Difference between revisions

Content deleted Content added
Example: Make comments PEP-8
Example: Make more comments PEP-8
Line 66:
import torch
dtype = torch.float
device = torch.device("cpu") # This# executesExecute all calculations on the CPU
# device = torch.device("cuda:0") # This# executesExecutes all calculations on the GPU
 
# Creation ofCreate a tensor and fillingfill of a tensorit with random numbers
a = torch.randn(2, 3, device=device, dtype=dtype)
print(a) # Output of tensor A
# Output: tensor([[-1.1884, 0.8498, -1.7129],
# [-0.8816, 0.1944, 0.5847]])
 
# Creation of a tensor and filling of a tensor with random numbers
b = torch.randn(2, 3, device=device, dtype=dtype)
print(b) # Output of tensor B
# Output: tensor([[ 0.7178, -0.8453, -1.3403],
# [ 1.3262, 1.1512, -1.7070]])
 
print(a * b)
print(a*b) # Output of a multiplication of the two tensors
# Output: tensor([[-0.8530, -0.7183, 2.58],
# [-1.1692, 0.2238, -0.9981]])
 
print(a.sum()) # Output of the sum of all elements in tensor A
# Output: tensor(-2.1540)
 
Line 91 ⟶ 90:
# Output: tensor(0.5847)
 
print(a.max()) # Output of the maximum value in tensor A
# Output: tensor(0.8498)
</syntaxhighlight>The following code-block shows an example of the higher level functionality provided <code>nn</code> module. A neural network with linear layers is defined in the example.<syntaxhighlight lang="python3" line="1">