Symbol *, torch.max and torch.sum, item() methods

*The role can be referenced

Torch.max can refer to

a.size()
# Out[134]: torch.Size([6, 4, 3])
torch.max(a, 0)[1].size()
# Out[135]: torch.Size([4, 3])
torch.max(a, 1)[1].size()
# Out[136]: torch.Size([6, 3])
torch.max(a, 2)[1].size()
# Out[137]: torch.Size([6, 4])

Specific how to compare can see below

b

tensor([[[  0.,   1.,   2.,   3.],
         [  4.,   5.,   6.,   7.],
         [  8.,   9.,  10.,  11.]],
        [[ 12.,  13.,  14.,  15.],
         [ 16.,  17.,  18.,  19.],
         [ 20.,  21.,  22.,  23.]]])

torch.max(b,0)[0]
 
tensor([[ 12.,  13.,  14.,  15.],
        [ 16.,  17.,  18.,  19.],
        [ 20.,  21.,  22.,  23.]])

torch.max(b,1)[0]

tensor([[  8.,   9.,  10.,  11.],
        [ 20.,  21.,  22.,  23.]])

torch.max(b,2)[0]

tensor([[  3.,   7.,  11.],
        [ 15.,  19.,  23.]])

The corresponding subscript can be obtained

b

tensor([[[  0.,   1.,   2.,   3.],
         [  4.,   5.,   6.,   7.],
         [  8.,   9.,  10.,  11.]],
        [[ 12.,  13.,  14.,  15.],
         [ 16.,  17.,  18.,  19.],
         [ 20.,  21.,  22.,  23.]]])

torch.max(b,0)[1]

tensor([[ 1,  1,  1,  1],
        [ 1,  1,  1,  1],
        [ 1,  1,  1,  1]])

torch.max(b,1)[1]

tensor([[ 2,  2,  2,  2],
        [ 2,  2,  2,  2]])

torch.max(b,2)[1]

tensor([[ 3,  3,  3],
        [ 3,  3,  3]])

 torch.sum:

torch.sum(input) → Tensor
torch.sum(input, dim, out=None) → Tensor
Parameters:

         Input (Tensor) – input tensor
         Dim (int) – reduced dimensions
         Out (Tensor, optional) – result tensor

The output of the function is a tensor

match
out:
tensor([[[ 0,  0,  2,  0],
         [ 0,  0,  0,  0],
         [ 0,  0,  0,  0]],
        [[ 0,  0,  0,  0],
         [ 0,  0,  0,  0],
         [ 0,  0,  0,  0]]], dtype=torch.uint8)

torch.sum(match)
Out: 
tensor(2)

torch.sum(match,0)
Out: 
tensor([[ 0,  0,  2,  0],
        [ 0,  0,  0,  0],
        [ 0,  0,  0,  0]])
torch.sum(match,1)
Out: 
tensor([[ 0,  0,  2,  0],
        [ 0,  0,  0,  0]])
torch.sum(match,2)
Out: 
tensor([[ 2,  0,  0],
        [ 0,  0,  0]])

One more thing to add is the use of the item method: if the tensor has only one element, then calling the item method will convert the tensor into scalar scalars; if the tensor is not a single element, then a ValueError will be raised, as below

b.item()
Traceback (most recent call last):
    b.item()
ValueError: only one element tensors can be converted to Python scalars

torch.sum(b)
Out: tensor(276.)
torch.sum(b).item()
Out: 276.0

So what is the item method in python? See

 

Intelligent Recommendation

Use the item symbol and number to organize text 1

Use the item symbol and number to organize text 1 Knowledge expansion Item Number: It is a point or other symbol before the text, which emphasizes the role. Reasonable use of the project number, you c...

[Pytorch function] Torch.sum

Torch.Sum () for a certain dimension of the input Tensor data, a total of two usage example: Output results: If you add Keepdim = True, you will keep the dimension of DIM is not SQUEEZE. Output result...

[Pytorch Basics] torch.sum ()

torch.sum()For a certain dimension of the input Tensor data, a total of two usage 1.torch.sum(input, dtype=None) 2.torch.sum(input, list: dim, bool: keepdim=False, dtype=None) → Tensor input: Ent...

Torch.Sum, DIM parameter record

Xiaobai learned Pytorch, always confused the parameters of Dim, recorded Still small white, wrong place troubles Suppose already has a Tensor X, the dimension is [2, 4, 3], 2 is BATCH, 4 is the number...

Torch.Sum dimension parameter analysis

Personal understanding: For two-dimensional arrays, DIM = 0 is fixed line, add columns; DIM = 1, fixed column, add line. It is worth noting that DIM = -1 and DIM = 2 are the same. Personal estimation:...

More Recommendation

Torch.cat and torch.sum understanding

torch.cat((A,B),dim) or torch.cat([A,B],dim) The stitching tensor A and B, DIM represents the stitching direction, dim = 0 means stitching according to the line (that is, the size of the two quantitie...

(1) Torch.sum use summary

Welcome to personal network logs Knowledgeable space🌹🌹 Articles directory 1. Do not specify DIM 2. Specify DIM 2.1dim is the constant 2.2 DIM is Tuple Reference information Found torch.sum dimParame...

torch.max () usage

———————————————— Copyright: Original article is CSDN blogger "swinging jelly", and follow CC 4...

torch.max() tutorial

table of Contents 1. torch.max(input, dim) function 2. Calculation of accuracy In the classification problem, usually need to usemax()Function pairsoftmaxThe output value of the function is operated t...

torch.max() and torch.min()

Official website:https://pytorch.org/docs/stable/torch.html#torch.max  torch.max() and torch.min() are functions to compare the size of tensor. Both have the same usage, so I have summarized one....

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top