Broadcasting
Broadcasting
- expand(扩展数据)
- without copying data(不复制数据)
- tf.broadcast_to
Key idea
- Insert 1 dim ahead if needed
- Expand dims with size 1 to same size
- example:
[4,16,16,32]
[32]
[4,16,16,32]
[1,1,1,32]
[4,16,16,32]
[4,16,16,32]

How to understand?
When it has no axis
- Create a new concepy
- [classes, students, scores] + [scores]
When it has dim of size 1
- Treat it shared by all
- [classes,students,scores] + [students,1]
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象。也就是小维度针对某个示例,然后让这个示例通用语大维度。
Why broadcasting?
for real demanding
- [classes, students, scores]
- Add bias for every student: +5 score
- [4,32,8] + [4,32,8]
- [4,32,8] + [5.0]
memory consumption
- [4,32,8] -> 1024
- bias = [8]: [5.0,5.0,5.0,...] -> 8
Broadcastable?
Match from Last dim!
- if current dim=1, expand to same
- if either has no dim, insert one dim and expand to same
- otherwise, Not Broadcastable
[4,32,14,14]
[1,32,1,1] -> [4,32,14,14] √
[14,14] -> [1,1,14,14] -> [4,32,14,14] √
[2,32,14,14] ×
[3] √
[32,32,1] √
[4,1,1,1] √
import tensorflow as tf
x = tf.random.normal([4,32,32,3])
x.shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([3])).shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([32,32,1])).shape
TensorShape([4, 32, 32, 3])
(x+tf.random.normal([4,1,1,1])).shape
TensorShape([4, 32, 32, 3])
try:
(x+tf.random.normal([1,4,1,1])).shape
except Exception as e:
print(e)
Incompatible shapes: [4,32,32,3] vs. [1,4,1,1] [Op:Add] name: add/
(x+tf.random.normal([4,1,1,1])).shape
TensorShape([4, 32, 32, 3])
b = tf.broadcast_to(tf.random.normal([4,1,1,1]),[4,32,32,3])
b.shape
TensorShape([4, 32, 32, 3])
Broadcast VS Tile
a = tf.ones([3,4])
a.shape
TensorShape([3, 4])
a1 = tf.broadcast_to(a,[2,3,4])
a1.shape
TensorShape([2, 3, 4])
a2 = tf.expand_dims(a,axis=0) # 0前插入一维
a2.shape
TensorShape([1, 3, 4])
a2 = tf.tile(a2,[2,1,1]) # 复制一维2次,复制二、三维1次
a2.shape
TensorShape([2, 3, 4])
Broadcasting的更多相关文章
- broadcasting Theano vs. Numpy
broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...
- theano broadcasting
当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...
- Arduino live weather broadcasting 实时天气站
Live broadcasting with arduino get a pc , make it run linux. make arduino catch the weather sensor a ...
- numpy 中的 broadcasting 理解
broadcast 是 numpy 中 array 的一个重要操作. 首先,broadcast 只适用于加减. 然后,broadcast 执行的时候,如果两个 array 的 shape 不一样,会先 ...
- MATLAB/Octave warning: mx_el_eq: automatic broadcasting operation applied 错误分析
在进行对一个mXn的矩阵与mX1的矩阵进行==比较时,原意是想让mXn的矩阵的每一行分别与mX1的矩阵每一行进行比较,得到的结果虽然是对的,但会报一个warning: mx_el_eq: automa ...
- some code about numpy and notes about copy&broadcasting
import numpy as np np.__version__ #版本 #由于python的list不要求存储同样的类型,但是效率不高. L = [i for i in range(10)] L[ ...
- tensor的维度扩张的手段--Broadcasting
broadcasting是tensorflow中tensor维度扩张的最常用的手段,指对某一个维度上重复N多次,虽然它呈现数据已被扩张,但不会复制数据. 可以这样理解,对 [b,784]@[784,1 ...
- 吴裕雄--天生自然TensorFlow2教程:Broadcasting
Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...
- 广播 (broadcasting)
广播 (broadcasting) 飞桨(PaddlePaddle,以下简称Paddle)和其他框架一样,提供的一些API支持广播(broadcasting)机制,允许在一些运算时使用不同形状的张量. ...
随机推荐
- 【插件开发】—— 14 Site is incorrect!编辑器启动报错!
前言 博文纵览 最近在弄编辑器的时候出现了一个十分尴尬的错误!这里收录一下: BUG如下图所示: 目测堆栈,与自己开发的代码无关.完全是Eclipse自己初始化的时候遇到了问题,最头疼的就是这种问题. ...
- Increasing Sequence CodeForces - 11A
Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...
- static属性
static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量 用static修饰的属性和方法称为静态属性和方法 需要注意的是 静态属性和方法属于类方法,加载类 ...
- Android最简单的实例 :环境搭建及HelloWorld
Android开发之旅:环境搭建及HelloWorld 2010-04-12 00:45 by 吴秦, 883961 阅读, 140 评论, 收藏, 编辑 ——工欲善其事必先利其器 引言 本系列适合 ...
- MongoDB操作简记
一.数据库操作 1.显示当前选择的数据库 [root@weekend05 ~]# mongod --dbpath /data/db/ [root@weekend05 ~]# mongo MongoDB ...
- React Native for Android 学习
前言 Facebook 在2015.9.15发布了 React Native for Android,把 JavaScript 开发技术扩展到了移动Android平台.基于React的React Na ...
- 阿里云服务器 Access to the path '....' is denied.解决方法
昨天公司项目迁移服务器,从自己服务器迁移到阿里云服务器,部署完成后发现有一个页面要读取磁盘上的静态文件就报错了... 如图: 解决办法: 在 Web.Config 的 <System.Web&g ...
- 日常记录-代码中Background后Padding 失效
近日,在开发过程中 遇到了 Layout 代码中设置 Background 后,padding失效的问题,只是在Android 4.4.4 和 4.4.2 的手机上遇到了. 网上搜索了下,说是 4.4 ...
- SharedPrefences的用处
存储数据 SharedPreferences.Editor edit = getSharedPreferences("data", MODE_PRIVATE).edit(); ed ...
- iOS Programming Localization 本地化
iOS Programming Localization 本地化 Internationalization is making sure your native cultural informatio ...