Summary of basic knowledge recently used in python (4)

1. The usage of return in python : The return statement is to return the result to the place of call, and return the control of the program together. The program runs until the first return encountered (exit the def block) and will not run again The second return.

Code measurement to figure out:

def test_return(x):
    for i in range(3):
        print(1)
        if x > 0:
            return x

test_return(-1)

1
1
1


def test_return(x):
    for i in range(3):
        print(1)
        if x > 0:
            return x

test_return(1)

1

It can be seen that the def block is directly exited after the return, and it must be replaced to achieve the cycle.

2. The meaning of nn.ReLU(inplace=True) in pytorch

What do nn.LeakyReLU(inplace=True)the inplacefields in the example mean? What is the use?

inplace=TrueSitu operation means, e.g. x=x+5, a xis a place operation y=x+5, x=ycomplete with the x=x+5same function, but need not place operation, above LeakyReLUthe inplace=Truemeaning is the same, for Conv2dtensor pass this down the upper network Modify directly, the advantage is that you can save the calculation memory, without storing more variables y.

3. The np.rot90() matrix is ​​rotated by 90° or a multiple of 90°. Rotate counterclockwise by default.

Examples
--------
>>> m = np.array([[1,2],[3,4]], int)
>>> m
array([[1, 2],
       [3, 4]])
>>> np.rot90(m)
array([[2, 4],
       [1, 3]])
>>> np.rot90(m, 2)
array([[4, 3],
       [2, 1]])

The above parameter 2 represents 90 rotations twice, if you want to rotate 90 clockwise twice, you can use -2 instead.

When the above is upgraded to 3d

>>> m = np.arange(8).reshape((2,2,2))
>>> np.rot90(m, 1, (1,2))
array([[[1, 3],
        [0, 2]],
       [[5, 7],
        [4, 6]]])

The previous 1 parameter has already been introduced, here is the main introduction to the next (1,2) which represents the second dimension and the third dimension to rotate. The sequence of parameters can be used

rot90(m, k=1, axes=(1,0)) is the reverse of rot90(m, k=1, axes=(0,1))
rot90(m, k=1, axes=(1,0)) is equivalent to rot90(m, k=-1, axes=(0,1))

(1,0) and (0,1) are equal when the two k are opposite numbers.

Examples of 3d rotation are as follows:

import numpy as np
m = np.arange(8).reshape((2,2,2))
print('before',m[:,:,0])
p=np.rot90(m, 1, (0,1))
print('after',p[:,:,0])

before [[0 2]
 [4 6]]
after [[2 6]
 [0 4]]

Also pay special attention

flip : Reverse the order of elements in an array along the given axis.反向沿给定轴反转数组中元素的顺序(跟前面的[:,::-1,:]翻转第二维度是等效的)
fliplr : Flip an array horizontally.水平翻转阵列
flipud : Flip an array vertically.垂直翻转数组
m1=np.copy(m)
mp=m[::-1,:,:]
print('compare',mp[:,:,0])
m1p=np.flip(m1,0)
print('compare1',m1p[:,:,0])
compare [[4 6]
 [0 2]]
compare1 [[4 6]
 [0 2]]

4. The set()  function creates an unordered set of non-repeating elements, which can be used for relationship testing, delete duplicate data, and calculate intersection, difference, union, etc.

example:

import random

h = set()
while (len(h) < 10):
    h.add(random.randint(10, 100))

print(h)

{99, 36, 37, 70, 72, 73, 44, 28, 13, 60}

5.isinstance() determines whether an object belongs to a certain class

如assert isinstance(output_size, (int, tuple))

Determine whether output_size is int or tuple. It is not an assert error.

6. The difference between torch.mul() and torch.mm(). It torch.mul(a, b)is the multiplication of the corresponding bits of the matrices a and b . The dimensions of a and b must be equal, andtorch.mm(a, b) the matrices a and b are multiplied. It is required to have the same connection dimensions.

import torch

a = torch.rand(3, 2)
b = torch.rand(3, 2)
c = torch.rand(2, 3)

print(torch.mul(a, b))  # 返回 3*2 的tensor,要求a,b各维度大小相同
print(torch.mm(a, c))   # 返回 3*3 的tensor,要求a,c的衔接维度相同(a的第二维度和c的第一维度)

 

Guess you like

Origin blog.csdn.net/qq_36401512/article/details/88714627