简体   繁体   English

张量流元素乘法广播?

[英]tensorflow element-wise multiplication broadcasting?

Does tensorflow offer any function for element-wise multiplication broadcasting on the last dimension? tensorflow 是否为最后一个维度上的元素乘法广播提供任何功能?

Here is an example of what I'm trying to do and what does not work:这是我正在尝试做什么以及什么不起作用的示例:

import tensorflow as tf
x = tf.constant(5, shape=(1, 200, 175, 6), dtype=tf.float32)
y = tf.constant(1, shape=(1, 200, 175), dtype=tf.float32)
tf.math.multiply(x, y)

Essentially, I would like for each one of x 's slice along the last dimension, do an element-wise matrix multiplication with y .本质上,我希望对x沿最后一个维度的每个切片,与y逐元素矩阵乘法。

I have found this question asking similar operation: Efficient element-wise multiplication of a matrix and a vector in TensorFlow我发现这个问题提出了类似的操作: Efficient element-wise multiplication of a matrix and a vector in TensorFlow

Unfortunately, the suggested approach (using tf.multiply() ) no longer work now.不幸的是,建议的方法(使用tf.multiply() )现在不再有效。 The corresponding tf.math.multiply also does not work, as the code above gives me the error below:相应的tf.math.multiply也不起作用,因为上面的代码给了我以下错误:

Traceback (most recent call last):
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1864, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 175 and 200 for 'Mul' (op: 'Mul') with input shapes: [1,200,175,6], [1,200,175].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py", line 322, in multiply
    return gen_math_ops.mul(x, y, name)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 6490, in mul
    "Mul", x=x, y=y, name=name)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3616, in create_op
    op_def=op_def)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 2027, in __init__
    control_input_ops)
  File "/home/yuqiong/miniconda3/envs/deep/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 1867, in _create_c_op
    raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 175 and 200 for 'Mul' (op: 'Mul') with input shapes: [1,200,175,6], [1,200,175].

I can think of a working approach: duplicate y 6 times so it has the exact same shape as x , then do element-wise multiplication.我可以想到一种可行的方法:将y复制 6 次,使其具有与x完全相同的形状,然后进行元素乘法。

But are there faster and memory efficient way of doing this in tensorflow?但是在张量流中是否有更快且内存高效的方法来做到这一点?

This should achieve what you want:这应该可以实现您想要的:

x = np.array([[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]])
# [[[ 1  2  3]
#   [ 4  5  6]
#   [ 7  8  9]
#   [10 11 12]]]
y = np.array([[1,2,3,4]])
# [[1 2 3 4]]
y = tf.expand_dims(y, axis=-1)
mul = tf.multiply(x, y)
# [[[ 1  2  3]
#   [ 8 10 12]
#   [21 24 27]
#   [40 44 48]]]

Finally, using the shapes you need:最后,使用您需要的形状:

x = np.random.rand(1, 200, 175, 6)
y = np.random.rand(1, 200, 175)
y = tf.expand_dims(y, axis=-1)
mul = tf.multiply(x, y)
with tf.Session() as sess:
    print(sess.run(mul).shape)
    # (1, 200, 175, 6)
​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM