gpt4 book ai didi

python - 沿任意轴相乘?

转载 作者:行者123 更新时间:2023-11-28 22:33:56 25 4
gpt4 key购买 nike

如果我有一个形状为 [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/

25 4 0
文章推荐: python - 用于从 Google 云存储呈现静态图像的 Django 设置
文章推荐: python - 为什么我创建的函数出现 TypeError : f() takes 0 positional arguments but 1 was given
文章推荐: python - 无法选择值 - 错误 "Select only works on