学习记录1

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、

#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

#3、
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()  # grid_x, grid_y用来遍历图上的每一个点
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]

#5、

x = torch.randn((39,2))
# print(torch.zeros_like(x).shape)
x = torch.zeros_like(x)[None]
print(x.shape)

y = torch.randn((5,2))
# print(y.shape)
y = y[:,None]
print(y.shape)

print((x+y).shape)
# print((torch.zeros_like(x)[None] + y[:,None]).shape)
torch.Size([1, 39, 2])
torch.Size([5, 1, 2])
torch.Size([5, 39, 2])

6、sum(0)、max(0)

#6、sum(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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值