Tensorflow Dividing a [X, 10, 10, 1] matrix by [X, 1]

Question:

I’m trying to divide a [X, 10, 10, 1] matrix by the sum of each [10, 10, 1] matrix – so dividing [X, 10, 10, 1] by [X, 1] however using tf.broadcast_to gives an "incompatible shapes" error as shown directly below:

t1 = tf.ones([128, 10, 10, 1]) 
t1_sum = tf.reduce_sum(t1, [1, 2])  # Yields a [128, 1]
t1_sum_reshaped = tf.broadcast_to(t1_sum, (t1.shape))
divided = tf.math.divide(t1, t1_sum_reshaped)

Error w/ X=128 when using tf.broadcast_to: Incompatible shapes: [128,1] vs. [128,10,10,1] [Op:BroadcastTo]

My workaround to get division working is as follows, but it seems to be awfully slow (and dumb)

t1 = tf.ones([128, 10, 10, 1])
t1_sum = tf.reduce_sum(t1, [1, 2])                                                                            # Yields a [128, 1]
t1_sum = tf.stack([t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum], axis=-2)  # Yields a [128, 10, 1]
t1_sum = tf.stack([t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum, t1_sum], axis=-2)  # Yields a [128, 10, 10, 1]
divided = tf.math.divide(t1, t1_sum)

Is there a better way to do this?

Asked By: JoshW

||

Answers:

Seems like you can add the keepdims=True argument to your code and it will work as intended:

t1 = tf.ones([128, 10, 10, 1])
t1_sum = tf.reduce_sum(t1, [1, 2], keepdims=True) # Yields a [128, 1, 1, 1]
t1_sum_reshaped = tf.broadcast_to(t1_sum, t1.shape)
divided = tf.math.divide(t1, t1_sum_reshaped)

After doing this you can remove the line t1_sum_reshaped = tf.broadcast_to(t1_sum, t1.shape since it already has the desired shape.

t1 = tf.ones([128, 10, 10, 1])
t1_sum = tf.reduce_sum(t1, [1, 2], keepdims=True) # Yields a [128, 1, 1, 1]
divided = tf.math.divide(t1, t1_sum)
Answered By: smoks
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.