简体   繁体   中英

How to mutiply 2 tensors of different shapes using tensorflow

I have 2 tensors a and b. I want to obtain the product of a and b as ([[1,2,3,4],[5,6,7,8]]). But a has 5 values per tensor element and b has 4. what is the right approach to multiply and b and obtain the desired result. I obtained an error. InvalidArgumentError: Incompatible shapes: [2,4] vs. [2,5] [Op:Mul].

a=tf.constant([[1,2,3,4,8],[5,6,7,8,9]])
b=tf.constant([[1,1,1,1],[1,1,1,1]])
tf.multiply(b, a)

As you stated in the question you want to obtain subset of the "a" tensor.

You can use tf.slice for this

For example:

import tensorflow as tf

a=tf.constant([[1,2,3,4,8],[5,6,7,8,9]])
b=tf.constant([[1,1,1,1],[1,1,1,1]])
print(tf.slice(a, [0, 0], [2, 4]))
tf.Tensor(
[[1 2 3 4]
 [5 6 7 8]], shape=(2, 4), dtype=int32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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