【问题标题】:Generating a matrix from pairwise comparison on a single vector in TensorFlow在 TensorFlow 中通过对单个向量的成对比较生成矩阵
【发布时间】:2018-08-26 00:26:56
【问题描述】:

我有一个一维向量,想根据 TensorFlow 中向量的成对比较生成一个矩阵。我需要将向量中的每个元素与所有其他元素(包括它自己)进行比较,如果它们相同,则相应的矩阵值将为 1,否则为 -1。比如有[1,2,3,4,1]的向量,那么想要的矩阵就是

[[1,-1,-1,-1,1],
 [-1,1,-1,-1,-1],
 [-1,-1,1,-1,-1],
 [-1,-1,-1,1,-1],
 [1,-1,-1,-1,1]].

问题是如何在TensorFlow中生成这样的矩阵。

【问题讨论】:

  • 实际上我确实想比较一维向量中的元素。假设向量的长度为 5,那么将进行 25 次比较,因此根据每次比较的结果生成一个 5x5 矩阵。

标签: python numpy matrix tensorflow pairwise


【解决方案1】:

我不知道 TensorFlow 是否有类似的内置函数,但 NumPy 中有一个非常简单的方法。它的工作原理是获取元素的所有乘积,并选择两个元素的乘积 xy 等于 x ** 2.0 的位置。

给定一个向量

v = np.array((1, 2, 3, 4, 1)).reshape(-1, 1) # shape == (5, 1)

您可以通过以下方式构建您想要的“相似度”矩阵:

sim = np.where(v.dot(v.T) == np.square(v), 1, -1)

sim 看起来像这样:

array([[ 1, -1, -1, -1,  1],
       [-1,  1, -1, -1, -1],
       [-1, -1,  1, -1, -1],
       [-1, -1, -1,  1, -1],
       [ 1, -1, -1, -1,  1]])

【讨论】:

  • 谢谢你的回答,我想过生成一个np矩阵,然后转换成tensorflow张量。但显然,我不知道如何将 np 链接到 tf 图。你知道它是如何工作的吗?
  • 我不知道,但看起来您可以直接在 TensorFlow 中使用这种方法,而无需将那里存储的任何数据转换为 ndarray 并返回。查看tf.where(),它可能会为您指明正确的方向。也许从你拥有的向量构造一个tf.constant,然后执行我描述的操作,替换tf.where()tf.square()tf.matmul()
  • 你确定 tf.where() 和 np.where 做同样的事情吗?
  • 它似乎做了同样的操作,但它可能不允许标量用于第二个和第三个参数。它们可能需要是与第一个参数具有相同等级的张量。我从来没用过。
【解决方案2】:

想法

要计算成对运算,您可以执行以下技巧:将向量扩展为两个二维向量:[n, 1][1, n],并将运算应用于它们。由于广播,它将生成[n, n] 矩阵,其中填充了向量内所有对的运算结果。

在您的情况下,操作是比较,但它可以是任何二进制操作。

张量流

为了说明,这里有两个单行。第一个产生布尔成对矩阵,第二个 - -11 的矩阵(你问的)。

import tensorflow as tf

tf.InteractiveSession()
v = tf.constant([1, 2, 3, 4, 1])

x = tf.equal(v[:, tf.newaxis], v[tf.newaxis, :])
print(x.eval())

x = 1 - 2 * tf.cast(x, tf.float32)
print(x.eval())

结果:

[[ True False False False  True]
 [False  True False False False]
 [False False  True False False]
 [False False False  True False]
 [ True False False False  True]]
[[ 1 -1 -1 -1  1]
 [-1  1 -1 -1 -1]
 [-1 -1  1 -1 -1]
 [-1 -1 -1  1 -1]
 [ 1 -1 -1 -1  1]]

麻木

在 numpy 中使用np.where 更简单:

import numpy as np

v = np.array([1, 2, 3, 4, 1])

x = v[:, np.newaxis] == v[np.newaxis, :]
print(x)

x = np.where(x, 1, -1)
print(x)

输出是一样的:

[[ True False False False  True]
 [False  True False False False]
 [False False  True False False]
 [False False False  True False]
 [ True False False False  True]]
[[ 1 -1 -1 -1  1]
 [-1  1 -1 -1 -1]
 [-1 -1  1 -1 -1]
 [-1 -1 -1  1 -1]
 [ 1 -1 -1 -1  1]]

【讨论】:

    【解决方案3】:

    这是一个简单的方法:

    In [123]: x = tf.placeholder(tf.float32, shape=(1, 5))
    
    In [124]: z = tf.equal(tf.matmul(tf.transpose(x), x), tf.square(x))
    
    In [125]: y = 2 * tf.cast(z, tf.int32) - 1
    
    In [126]: sess = tf.Session()
    
    In [127]: sess.run(y, feed_dict={x: np.array([1, 2, 3, 4, 1])[None, :]})
    Out[127]: 
    array([[ 1, -1, -1, -1,  1],
           [-1,  1, -1, -1, -1],
           [-1, -1,  1, -1, -1],
           [-1, -1, -1,  1, -1],
           [ 1, -1, -1, -1,  1]], dtype=int32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-22
      • 2014-04-22
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多