broadcasting Theano vs. Numpy

broadcast mechanism allows a scalar may be added to a matrix, a vector to a matrix or a scalar to a vecotor.

Examples

T and F stands for True and False respectively, denoting which dimension can be broadcasted.

Diference

  1. numpy broadcast dynamically;
  2. theano needs to knows, for any operations which supports broadcasting, which dimensions will need to be broadcasted.

Numpy Broadcasting

broadcasting describe how numpy treats arrays with difference shapes during arithmetic operations:

the smaller array is broadcast across the larger array so that they have compatible shapes

Simple Case

in this case, the two arrays must have exactly the same shape:

a=np.array([1.0,2.0,3.0])
b=np.array([2.0,2.0,2.0])
print a*b
>>> array([2., 4., 6.])

numpy broadcast mechanism relaxes this constraint when the arrays' shape meet certain constraints:

  1. they are equal, or
  2. one of them is 1
a=np.array([1.0,2.0,3.0])
b=2
print a*b
>>> array([2., 4., 6.])

rules

Image  (3d array): 256 x 256 x 3
Scale (1d array): 3
Result (3d array): 256 x 256 x 3
A      (4d array):  8 x 1 x 6 x 1
B (3d array): 7 x 1 x 5
Result (4d array): 8 x 7 x 6 x 5

more examples

A      (2d array):  5 x 4
B (1d array): 1
Result (2d array): 5 x 4 A (2d array): 5 x 4
B (1d array): 4
Result (2d array): 5 x 4 A (3d array): 15 x 3 x 5
B (3d array): 15 x 1 x 5
Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5
B (2d array): 3 x 5
Result (3d array): 15 x 3 x 5 A (3d array): 15 x 3 x 5
B (2d array): 3 x 1
Result (3d array): 15 x 3 x 5

broadcasting Theano vs. Numpy的更多相关文章

  1. 关于PDNN、Theano、Numpy以及Scipy的安装

    最近为了用下PDNN,先得安装这玩意,不装不知道,一装吓一跳,依赖关系也太多了吧,顿时有种贵圈真乱的感觉,如图1. 不过这B还得装下去. 图1 安装PDNN的依赖关系 之前也碰了好多问题,不过各种参考 ...

  2. Theano 学习笔记(一)

    Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...

  3. python 之 theano学习:

    (1)theano主要支持符号矩阵表达式 (2)theano与numpy中都有broadcasting:numpy中是动态的,而theano需要在这之前就知道是哪维需要被广播.针对不同类型的数据给出如 ...

  4. theano支持的数组、向量、矩阵表达式

    1)theano主要支持符号矩阵表达式 (2)theano与numpy中都有broadcasting:numpy中是动态的,而theano需要在这之前就知道是哪维需要被广播.针对不同类型的数据给出如下 ...

  5. ubuntu系统theano和keras的安装

    说明:系统是unbuntu14.04LTS,32位的操作系统,以前安装了python3.4,现在想要安装theano和keras.步骤如下: 1,安装pip sudo apt-get install ...

  6. theano 实现图像局部对比度归一化

    很多时候我们需要对图像进行局部对比度归一化,比如分块CNN的预处理阶段.theano对此提供了一些比较方便的操作. 局部归一化的一种简单形式为: 其中μ和σ分别为局部(例如3x3的小块)的均值和标准差 ...

  7. theano中的dimshuffle

    theano中的dimshuffle函数用于对张量的维度进行操作,可以增加维度,也可以交换维度,删除维度. 注意的是只有shared才能调用dimshuffle() 'x'表示增加一维,从0d sca ...

  8. Theano2.1.5-基础知识之打印出theano的图

    来自:http://deeplearning.net/software/theano/tutorial/printing_drawing.html Printing/Drawing Theano gr ...

  9. Theano2.1.18-基础知识之theano的扩展

    来自:http://deeplearning.net/software/theano/tutorial/extending_theano.html Extending Theano 该教程覆盖了如何使 ...

随机推荐

  1. UTFGrid

    UTFGrid UTFGrid is a specification for rasterized interaction data. As of version 1.2, it was remove ...

  2. iOS中NSLog输出格式大全

    iOS开发中的输出格式大全: %@                   对象 %d, %i               整数%u                    无符整形%f           ...

  3. QQ空间/朋友圈类界面的搭建

    类似于QQ空间的布局主要是在说说信息.点赞.回复三大部分的自适应布局上. 当我们需要搭建类似QQ空间.微信朋友圈的界面的时候,可做如下操作: 创建一个对应的model类: 创建一个对应model类的f ...

  4. swift-运算符

    运算符:+ , - , * , / ,%, ++,-- OC和swiftch除了取模运算符,其他用法都一样 可以检测 //var num:UInt8 = 255 + 1 不可以检测 var num1: ...

  5. 4.1/4.2 多线程进阶篇<上>(Pthread & NSThread)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Githubhttps://github.com/shorfng ...

  6. Oracle 外网访问

    环境:centos7+oracle 11gr2 公网:固定IP 症状:1521端口正常,netmanager配置测试正常,plsql连接提示ORA-12514: TNS:listener does n ...

  7. lucene自定义过滤器

    先介绍下查询与过滤的区别和联系,其实查询(各种Query)和过滤(各种Filter)之间非常相似,可以这样说只要用Query能完成的事,用过滤也都可以完成,它们之间可以相互转换,最大的区别就是使用过滤 ...

  8. vs2013 git 使用总结

    一.参与别人已经建好的项目 方法1.打开VS2013,切换到“团队资源管理器”,点上方“主页”右侧的下拉三角,选择项目->连接到团队项目,然后选择“克隆”,填入Git的Remote Url和要克 ...

  9. Spring Bean的加载

    Spring加载bean的原则:不等bean创建完成就会将创建bean的ObjectFactory提早曝光加入到缓存中.   单例bean在Spring容器里只会创建一次,后续创建时会首先从缓存中获取 ...

  10. Java 数组

    数组对于每一门编程语言来说都是重要的数据结构之一,当然不同语言对数组的实现及处理也不尽相同. Java语言中提供的数组是用来存储固定大小的同类型元素. 你可以声明一个数组变量,如numbers[100 ...