1. 程式人生 > >tf.multiply()和tf.matmul()區別

tf.multiply()和tf.matmul()區別

  1. tf.multiply是點乘,即Returns x * y element-wise,支援broadcasting
  2. tf.matmul是矩陣乘法,即Multiplies matrix a by matrix b, producing a * b.

示例:

import tensorflow as tf
import pprint
a = tf.reshape(tf.constant([1,2,3,4,5,6]), [2,3])
b = tf.reshape(tf.constant([1,2,3,4,5,6]), [3,2])
c = tf.reshape(tf.constant([1
,2,3,4,5,6]), [2,3]) x = tf.matmul(a, b) y = a * c z = tf.multiply(a,c) with tf.Session() as sess: pprint.pprint(sess.run([a,b,c, x, y,z]))

輸出:

[array([[1, 2, 3],
       [4, 5, 6]], dtype=int32),
 array([[1, 2],
       [3, 4],
       [5, 6]], dtype=int32),
 array([[1, 2, 3],
       [4, 5, 6
]], dtype=int32), array([[22, 28], [49, 64]], dtype=int32), array([[ 1, 4, 9], [16, 25, 36]], dtype=int32), array([[ 1, 4, 9], [16, 25, 36]], dtype=int32)]