torch norm 28 29

Solutions on MaxInterview for torch norm 28 29 by the best coders in the world

showing results for - "torch norm 28 29"
Arianna
10 Oct 2020
1Returns the matrix norm or vector norm of a given tensor.
Bay
22 May 2020
1>>> import torch
2>>> a = torch.arange(9, dtype= torch.float) - 4
3>>> b = a.reshape((3, 3))
4>>> torch.norm(a)
5tensor(7.7460)
6>>> torch.norm(b)
7tensor(7.7460)
8>>> torch.norm(a, float('inf'))
9tensor(4.)
10>>> torch.norm(b, float('inf'))
11tensor(4.)
12>>> c = torch.tensor([[ 1, 2, 3],[-1, 1, 4]] , dtype= torch.float)
13>>> torch.norm(c, dim=0)
14tensor([1.4142, 2.2361, 5.0000])
15>>> torch.norm(c, dim=1)
16tensor([3.7417, 4.2426])
17>>> torch.norm(c, p=1, dim=1)
18tensor([6., 6.])
19>>> d = torch.arange(8, dtype= torch.float).reshape(2,2,2)
20>>> torch.norm(d, dim=(1,2))
21tensor([ 3.7417, 11.2250])
22>>> torch.norm(d[0, :, :]), torch.norm(d[1, :, :])
23(tensor(3.7417), tensor(11.2250))