1. 程式人生 > >tensorflow——乘法

tensorflow——乘法

  線性代數中,乘法是很重要的運算,具體的矩陣乘法原理可以翻教材,或看一下阮大神的這篇文章:http://www.ruanyifeng.com/blog/2015/09/matrix-multiplication.html。

  在tensorflow中,經常使用以下幾種乘法:

  1. 點乘  即常數與向量相乘或向量點乘
    sess = tf.Session()
    
    a = tf.constant([ 0.1, 0.4, 1.2], dtype=tf.float32)
    b = tf.constant([10.0, 5.0, 1.0], dtype=tf.float32)
    
    c = 2.0
    
    print
    (sess.run(a * b)) print(sess.run(c * a))

     

  2. 矩陣相乘
    sess = tf.Session()
    
    a = np.random.random((4, 3))
    b = np.random.random((3, 5))
    
    print(sess.run(tf.matmul(a, b)))

     

  3. 內積
    c = np.random.random((1, 5))
    
    print(sess.run(tf.multiply(b, c)))