Content deleted Content added
TomRitchford (talk | contribs) →Example: Make comments PEP-8 |
TomRitchford (talk | contribs) →Example: Make more comments PEP-8 |
||
Line 66:
import torch
dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0")
#
a = torch.randn(2, 3, device=device, dtype=dtype)
print(a)
# Output: tensor([[-1.1884, 0.8498, -1.7129],
# [-0.8816, 0.1944, 0.5847]])
b = torch.randn(2, 3, device=device, dtype=dtype)
print(b)
# Output: tensor([[ 0.7178, -0.8453, -1.3403],
# [ 1.3262, 1.1512, -1.7070]])
print(a * b)
# Output: tensor([[-0.8530, -0.7183, 2.58],
# [-1.1692, 0.2238, -0.9981]])
print(a.sum())
# Output: tensor(-2.1540)
Line 91 ⟶ 90:
# Output: tensor(0.5847)
print(a.max())
# 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">
|