若 w 為 m*1 的矩陣,x 為 m*n 的矩陣,那么通過點乘結果就會得到一個 m*n 的矩陣。
若 w 為 m*n 的矩陣,x 為 m*n 的矩陣,那么通過點乘結果就會得到一個 m*n 的矩陣。
w的列數(shù)只能為 1 或 與x的列數(shù)相等(即n),w的行數(shù)與x的行數(shù)相等 才能進行乘法運算。
若 w 為 m*p 的矩陣,x 為 p*n 的矩陣,那么通過矩陣相乘結果就會得到一個 m*n 的矩陣。
只有 w 的列數(shù) == x的行數(shù) 時,才能進行乘法運算
1)點乘
import numpy as np w = np.array([[0.4], [1.2]]) x = np.array([range(1,6), range(5,10)]) print w print x print w*x
運行結果如下圖:
2)矩陣乘
import numpy as np w = np.array([[0.4, 1.2]]) x = np.array([range(1,6), range(5,10)]) print w print x print np.dot(w,x)
運行結果如下:
1)點乘
import tensorflow as tf w = tf.Variable([[0.4], [1.2]], dtype=tf.float32) # w.shape: [2, 1] x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5] y = w * x # 等同于 y = tf.multiply(w, x) y.shape: [2, 5] sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print sess.run(w) print sess.run(x) print sess.run(y)
運行結果如下:
2)矩陣乘
# coding:utf-8 import tensorflow as tf w = tf.Variable([[0.4, 1.2]], dtype=tf.float32) # w.shape: [1, 2] x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5] y = tf.matmul(w, x) # y.shape: [1, 5] sess = tf.Session() init = tf.global_variables_initializer() sess.run(init) print sess.run(w) print sess.run(x) print sess.run(y)
運行結果如下:
到此這篇關于numpy和tensorflow中的各種乘法(點乘和矩陣乘)的文章就介紹到這了,更多相關numpy和tensorflow 乘法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
標簽:渭南 綿陽 興安盟 內(nèi)江 黔東 亳州 廊坊 拉薩
巨人網(wǎng)絡通訊聲明:本文標題《numpy和tensorflow中的各種乘法(點乘和矩陣乘)》,本文關鍵詞 numpy,和,tensorflow,中的,各種,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。