TensorFlow 广播 [英] TensorFlow broadcasting

查看:21
本文介绍了TensorFlow 广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

广播是使具有不同形状的数组具有用于算术运算的兼容形状的过程.在 numpy 中,我们可以广播数组.TensorFlow 图是否支持类似于 numpy 的广播?

Broadcasting is the process of making arrays with different shapes have compatible shapes for arithmetic operations. In numpy, we can broadcast arrays. Does TensorFlow graph support broadcasting similar to the numpy one?

推荐答案

是的,支持.打开终端并尝试以下操作:

yes it is supported. Open a terminal and try this:

import tensorflow as tf

#define tensors
a=tf.constant([[10,20],[30,40]]) #Dimension 2X2
b=tf.constant([5])
c=tf.constant([2,2])
d=tf.constant([[3],[3]])

sess=tf.Session() #start a session

#Run tensors to generate arrays
mat,scalar,one_d,two_d = sess.run([a,b,c,d])

#broadcast multiplication with scalar
sess.run(tf.multiply(mat,scalar))

#broadcast multiplication with 1_D array (Dimension 1X2)
sess.run(tf.multiply(mat,one_d))

#broadcast multiply 2_d array (Dimension 2X1)
sess.run(tf.multiply(mat,two_d))

sess.close()

这篇关于TensorFlow 广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆