Dot product of two vectors in tensorflow

前端 未结 9 1178
悲&欢浪女
悲&欢浪女 2021-01-01 12:36

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=(

9条回答
  •  渐次进展
    2021-01-01 12:42

    Just use * and 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
    

提交回复
热议问题