import numpy as np
import torch
1、np.where
a = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
b = np. array( [ 0 , 3 , 2 , 5 , 4 ] )
c = np. where( a > b, a, b)
print ( c)
[ 1 3 3 5 5 ]
2、
shape = ( 640 , 640 , 3 )
print ( shape[ : - 1 ] )
print ( shape[ : - 1 ] + ( 2 , ) )
print ( ( shape[ : - 1 ] , 2 ) )
( 640 , 640 )
( 640 , 640 , 2 )
( ( 640 , 640 ) , 2 )
3、np.tile
shape = ( 10 , 10 , 1 )
grid_x = np. tile( np. arange( shape[ 1 ] ) , ( shape[ 0 ] , 1 ) )
grid_y = np. tile( np. arange( shape[ 0 ] ) , ( shape[ 1 ] , 1 ) ) . transpose( )
print ( "grid_x" , grid_x)
print ( "grid_y" , grid_y)
print ( grid_x. shape)
center_gird = ( 5 , 5 )
grid_mask = ( grid_x - center_gird[ 0 ] ) ** 2 + ( grid_y - center_gird[ 1 ] ) ** 2
print ( grid_mask)
grid_x [ [ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ]
[ 0 1 2 3 4 5 6 7 8 9 ] ]
grid_y [ [ 0 0 0 0 0 0 0 0 0 0 ]
[ 1 1 1 1 1 1 1 1 1 1 ]
[ 2 2 2 2 2 2 2 2 2 2 ]
[ 3 3 3 3 3 3 3 3 3 3 ]
[ 4 4 4 4 4 4 4 4 4 4 ]
[ 5 5 5 5 5 5 5 5 5 5 ]
[ 6 6 6 6 6 6 6 6 6 6 ]
[ 7 7 7 7 7 7 7 7 7 7 ]
[ 8 8 8 8 8 8 8 8 8 8 ]
[ 9 9 9 9 9 9 9 9 9 9 ] ]
( 10 , 10 )
[ [ 50 41 34 29 26 25 26 29 34 41 ]
[ 41 32 25 20 17 16 17 20 25 32 ]
[ 34 25 18 13 10 9 10 13 18 25 ]
[ 29 20 13 8 5 4 5 8 13 20 ]
[ 26 17 10 5 2 1 2 5 10 17 ]
[ 25 16 9 4 1 0 1 4 9 16 ]
[ 26 17 10 5 2 1 2 5 10 17 ]
[ 29 20 13 8 5 4 5 8 13 20 ]
[ 34 25 18 13 10 9 10 13 18 25 ]
[ 41 32 25 20 17 16 17 20 25 32 ] ]
4、
print ( np. stack( ( grid_y, grid_x) ) . shape)
print ( np. stack( ( grid_y, grid_x) , - 1 ) . shape)
( 2 , 10 , 10 )
( 10 , 10 , 2 )
5、[None] 、[:,None]
x = torch. randn( ( 39 , 2 ) )
x = torch. zeros_like( x) [ None ]
print ( x. shape)
y = torch. randn( ( 5 , 2 ) )
y = y[ : , None ]
print ( y. shape)
print ( ( x+ y) . shape)
torch. Size( [ 1 , 39 , 2 ] )
torch. Size( [ 5 , 1 , 2 ] )
torch. Size( [ 5 , 39 , 2 ] )
6、sum(0)、max(0)
x = torch. range ( 0 , 11 , 1 ) . resize( 3 , 4 )
print ( x)
print ( x. sum ( 0 ) )
print ( x. sum ( 1 ) )
print ( x. max ( 0 ) )
print ( x. max ( 1 ) )
tensor( [ [ 0. , 1. , 2. , 3. ] ,
[ 4. , 5. , 6. , 7. ] ,
[ 8. , 9. , 10. , 11. ] ] )
tensor( [ 12. , 15. , 18. , 21. ] )
tensor( [ 6. , 22. , 38. ] )
torch. return_types. max (
values= tensor( [ 8. , 9. , 10. , 11. ] ) ,
indices= tensor( [ 2 , 2 , 2 , 2 ] ) )
torch. return_types. max (
values= tensor( [ 3. , 7. , 11. ] ) ,
indices= tensor( [ 3 , 3 , 3 ] ) )
7、
import torch
import numpy as np
from torch. nn. functional import one_hot
from torch import nn
predicts = np. array( ( [
[ - 1.0606 , 1.5613 , 1.2007 , - 0.2481 ] ,
[ - 1.9652 , - 0.4367 , - 0.0645 , - 0.5104 ] ,
[ 0.1011 , - 0.5904 , 0.0243 , 0.1002 ]
] ) )
print ( "pred_shape:" , predicts. shape)
label= torch. tensor( [ 0 , 2 , 1 ] )
label = one_hot( label, num_classes= 4 )
print ( "label:" , label)
print ( "softmax:" , torch. softmax( torch. tensor( predicts) , 1 ) )
loss = - 1 / predicts. shape[ 0 ] * torch. sum ( label* torch. log( torch. softmax( torch. tensor( predicts) , 1 ) ) )
print ( "loss:" , loss)
crit = nn. CrossEntropyLoss( )
loss = crit( torch. tensor( predicts) , torch. tensor( [ 0 , 2 , 1 ] ) )
print ( "CrossEntropyLoss:" , loss)
pred_shape: ( 3 , 4 )
label: tensor( [ [ 1 , 0 , 0 , 0 ] ,
[ 0 , 0 , 1 , 0 ] ,
[ 0 , 1 , 0 , 0 ] ] )
softmax: tensor( [ [ 0.0376 , 0.5172 , 0.3606 , 0.0847 ] ,
[ 0.0603 , 0.2780 , 0.4034 , 0.2583 ] ,
[ 0.2919 , 0.1462 , 0.2703 , 0.2916 ] ] , dtype= torch. float64)
loss: tensor( 2.0373 , dtype= torch. float64)
CrossEntropyLoss: tensor( 2.0373 , dtype= torch. float64)