keras 两Tensor在轴上相乘

nimxete2  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(168)

假设我有两个Tensor其中TensorA有形状(100,7),TensorB具有形状(100,7,64).我想从A和B中选择第一项,然后将它们乘以tf.matmul,得到shape(1,64),然后下一项,依此类推,最后合并所有Tensor,得到一个有形状的Tensor(100,64).我找不到任何函数来执行此操作...有帮助吗?
编辑:我可以用下面的代码做这个,但是非常慢,有没有tensorflow 函数?

outputs = []
for i in range(A.shape[0]):
    outputs = outputs + [tf.matmul(tf.expand_dims(A[i],0),B[i])[0]]
outputs = tf.stack(outputs,axis=0)
3mpgtkmj

3mpgtkmj1#

那是因为我不喜欢挤东西

[样品]:

Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)                         
Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)
Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)
Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)
tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]).shape) + " and " + str(tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64])
tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) )
tf.squeeze(tf.slice(tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) ), [0, 0, 0], [100, 1, 64])))

[输出]:

* Slice or slice windows

1. Create constants Matrix A and Matrix B with shape: (100, 7, 1) and (100, 7, 64)
   Matrix_A = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 1, axis=2)
   Matrix_B = tf.linspace(tf.zeros([100, 7], tf.float32), tf.math.multiply(tf.ones([100, 7], tf.float32), tf.constant([10.0], tf.float32)), 64, axis=2)

 ___________________________________________________________________________________________________________________________________________________________________________
2. Slice Matrix A and Matrix B target shape: (100, 1, 1) and (100, 1, 64)
   tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1])
   tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64])

 ___________________________________________________________________________________________________________________________________________________________________________
3. Multiply them into target shape: (100, 1, 64)
   tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) )

 ___________________________________________________________________________________________________________________________________________________________________________
4. Squeeze dimension: tf.Tensor([100  64], shape=(2,), dtype=int32)
   tf.squeeze(tf.slice(tf.math.multiply( tf.slice(Matrix_A, [0, 0, 0], [100, 1, 1]), tf.slice(Matrix_B, [0, 0, 0], [100, 1, 64]) ), [0, 0, 0], [100, 1, 64])))

 ___________________________________________________________________________________________________________________________________________________________________________

相关问题