TF-搞不懂的TF矩阵加法
看谷歌的demo mnist,卷积后加偏执量的代码
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
其中的x_image的维数是[-1, 28, 28, 1],W_conv1的维数是[5, 5, 1, 32], b的维数是[32]
conv2d对x_image和W_conv1进行卷积,结果为[-1, 28, 28, 32],结果就是:
[-1, 28, 28, 32]和[32]的加法。
完全搞不清为什么[-1, 28, 28, 32]和[32]两个完全不同维数可以做加法?而且加出的结果还是[-1, 28, 28, 32]?
于是做了下面的测试:
sess = tf.InteractiveSession()
test1 = tf.ones([,,,],tf.float32)
b1 = tf.ones([])
re1 = test1 + b1
print("shap3={},eval=\n{}".format(b1.shape, b1.eval()))
print("shap4={},eval=\n{}".format(test1.shape, test1.eval()))
print("shap5={},eval=\n{}".format(re1.shape, re1.eval())) test1 = tf.ones([,,,],tf.float32)
b1 = tf.ones([,,,])
re1 = test1 + b1
print("shap6={},eval=\n{}".format(b1.shape, b1.eval()))
print("shap7={},eval=\n{}".format(test1.shape, test1.eval()))
print("shap8={},eval=\n{}".format(re1.shape, re1.eval())) test1 = tf.ones([,,,],tf.float32)
b1 = tf.ones([,,,])
re1 = test1 + b1
print("shap9 ={},eval=\n{}".format(b1.shape, b1.eval()))
print("shap10={},eval=\n{}".format(test1.shape, test1.eval()))
print("shap11={},eval=\n{}".format(re1.shape, re1.eval())) test1 = tf.ones([,,,],tf.float32)
b1 = tf.ones([])
re1 = test1 + b1
print("shap12={},eval=\n{}".format(b1.shape, b1.eval()))
print("shap13={},eval=\n{}".format(test1.shape, test1.eval()))
print("shap14={},eval=\n{}".format(re1.shape, re1.eval()))
test1 = tf.ones([1,2,2,3],tf.float32)
alist = [[[[ 1, 1, 1.],
[ 0, 0, 0.]],
[[ 1, 1, 1.],
[ 0, 0, 0.]]]]
b1 = tf.constant(alist)
re1 = test1 + b1
print("shap15={},eval=\n{}".format(b1.shape, b1.eval()))
print("shap16={},eval=\n{}".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[ 1. 1. 1.]]]]
这个结果说明了什么呢?说明张量加法时,维数不等时会自动扩充,用存在的数字填充。
比如下面这个[4, 3, 2, 3]的矩阵A,
我们把A加上[1, 2, 3]结果为
[[[[1 2 3]
[2 3 4]]
[[3 4 5]
[4 5 6]]
[[5 6 7]
[6 7 8]]]
[[[1 2 3]
[2 3 4]]
[[3 4 5]
[4 5 6]]
[[5 6 7]
[6 7 8]]]
[[[1 2 3]
[2 3 4]]
[[3 4 5]
[4 5 6]]
[[5 6 7]
[6 7 8]]]
[[[1 2 3]
[2 3 4]]
[[3 4 5]
[4 5 6]]
[[5 6 7]
[6 7 8]]]]
TF-搞不懂的TF矩阵加法的更多相关文章
- 深度学习原理与框架-图像补全(原理与代码) 1.tf.nn.moments(求平均值和标准差) 2.tf.control_dependencies(先执行内部操作) 3.tf.cond(判别执行前或后函数) 4.tf.nn.atrous_conv2d 5.tf.nn.conv2d_transpose(反卷积) 7.tf.train.get_checkpoint_state(判断sess是否存在
1. tf.nn.moments(x, axes=[0, 1, 2]) # 对前三个维度求平均值和标准差,结果为最后一个维度,即对每个feature_map求平均值和标准差 参数说明:x为输入的fe ...
- 深度学习原理与框架-Tensorflow基本操作-变量常用操作 1.tf.random_normal(生成正态分布随机数) 2.tf.random_shuffle(进行洗牌操作) 3. tf.assign(赋值操作) 4.tf.convert_to_tensor(转换为tensor类型) 5.tf.add(相加操作) tf.divide(相乘操作) 6.tf.placeholder(输入数据占位
1. 使用tf.random_normal([2, 3], mean=-1, stddev=4) 创建一个正态分布的随机数 参数说明:[2, 3]表示随机数的维度,mean表示平均值,stddev表示 ...
- tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask
1. tf.split(3, group, input) # 拆分函数 3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...
- tf.metrics.sparse_average_precision_at_k 和 tf.metrics.precision_at_k的自己理解
tensorflow最大的问题就是大家都讲算法,不讲解用法,API文档又全是英文的,看起来好吃力,理解又不到位.当然给数学博士看的话,就没问题的. 最近看了一系列非常不错的文章,做一下记录: http ...
- deep_learning_Function_tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法
[Tensorflow] tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法 作用:输出正确的预测结果利用tf.argmax()按行求出真实值y_.预测值y最大值 ...
- tf.nn.conv2d 和 tf.nn.max_pool 中 padding 分别为 'VALID' 和 'SAME' 的直觉上的经验和测试代码
这个地方一开始是迷糊的,写代码做比较分析,总结出直觉上的经验. 某人若想看精准的解释,移步这个网址(http://blog.csdn.net/fireflychh/article/details/73 ...
- python 用嵌套列表做矩阵加法
写一个函数,接收两个由嵌套列表模拟成的矩阵,返回一个嵌套列表作为计算结果,要求运行效果如下: >>> matrix1 = [[1, 1], [-3, 4]] >>> ...
- TF之RNN:TF的RNN中的常用的两种定义scope的方式get_variable和Variable—Jason niu
# tensorflow中的两种定义scope(命名变量)的方式tf.get_variable和tf.Variable.Tensorflow当中有两种途径生成变量 variable import te ...
- tensorflow中 tf.train.slice_input_producer 和 tf.train.batch 函数(转)
tensorflow数据读取机制 tensorflow中为了充分利用GPU,减少GPU等待数据的空闲时间,使用了两个线程分别执行数据读入和数据计算. 具体来说就是使用一个线程源源不断的将硬盘中的图片数 ...
- TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add(转)
1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...
随机推荐
- java堆溢出
java堆是用来存储对象实例的:只要不断创建对象,GC Roots到对象之间有可达路径来避免垃圾回收机制清除这些对象,当对象数量达到最大堆的容量限制的时候就会产生内存溢出异常.异常对战信息为OutOf ...
- 东北育才 NOIP模拟赛第1场
终于400了.这套题很鬼畜.两道贪心. GRACE sort过后,不能直接统计,本人毫无多想,相同的直接放在一起.结果太多人AC. SUM sigma+异或和(可使用前缀和处理),本人毫无考虑乱MOD ...
- 数据库之SQLite的介绍与使用20180705
一.SQLite 简介 1.介绍 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌 ...
- ASP.NET MVC3 入门指南之数据验证[源码RAR下载]
http://www.cnblogs.com/BingoLee/archive/2011/12/23/2298822.html 前言: 无论你编写什么样的网页程序,都需要对用户的数据进行验证,以确数据 ...
- C++ public private protect 继承关系(链接)
基础链接 总结: public继承基类成员访问权限没有变化; protected继承基类public和protected权限变为protected,基类private不变. private继承基类p ...
- C++ explicit constructor/copy constructor note
C++:explict 作用显示声明构造函数只能被显示调用从而阻止编译器的隐式转换,类似只能用()显示调用,而不能=或者隐式调用 #include <iostream> #include ...
- Docker入门与应用系列(二)镜像管理
1.1 什么是镜像 简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统. 1.2 镜像从哪里来 Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容 ...
- java基础-System类常用方法介绍
java基础-System类常用方法介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.System类概念 在API中system类介绍的比较简单,我们给出定义,system中 ...
- Liunx操作指令搜素引擎
链接:http://wangchujiang.com/linux-command/c/vi.html
- javascript精雕细琢(一):var let const function声明的区别
目录 引言 一.var 二.let 三.const 四.function 五.总结 引言 在学习javascript的过程中,变量是无时无刻不在使用的.那么相对应的,变量声明方法也如是. ...