简体   繁体   English

在TensorFlow中收缩张量指数

[英]Contracting tensor indices in TensorFlow

Suppose I have a tensor objects represented as $A_{i_0 i_1 ... i_k ... i_N}$ and $B_{j_0 j_1 ... j_p ... j_M}$ (in Tensorflow the would have shapes of N and M dimensions respectively). 假设我有一个张量对象表示为$A_{i_0 i_1 ... i_k ... i_N}$$B_{j_0 j_1 ... j_p ... j_M}$ (在Tensorflow中,它将具有N和M的形状尺寸分别)。 I want to create a contraction over dimensions k and p , so basically I want to create 我想在尺寸kp上创建一个收缩,所以基本上我想要创建

$$A_{i_0 i_1 ... 0 ... i_N} B_{j_0 j_1 ... 0 ... j_M} + A_{i_0 i_1 ... 1 ... i_N} B_{j_0 j_1 ... 1 ... j_M} + A_{i_0 i_1 ... 2 ... i_N} B_{j_0 j_1 ... 2 ... j_M}....$$

What would be the right ops for this case? 对于这种情况,什么是正确的操作?

Starting from Tensorflow 11 you can use einsum to do that. 从Tensorflow 11开始,您可以使用einsum来做到这一点。

So assuming A_ijkl and B_mnp and supposing you want to contract j with p you can do: 因此,假设A_ijklB_mnp并假设您想要与p签约j ,您可以这样做:

import tensorflow as tf
tf.einsum("ijkl,mnj->iklmn", A,B)

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

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