<p>I'm getting the hang of TensorFlow and found a solution. I managed to write the following example code to multiply the last index of <span class="math-container">$A$</span> and the first index of <span class="math-container">$B$</span>:</p>
<pre><code>import tensorflow as tf


def stitch(a, b):
    rank_a = int(tf.rank(a))
    shape_a = list(tf.shape(a))
    rank_b = int(tf.rank(b))
    shape_b = list(tf.shape(b))
    rank_c = rank_a + rank_b - 1

    num_pad_right = rank_c - rank_a
    num_pad_left = rank_c - rank_b
    shape_padded_a = shape_a + [1] * num_pad_right
    shape_padded_b = [1] * num_pad_left + shape_b
    shape_c = shape_a[:-1] + shape_b

    padded_a = tf.reshape(a, shape_padded_a)
    padded_b = tf.reshape(b, shape_padded_b)
    padded_c = tf.math.multiply(padded_a, padded_b)
    c = tf.reshape(padded_c, shape_c)
    return c


x = tf.constant([[[1.0, 1.0], [2.0, 2.0]], [[3.0, 3.0], [4.0, 4.0]]])
y = tf.constant([[2.0, 2.0], [2.0, 2.0]])

print(&quot;=== Left tensor ===&quot;)
print(x)
print(&quot;\n&quot;)
print(&quot;=== Right tensor ===&quot;)
print(y)
print(&quot;\n&quot;)
print(&quot;=== Stitched tensor ===&quot;)
print(stitch(x, y))
print(&quot;\n&quot;)
</code></pre>
<p>This outputs</p>
<pre><code>=== Left tensor ===
tf.Tensor(
[[[1. 1.]
  [2. 2.]]

 [[3. 3.]
  [4. 4.]]], shape=(2, 2, 2), dtype=float32)


=== Right tensor ===
tf.Tensor(
[[2. 2.]
 [2. 2.]], shape=(2, 2), dtype=float32)


=== Stitched tensor ===
tf.Tensor(
[[[[2. 2.]
   [2. 2.]]

  [[4. 4.]
   [4. 4.]]]


 [[[6. 6.]
   [6. 6.]]

  [[8. 8.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)
</code></pre>
<p>I don't know if there is a way to do this in a less cumbersome way with native TensorFlow broadcasting rules. If you are aware, let me know. As Snehal said in the comments, I might try my luck on a more programming oriented forum.</p>

CC BY-SA 4.0
2022-11-06T16:13:19.487Z
2022-11-06T16:13:19.487Z
Ben
<p>I'm new to TensorFlow and I want to perform the following operation on two tensors. Let <span class="math-container">$A_{\mu_1,\dots,\mu_M}$</span> be a tensor of rank <span class="math-container">$M$</span> and let <span class="math-container">$B_{\nu_1,\dots,\nu_N}$</span> be a tensor of rank <span class="math-container">$N$</span>. The indices <span class="math-container">$\mu$</span> and <span class="math-container">$\nu$</span> all run over <span class="math-container">$1,\dots,d$</span>.</p>
<p>Now let's pick a dimension <span class="math-container">$i$</span> for tensor <span class="math-container">$A$</span> and a dimension <span class="math-container">$j$</span> for tensor <span class="math-container">$B$</span>. I want to construct the tensor</p>
<p><span class="math-container">$A_{\mu_1,\dots,\rho_i,\dots,\mu_M} B_{\nu_1, \dots,\rho_j, \dots, \nu_N}\;\textrm{(no Einstein summation)}$</span>.</p>
<p>That is, I want to multiply the tensors element-wise along two specifically chosen dimensions. Note that I don't want to contract over <span class="math-container">$\rho$</span>. I want a tensor of dimension <span class="math-container">$M + N - 1$</span>. The final ordering of the indices doesn't matter as long as I can keep track of them.</p>
<p>EDIT: For clarity, let's just multiply the last index of <span class="math-container">$A$</span> and the first index of <span class="math-container">$B$</span> element-wise.</p>
<p>EDIT 2: The solution previously posted here as an edit has been posted as a solution.</p>

CC BY-SA 4.0
2022-10-30T20:15:41.930Z
Can you give an example using real numbers of what you are looking to do?
Snehal Patel
CC BY-SA 4.0
2022-11-01T13:44:25.693Z
I added working code. I don't know if there is a simpler way to do this with native broadcasting. Suggestions are welcome.
Ben
CC BY-SA 4.0
2022-11-05T04:43:28.710Z
Thanks for posting an example.  This does help me visualize what you are trying to do.  This is a viable solution, so it is fair to post it as an answer.  As far as I know, there is no built-in TF2.0 functionality that allows you to do this more easily.  I do think this is more of a programming question, so StackOverflow will be a better place to post this (and you will probably get more input).
Snehal Patel
<tensorflow>
Multiplying tensors element-wise along one dimension
Ben
# Multiplying tensors element-wise along one dimension

(Asked by: username_0 on 2022-10-30)

I'm new to TensorFlow and I want to perform the following operation on two tensors. Let $A\_{\mu\_1,\dots,\mu\_M}$ be a tensor of rank $M$ and let $B\_{\nu\_1,\dots,\nu\_N}$ be a tensor of rank $N$. The indices $\mu$ and $\nu$ all run over $1,\dots,d$.

Now let's pick a dimension $i$ for tensor $A$ and a dimension $j$ for tensor $B$. I want to construct the tensor

$A\_{\mu\_1,\dots,\rho\_i,\dots,\mu\_M} B\_{\nu\_1, \dots,\rho\_j, \dots, \nu\_N}\;\textrm{(no Einstein summation)}$.

That is, I want to multiply the tensors element-wise along two specifically chosen dimensions. Note that I don't want to contract over $\rho$. I want a tensor of dimension $M + N - 1$. The final ordering of the indices doesn't matter as long as I can keep track of them.

EDIT: For clarity, let's just multiply the last index of $A$ and the first index of $B$ element-wise.

EDIT 2: The solution previously posted here as an edit has been posted as a solution.

**Tags:** tensorflow

> **Comments:**
> **username_1**: Can you give an example using real numbers of what you are looking to do?  
> **username_0**: I added working code. I don't know if there is a simpler way to do this with native broadcasting. Suggestions are welcome.  
> **username_1**: Thanks for posting an example.  This does help me visualize what you are trying to do.  This is a viable solution, so it is fair to post it as an answer.  As far as I know, there is no built-in TF2.0 functionality that allows you to do this more easily.  I do think this is more of a programming question, so StackOverflow will be a better place to post this (and you will probably get more input).  

---

## 1 answers

### Answer by username_0 on 2022-11-06. Score: 1 | Accepted answer

I'm getting the hang of TensorFlow and found a solution. I managed to write the following example code to multiply the last index of $A$ and the first index of $B$:
import tensorflow as tf


def stitch(a, b):
    rank_a = int(tf.rank(a))
    shape_a = list(tf.shape(a))
    rank_b = int(tf.rank(b))
    shape_b = list(tf.shape(b))
    rank_c = rank_a + rank_b - 1

    num_pad_right = rank_c - rank_a
    num_pad_left = rank_c - rank_b
    shape_padded_a = shape_a + [1] * num_pad_right
    shape_padded_b = [1] * num_pad_left + shape_b
    shape_c = shape_a[:-1] + shape_b

    padded_a = tf.reshape(a, shape_padded_a)
    padded_b = tf.reshape(b, shape_padded_b)
    padded_c = tf.math.multiply(padded_a, padded_b)
    c = tf.reshape(padded_c, shape_c)
    return c


x = tf.constant([[[1.0, 1.0], [2.0, 2.0]], [[3.0, 3.0], [4.0, 4.0]]])
y = tf.constant([[2.0, 2.0], [2.0, 2.0]])

print("=== Left tensor ===")
print(x)
print("\n")
print("=== Right tensor ===")
print(y)
print("\n")
print("=== Stitched tensor ===")
print(stitch(x, y))
print("\n")

This outputs
=== Left tensor ===
tf.Tensor(
[[[1. 1.]
  [2. 2.]]

 [[3. 3.]
  [4. 4.]]], shape=(2, 2, 2), dtype=float32)


=== Right tensor ===
tf.Tensor(
[[2. 2.]
 [2. 2.]], shape=(2, 2), dtype=float32)


=== Stitched tensor ===
tf.Tensor(
[[[[2. 2.]
   [2. 2.]]

  [[4. 4.]
   [4. 4.]]]


 [[[6. 6.]
   [6. 6.]]

  [[8. 8.]
   [8. 8.]]]], shape=(2, 2, 2, 2), dtype=float32)

I don't know if there is a way to do this in a less cumbersome way with native TensorFlow broadcasting rules. If you are aware, let me know. As Snehal said in the comments, I might try my luck on a more programming oriented forum.


---

