【问题标题】:Tensorflow multiplication 3D tensor by 2D matrixTensorflow 将 3D 张量乘以 2D 矩阵
【发布时间】:2018-06-12 08:12:35
【问题描述】:

我正在尝试将 3D 张量乘以 2D 矩阵,但有一个未知维度。 我在这里查看了所有关于此的帖子,但没有找到我想要的。

我有这些参数:

T - 形状(M,N)

L - 形状 (?,M,M)

F - 形状 (?, N)

我想用输出形状 (?,M) 做乘法 L * T * F。

我尝试扩展尺寸等。

不幸的是,我总是丢失 ? 维。

感谢您的建议。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    你可以这样实现。

    L --> [?, M, M]
    T --> [M, N]
    tensordot(L,T) axes [[2], [0]] --> [?,M, N]
    F --> [?, N] --> expand axis --> [?, N, 1]
    matmul [?, M, N], [?, N, 1] --> [?, M, 1] --> squeeze --> [?, M]
    

    放在一起:

    tf.squeeze(tf.matmul(tf.tensordot(L,T, axes=[[2],[0]]),F[...,None]))
    

    【讨论】:

      【解决方案2】:

      作为一名学习者,我发现问题和答案非常神秘。所以我为自己简化了。

      import tensorflow as tf
      
      L = tf.placeholder(tf.float32, shape=[None, 5, 5])
      
      T = tf.placeholder(tf.float32, shape=[ 5, 10])
      
      F = tf.placeholder(tf.float32, shape=[ None, 10])
      
      print ((tf.tensordot(L,T, axes=[[2],[0]])).get_shape)
      
      # This is more cryptic. Not really sure.
      #print(F[...,None].get_shape) 
      print( tf.expand_dims(F,2).get_shape)
      
      finaltensor = tf.matmul(tf.tensordot(L,T, axes=[[2],[0]]),F[...,None])
      
      print (finaltensor.get_shape)
      
      squeezedtensor = tf.squeeze(finaltensor)
      
      print (tf.shape(squeezedtensor))
      

      除了最后一行之外,所有打印的内容都是清晰的。

      <bound method Tensor.get_shape of <tf.Tensor 'Tensordot:0' shape=(?, 5, 10) dtype=float32>>
      
      <bound method Tensor.get_shape of <tf.Tensor 'ExpandDims:0' shape=(?, 10, 1) dtype=float32>>
      
      <bound method Tensor.get_shape of <tf.Tensor 'MatMul:0' shape=(?, 5, 1) dtype=float32>>
      
      Tensor("Shape:0", shape=(?,), dtype=int32)
      

      【讨论】:

        猜你喜欢
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多