张量流中两个向量的点积 [英] Dot product of two vectors in tensorflow

查看:115
本文介绍了张量流中两个向量的点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种简单的方法来计算两个向量(即一维张量)的点积并返回张量流中的标量值.

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.

给定两个向量X =(x1,...,xn)和Y =(y1,...,yn),点积为 点(X,Y)= x1 * y1 + ... + xn * yn

Given two vectors X=(x1,...,xn) and Y=(y1,...,yn), the dot product is dot(X,Y) = x1 * y1 + ... + xn * yn

我知道,可以通过首先将向量X和Y广播到2-d张量,然后使用tf.matmul来实现.但是,结果是一个矩阵,我在标量之后.

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.

是否有像tf.matmul这样的运算符专用于向量?

Is there an operator like tf.matmul that is specific to vectors?

推荐答案

计算两个张量(矢量为一维张量)之间的点积的最简单方法之一是使用

One of the easiest way to calculate dot product between two tensors (vector is 1D tensor) is using 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

这篇关于张量流中两个向量的点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆