python - 沿任意轴相乘?

标签 python tensorflow

如果我有一个形状为 [batch_size, height, width, depth] 的层的输出和另一个形状为 [depth] 的张量,我如何将第一个张量乘以第二个,这样沿 depth 方向的每个切片都乘以第二个张量中的相应值。也就是说,如果第二个张量是[4, 5, 6],那么乘法是:

tensor1[:, :, :, 0] * 4
tensor1[:, :, :, 1] * 5
tensor1[:, :, :, 2] * 6

还有,不知道有没有这种乘法的名字去搜一下?谢谢!

最佳答案

这很简单。只需将两个张量相乘即可。例如:

import tensorflow as tf

tensor = tf.Variable(tf.ones([2, 2, 2, 3]))
depth = tf.constant([4, 5, 6], dtype=tf.float32)
result = tensor * depth

sess = tf.Session()
sess.run(tf.initialize_all_variables())
print(sess.run(result))

关于python - 沿任意轴相乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432138/

相关文章:

python - 如何在 Python3 中使用/n?

python - tf.function 输入参数

python - 在 TensorFlow 2 中使用 tf.ConfigProto 初始化 tf.Session 有什么等价物?

tensorflow - 神经网络中的分类特征值

python - 如何在 Tensorflow2 中只保存张量而不是模型

Python Pil 将 GreyScale Tif 转换为 RGB

python - 在 python 中在另一个类中创建一个类的正确方法?

python - 导入 rpy2.objects 出现错误 : unable to initialize the JiT

python - 理解 : multiple values per iteration

c++ - 如何在用户定义的tensorflow op中使用随机数?