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)]
發佈了35 篇原創文章 · 獲贊 137 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章