【问题标题】:TensorFlow Batch Outer ProductTensorFlow Batch 外积
【发布时间】:2016-02-04 23:37:59
【问题描述】:

我有以下两个张量:

x, with shape [U, N]
y, with shape [N, V]

我想执行批量外积:我想将x 第一列中的每个元素乘以y 第一行中的每个元素,得到一个形状为[U, V] 的张量,然后x 的第二列乘y 的第二行,以此类推。最终张量的形状应该是[N, U, V],其中N 是批量大小。

在 TensorFlow 中是否有任何简单的方法可以实现这一点?我尝试使用 batch_matmul() 没有成功。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    也许,使用tf.einsum 有一个更优雅的解决方案:

    result = tf.einsum("un,nv->nuv", x, y)
    

    【讨论】:

      【解决方案2】:

      下面的工作,使用tf.batch_matmul()吗?

      print x.get_shape()  # ==> [U, N]
      print y.get_shape()  # ==> [N, V]
      
      x_transposed = tf.transpose(x)
      print x_transposed.get_shape()  # ==> [N, U]
      
      x_transposed_as_matrix_batch = tf.expand_dims(x_transposed, 2)
      print x_transposed_as_matrix_batch.get_shape()  # ==> [N, U, 1]
      
      y_as_matrix_batch = tf.expand_dims(y, 1)
      print y_as_matrix_batch.get_shape()  # ==> [N, 1, V]
      
      result = tf.batch_matmul(x_transposed_as_matrix_batch, y_as_matrix_batch)
      print result.get_shape()  # ==> [N, U, V]
      

      【讨论】:

        猜你喜欢
        • 2017-07-10
        • 1970-01-01
        • 2017-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多