Python 张量流中两个向量的点积
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40670370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dot product of two vectors in tensorflow
提问by user6952886
I was wondering if there is an easy way to calculate the dot product of two vectors (i.e. 1-d tensors) and return a scalar value in tensorflow.
我想知道是否有一种简单的方法来计算两个向量(即一维张量)的点积并在张量流中返回一个标量值。
Given two vectors X=(x1,...,xn) and Y=(y1,...,yn), the dot product is dot(X,Y) = x1 * y1 + ... + xn * yn
给定两个向量 X=(x1,...,xn) 和 Y=(y1,...,yn),点积为 dot(X,Y) = x1 * y1 + ... + xn * yn
I know that it is possible to achieve this by first broadcasting the vectors X and Y to a 2-d tensor and then using tf.matmul. However, the result is a matrix, and I am after a scalar.
我知道可以通过首先将向量 X 和 Y 广播到二维张量然后使用 tf.matmul 来实现这一点。然而,结果是一个矩阵,我在追求一个标量。
Is there an operator like tf.matmul that is specific to vectors?
是否有像 tf.matmul 这样特定于向量的运算符?
回答by Ishant Mrinal
One of the easiest way to calculate dot product between two tensors (vector is 1D tensor) is using tf.tensordot
计算两个张量(向量是一维张量)之间的点积的最简单方法之一是使用 tf.tensordot
a = tf.placeholder(tf.float32, shape=(5))
b = tf.placeholder(tf.float32, shape=(5))
dot_a_b = tf.tensordot(a, b, 1)
with tf.Session() as sess:
print(dot_a_b.eval(feed_dict={a: [1, 2, 3, 4, 5], b: [6, 7, 8, 9, 10]}))
# results: 130.0
回答by yuefengz
In addition to tf.reduce_sum(tf.multiply(x, y))
, you can also do tf.matmul(x, tf.reshape(y, [-1, 1]))
.
除了tf.reduce_sum(tf.multiply(x, y))
,你还可以做到tf.matmul(x, tf.reshape(y, [-1, 1]))
。
回答by aarbelle
you can use tf.matmul and tf.transpose
你可以使用 tf.matmul 和 tf.transpose
tf.matmul(x,tf.transpose(y))
or
或者
tf.matmul(tf.transpose(x),y)
depending on the dimensions of x and y
取决于 x 和 y 的维度
回答by normanyu
import tensorflow as tf
x = tf.Variable([1, -2, 3], tf.float32, name='x')
y = tf.Variable([-1, 2, -3], tf.float32, name='y')
dot_product = tf.reduce_sum(tf.multiply(x, y))
sess = tf.InteractiveSession()
init_op = tf.global_variables_initializer()
sess.run(init_op)
dot_product.eval()
Out[46]: -14
Here, x and y are both vectors. We can do element wise product and then use tf.reduce_sum to sum the elements of the resulting vector. This solution is easy to read and does not require reshaping.
这里,x 和 y 都是向量。我们可以按元素进行乘积,然后使用 tf.reduce_sum 对结果向量的元素求和。此解决方案易于阅读,不需要重新整形。
Interestingly, it does not seem like there is a built in dot product operator in the docs.
有趣的是,文档中似乎没有内置点积运算符。
Note that you can easily check intermediate steps:
请注意,您可以轻松检查中间步骤:
In [48]: tf.multiply(x, y).eval()
Out[48]: array([-1, -4, -9], dtype=int32)
回答by David Wong
You can do tf.mul(x,y), followed by tf.reduce_sum()
你可以做 tf.mul(x,y),然后是 tf.reduce_sum()
回答by phipsgabler
In newer versions (I think since 0.12), you should be able to do
在较新的版本中(我认为从 0.12 开始),你应该能够做到
tf.einsum('i,i->', x, y)
(Before that, the reduction to a scalar seemed not to be allowed/possible.)
(在此之前,减少到标量似乎是不允许/不可能的。)
回答by Charlie Parker
Maybe with the new docs you can just set the transpose option to true for either the first argument of the dot product or the second argument:
也许使用新文档,您可以将点积的第一个参数或第二个参数的转置选项设置为 true:
tf.matmul(a, b, transpose_a=False, transpose_b=False, adjoint_a=False, adjoint_b=False, a_is_sparse=False, b_is_sparse=False, name=None)
leading:
领导:
tf.matmul(a, b, transpose_a=True, transpose_b=False)
tf.matmul(a, b, transpose_a=False, transpose_b=True)
回答by u5700346
Just use * and reduce_sum
只需使用 * 和 reduce_sum
ab = tf.reduce_sum(a*b)
Take a simple example as follows:
举一个简单的例子,如下所示:
import tensorflow as tf
a = tf.constant([1,2,3])
b = tf.constant([2,3,4])
print(a.get_shape())
print(b.get_shape())
c = a*b
ab = tf.reduce_sum(c)
with tf.Session() as sess:
print(c.eval())
print(ab.eval())
# output
# (3,)
# (3,)
# [2 6 12]
# 20
回答by FRS
Let us assume that you have two column vectors
让我们假设您有两个列向量
u = tf.constant([[2.], [3.]])
v = tf.constant([[5.], [7.]])
If you want a 1x1 matrix you can use
如果你想要一个 1x1 矩阵,你可以使用
tf.einsum('ij,ik->jk',x,y)
If you are interested in a scalar you can use
如果您对标量感兴趣,可以使用
tf.einsum('ij,ik->',x,y)