Note——torch.size() & umr_maximum() array.max() & itertools.product()

torch.size

在这里插入图片描述

Problem

TypeError: ‘torch.Size’ object is not callable

Reason Analysis

torch.Size函数不可调用
因为torch只可以.size()

shape

Solution

将y.shape()替换为y.size()

y.shape

y+=torch.normal(0,0.01,y.size())

2

return umr_maximum(a, axis, None, out, keepdims, initial, where)

Problem

ValueError: zero-size array to reduction operation maximum which has no identity

Reason Analysis

数组“array”的“size”为0,所以无法进行计算;

umr_maximum()函数
array.max()函数

Solution

出错:
array.max()
具体原因:
“array”的size为0了,于是元素的个数为0,因而就不存在最大值

在调试时,可以先对数据的规范性进行验证
验证数组array的size是否为0

assert array.size != 0

itertools.product()

Formula

itertools.product(*iterables, repeat=1)

product 用于求多个可迭代对象的笛卡尔积(Cartesian Product)
它跟嵌套的 for 循环等价.

即:
product(A, B)

((x,y) for x in A for y in B)
的效果是一样的

本质essence:
先合成一个元组再组成list

Param

  • iterables 是可迭代对象
  • repeat指定 iterables 重复几次
    即:
    product(A,repeat=3)等价于product(A,A,A)

For example

import itertools
A = [1, 2, 3]
B = ['A', 'B', 'C']
for i in itertools.product(A,B):
    print(i)

在这里插入图片描述

直接使用

分别生成元组,然后合成一个list

import itertools
a = itertools.product(['A','B','C'], ['D','E'])
b = list(a)   
#按照顺序生成笛卡尔积,repeat默认为1
print(a,'\n','\n')
print(b)

set param

repeat=3

a = list(itertools.product(['A','B','C'], ['D','E'], repeat=3))
print(a) 

此list长度为216

在不设置 repeat 参数的时候,默认是1,生成的list长度时6 —— permutation and combination
3*2=6种

当设置 repeat=3 时,也就是说将 repeat=1(默认)的结果再重复2次后(也就是最后一共有3套一样的第一层结果)
从第一个结果(6种结果)取出一个元素的可能有6种
同理,从第二第三个重复结果中取出一个元素的可能各有6种,
So, 666=216种。

others

  1. 如果要从列表中随机取出几个不重复的元素的话
    (原来的列表本身元素不重复),可用 random.sample 方法。
import random
random.seed(1)   
#设置随机数种子,可用来检测相同的随机数得到的结果是否一致

n = 2
aa = random.sample(a, n)   
#随机从列表中取出n个元素
print(aa)
  1. 生成随机的坐标,另一种生成随机坐标的方法
random_list = list(itertools.product(range(1,4), range(1,2)))
# itertools.product([1,2,3],[1])
print(random_list)

n = 2
aa = random.sample(random_list, n)   
#随机取出列表中的n个元素
print(aa)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
YOLO系列是基于深度学习的端到端实时目标检测方法。 PyTorch版的YOLOv5轻量而性能高,更加灵活和易用,当前非常流行。 本课程将手把手地教大家使用labelImg标注和使用YOLOv5训练自己的数据集。课程实战分为两个项目:单目标检测(足球目标检测)和多目标检测(足球和梅西同时检测)。 本课程的YOLOv5使用ultralytics/yolov5,在Ubuntu系统上做项目演示。包括:安装YOLOv5、标注自己的数据集、准备自己的数据集、修改配置文件、训练自己的数据集、测试训练出的网络模型和性能统计。 希望学习在Windows系统上演示的学员,请前往《YOLOv5(PyTorch)实战:训练自己的数据集(Windows)》课程链接:https://edu.csdn.net/course/detail/30923本人推出了有关YOLOv5目标检测的系列课程。请持续关注该系列的其它视频课程,包括:《YOLOv5(PyTorch)目标检测实战:训练自己的数据集》Ubuntu系统 https://edu.csdn.net/course/detail/30793Windows系统 https://edu.csdn.net/course/detail/30923《YOLOv5(PyTorch)目标检测:原理与源码解析》课程链接:https://edu.csdn.net/course/detail/31428《YOLOv5目标检测实战:Flask Web部署》课程链接:https://edu.csdn.net/course/detail/31087《YOLOv5(PyTorch)目标检测实战:TensorRT加速部署》课程链接:https://edu.csdn.net/course/detail/32303《YOLOv5目标检测实战:Jetson Nano部署》课程链接:https://edu.csdn.net/course/detail/32451《YOLOv5+DeepSORT多目标跟踪与计数精讲》课程链接:https://edu.csdn.net/course/detail/32669《YOLOv5实战口罩佩戴检测》课程链接:https://edu.csdn.net/course/detail/32744《YOLOv5实战中国交通标志识别》课程链接:https://edu.csdn.net/course/detail/35209 《YOLOv5实战垃圾分类目标检测》课程链接:https://edu.csdn.net/course/detail/35284  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cmy_CTO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值