失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 详解tf.nn.bias_add和tf.add tf.add_n的区别

详解tf.nn.bias_add和tf.add tf.add_n的区别

时间:2019-09-17 14:48:21

相关推荐

详解tf.nn.bias_add和tf.add tf.add_n的区别

任何关于算法、编程、AI行业知识或博客内容的问题,可以随时扫码关注公众号「图灵的猫」,加入”学习小组“,沙雕博主在线答疑~此外,公众号内还有更多AI、算法、编程和大数据知识分享,以及免费的节点和学习资料。其他平台(知乎/B站)也是同名「图灵的猫」,不要迷路哦~

tf.add(x,y,name=None)

x:a tensor musut be one of thefollowingtypes:half,float32,float64,uint8,int8,int16,int32,int64,complex64,complex128,string.

y: ATensor. Must have the same type asx.

name: A name for the operation (optional).

import tensorflow as tfa=tf.constant([[1,1],[2,2],[3,3]],dtype=tf.float32)b=tf.constant([1,-1],dtype=tf.float32)c=tf.constant([1],dtype=tf.float32)with tf.Session() as sess:print('bias_add:')print(sess.run(tf.nn.bias_add(a, b)))#执行下面语句错误#print(sess.run(tf.nn.bias_add(a, c)))print('add:')print(sess.run(tf.add(a, c)))

输出结果:

bias_add:

[[ 2. 0.]

[ 3. 1.]

[ 4. 2.]]

add:

[[ 2. 2.]

[ 3. 3.]

[ 4. 4.]]

tf.add_n(inputs,name=None)

函数是实现一个列表的元素的相加。就是输入的对象是一个列表,列表里的元素可以是向量,矩阵等但没有广播功能

例子:

import tensorflow as tf;import numpy as np;input1 = tf.constant([1.0, 2.0, 3.0])input2 = tf.Variable(tf.random_uniform([3]))output = tf.add_n([input1, input2])with tf.Session() as sess:sess.run(tf.initialize_all_variables())print (sess.run(input1 + input2))print (sess.run(output))

输出:

[ 1.30945706 2.29760814 3.81558323][ 1.30945706 2.29760814 3.81558323]

如果觉得《详解tf.nn.bias_add和tf.add tf.add_n的区别》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。