美文网首页
Numpy广播

Numpy广播

作者: 哈哈哈哈_ad14 | 来源:发表于2018-07-28 09:55 被阅读0次
  • cs231 numpy介绍
    这个定义比较准确,可以先看看这个,再看后面的例子。
    Broadcasting two arrays together follows these rules:
  1. If the arrays do not have the same rank, prepend the shape of the lower rank array with 1s until both shapes have the same length.
  2. The two arrays are said to be compatible in a dimension if they have the same size in the dimension, or if one of the arrays has size 1 in that dimension.
  3. The arrays can be broadcast together if they are compatible in all dimensions.
  4. After broadcasting, each array behaves as if it had shape equal to the elementwise maximum of shapes of the two input arrays.
  5. In any dimension where one array had size 1 and the other array had size greater than 1, the first array behaves as if it were copied along that dimension
  • numpy 数组广播:例子比较多
  • Numpy中的广播(Broadcasting):这个例子比较清晰。
    a.shape得到的是(3,) b是一个浮点数,如果转换成array,则b.shape是一个(),a的1轴对齐,补齐为1,a.shape(3,1),b对齐,则对齐也为(3,1),然后按照一一对应的方式计算
    但是这一段说的不太对,根据后面的介绍,正确流程应该是这样:
  1. b的维度小于a, 因此首先增加维度对齐,b.shape变成(1,),
  2. 然后b再在维度为1上tail变成(3,)
  3. 两个shape相同的array最后相加.最后的shape也是(3,0)
    ps:如果按照原文说的,最后的shape将是(3,1),这个可以自己实验一下
  • 总结:
    1.对齐
    先看数组维度,维度不相同在短维度的数组前面连续增加1维,直到两个数组维度相同,转向第2步
    2.检查
    两个维度数目相同的数组,如果二者各个维度的长度相同或者一个长度为1,那么可以进行第3步执行tail(拓展).如果不满足,则不可广播
    3.拓展
    对于检查无误的a,b两数组,输出的数组的shape是a.shape*b.shape(按位乘).将两数组沿着维度长度为1的轴进行拓展(copy),拓展成与输出一致的shape,然后进行运算.

相关文章

  • Numpy广播

    cs231 numpy介绍这个定义比较准确,可以先看看这个,再看后面的例子。Broadcasting two ar...

  • numpy的广播

    参考:numpy中的广播机制 - 小舔哥 - 博客园 把数组铺开,扩展之后无法匹配就无法进行广播。 可以广播的条件...

  • Python|Numpy广播

    广播可以简单理解为用于不同大小数组的二进制通用函数(加、减、乘等)的一组规则。 1. 介绍 对于同样大小的数组,二...

  • numpy广播机制

    在写代码计算梯度的过程中发现了一个以前忽略的点,numpy的广播机制。 在不同形状的矩阵进行对应元素的相加、相减、...

  • Python Data Science, NumPy 2

    这篇文章延续Python Data Science, NumPy 1,介绍广播、高级索引以及数组排序。 广播 广播...

  • NumPy广播(Broadcast)&&NumPy数组比较

    今天看一个通过逻辑回归解决手写数字识别任务的案例,有个之前没有注意到的点,记录一下。 输出如下 那么问题来了cur...

  • Numpy中的广播

    简单举例便于回忆

  • Numpy的广播机制

    来自我的个人博客,https://www.zhouwenzhen.top/post/86684361.html

  • Numpy广播机制 (2020.04.08)

    1.何为广播机制 不规则数组相加机制 2. 三种广播规则 规则1:如果两个数组形状中,只有其中一个维度相同,另一个...

  • NumPy之:理解广播

    简介 广播描述的是NumPy如何计算不同形状的数组之间的运算。如果是较大的矩阵和较小的矩阵进行运算的话,较小的矩阵...

网友评论

      本文标题:Numpy广播

      本文链接:https://www.haomeiwen.com/subject/fzxujxtx.html