Broadcasting

  • expand(扩展数据)
  • without copying data(不复制数据)
  • tf.broadcast_to

Key idea

  1. Insert 1 dim ahead if needed
  2. Expand dims with size 1 to same size
  3. 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?

  1. for real demanding

    • [classes, students, scores]
    • Add bias for every student: +5 score
    • [4,32,8] + [4,32,8]
    • [4,32,8] + [5.0]
  2. 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的更多相关文章

  1. broadcasting Theano vs. Numpy

    broadcasting Theano vs. Numpy broadcast mechanism allows a scalar may be added to a matrix, a vector ...

  2. theano broadcasting

    当我们使用函数对两个数组进行计算时,函数会对这两个数组的对应元素进行计算,因此它要求这两个数组有相同的大小(shape相同).如果两个数组的shape不同的话,会进行如下的广播(broadcastin ...

  3. Arduino live weather broadcasting 实时天气站

    Live broadcasting with arduino get a pc , make it run linux. make arduino catch the weather sensor a ...

  4. numpy 中的 broadcasting 理解

    broadcast 是 numpy 中 array 的一个重要操作. 首先,broadcast 只适用于加减. 然后,broadcast 执行的时候,如果两个 array 的 shape 不一样,会先 ...

  5. MATLAB/Octave warning: mx_el_eq: automatic broadcasting operation applied 错误分析

    在进行对一个mXn的矩阵与mX1的矩阵进行==比较时,原意是想让mXn的矩阵的每一行分别与mX1的矩阵每一行进行比较,得到的结果虽然是对的,但会报一个warning: mx_el_eq: automa ...

  6. 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[ ...

  7. tensor的维度扩张的手段--Broadcasting

    broadcasting是tensorflow中tensor维度扩张的最常用的手段,指对某一个维度上重复N多次,虽然它呈现数据已被扩张,但不会复制数据. 可以这样理解,对 [b,784]@[784,1 ...

  8. 吴裕雄--天生自然TensorFlow2教程:Broadcasting

    Broadcasting可以理解成把维度分成大维度和小维度,小维度较为具体,大维度更加抽象.也就是小维度针对某个示例,然后让这个示例通用语大维度. import tensorflow as tf x ...

  9. 广播 (broadcasting)

    广播 (broadcasting) 飞桨(PaddlePaddle,以下简称Paddle)和其他框架一样,提供的一些API支持广播(broadcasting)机制,允许在一些运算时使用不同形状的张量. ...

随机推荐

  1. 51nod 1122 机器人走方格 V4 【矩阵快速幂】

    首先建立矩阵,给每个格子编号,然后在4*4的格子中把能一步走到的格子置为1,然后乘n次即可,这里要用到矩阵快速幂 #include<iostream> #include<cstdio ...

  2. 《windows核心编程系列》十五谈谈windows线程栈

    谈谈windows线程栈. 当系统创建线程时会为线程预订一块地址空间区域,注意仅仅是预订.默认情况下预定的这块区域的大小是1MB,虽然预订这么多,但是系统并不会给全部区域调拨物理存储器.默认情况下,仅 ...

  3. Linux下VIM配置以及常用快捷键

    一.VIM配置 在目录 /etc/vim下面,有个名为vimrc的文件,这是系统中公共的vim设置文件,对所有用户都有效.而在每个用户的主目录下,都能自己建立私有的设置文件,命名为:“.vimrc”. ...

  4. taskkill帮助信息

    taskkill帮助信息: C:\Users\xusweeter>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { ...

  5. List与类之间的运用,即与javabean的应用

    package com.wh.Object; public class Goods { private String name; private double price; private int n ...

  6. webapp开发学习---Cordova环境搭建

    Cordova 使用HTML, CSS & JS进行移动App开发;多平台共用一套代码;免费开源 步骤:(来自Cordova官网) 1.安装Cordova(在node.js环境下进行安装) 命 ...

  7. XDocument

    XDocument学习(Winform) using System; using System.Collections.Generic; using System.ComponentModel; us ...

  8. Partial(部分方法,局部方法),virtual(虚方法),abstract(抽象方法)

    Partial 部分方法顾明思议是方法的一部分,不完整的,在ide编译时候,会将所有部分方法加载到一起统一编译,如果分部方法没有被实现,编译器就不会.对他们进行编译. 局部类型的限制 (1) 局部类型 ...

  9. iOS Programming UIStoryboard 故事板

    iOS Programming UIStoryboard In this chapter, you will use a storyboard instead. Storyboards are a f ...

  10. 架构(Architecture)随想

    架构(Architecture)的意义: 先不要看什么是架构,先看下architect是什么,没有错,它是建筑师,在一块空地上build高楼大厦的人,它是一个designer,设计好整个大楼,也是一个 ...