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]

    1. [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] √

  1. import tensorflow as tf
  1. x = tf.random.normal([4,32,32,3])
  2. x.shape
  1. TensorShape([4, 32, 32, 3])
  1. (x+tf.random.normal([3])).shape
  1. TensorShape([4, 32, 32, 3])
  1. (x+tf.random.normal([32,32,1])).shape
  1. TensorShape([4, 32, 32, 3])
  1. (x+tf.random.normal([4,1,1,1])).shape
  1. TensorShape([4, 32, 32, 3])
  1. try:
  2. (x+tf.random.normal([1,4,1,1])).shape
  3. except Exception as e:
  4. print(e)
  1. Incompatible shapes: [4,32,32,3] vs. [1,4,1,1] [Op:Add] name: add/
  1. (x+tf.random.normal([4,1,1,1])).shape
  1. TensorShape([4, 32, 32, 3])
  1. b = tf.broadcast_to(tf.random.normal([4,1,1,1]),[4,32,32,3])
  1. b.shape
  1. TensorShape([4, 32, 32, 3])

Broadcast VS Tile

  1. a = tf.ones([3,4])
  1. a.shape
  1. TensorShape([3, 4])
  1. a1 = tf.broadcast_to(a,[2,3,4])
  1. a1.shape
  1. TensorShape([2, 3, 4])
  1. a2 = tf.expand_dims(a,axis=0) # 0前插入一维
  1. a2.shape
  1. TensorShape([1, 3, 4])
  1. a2 = tf.tile(a2,[2,1,1]) # 复制一维2次,复制二、三维1次
  1. a2.shape
  1. 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. CentOS中设置Windows共享文件夹

    在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...

  2. 使用Quartz实现定时作业

    该文章是系列文章 基于.NetCore和ABP框架如何让Windows服务执行Quartz定时作业 的其中一篇. Quartz是一个开源的作业调度框架,准确的称谓应该是 Quartz.Net,它是Ja ...

  3. django 第一个项目测试

    我们安装好Django以后就可以使用Django命令来创建项目了 1.Django 创建第一个项目 安装 Django 之后,您现在应该已经有了可用的管理工具 django-admin.py.我们可以 ...

  4. 前端基础jQuery

    jQury jQuery 是一个 JavaScript 函数库,jQuery 极大地简化了 JavaScript 编程. jQuery库包含以下功能: HTML 元素选取 HTML 元素操作 CSS ...

  5. Object流

  6. jacaScript数组

    1.var arr=['1','2','3'] typeof arr (判断数组类型)    print(arr)打印数组内容 2.arr[100]='x',  数组中间自动添加,alert(arr. ...

  7. 轻松搞懂Java中的自旋锁

    前言 在之前的文章<一文彻底搞懂面试中常问的各种“锁”>中介绍了Java中的各种“锁”,可能对于不是很了解这些概念的同学来说会觉得有点绕,所以我决定拆分出来,逐步详细的介绍一下这些锁的来龙 ...

  8. CF940D Alena And The Heater

    思路: 模拟. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 1e9; ], n; string b; ...

  9. Program received signal SIGILL, Illegal instruction

    Program received signal SIGILL, Illegal instruction 这个错误,发现是直接在printf 的%s中直接使用string类型,而没有使用c字符串格式造成 ...

  10. 用RecyclerView做一个小清新的Gallery效果

    一.简介 RecyclerView现在已经是越来越强大,且不说已经被大家用到滚瓜烂熟的代替ListView的基础功能,现在RecyclerView还可以取代ViewPager实现Banner效果,当然 ...