Numpy和Tensorflow數據形式對比、點乘和矩陣乘對比

numpy在生成數組的時候,不知道你有沒有疑惑,它默認生成的是行向量還是列向量呢?同理,tensorflow呢,它默認生成的數據的又是行向量還是列向量呢,如果對這個比較模糊的話,我自己喜歡鑽進死衚衕,特別是在矩陣數據運算的過程中,下面我們通過實驗來證明。


1、Numpy默認維度

這部分內容參考:np.newaxis與np.shape的一些細節

Numpy默認是行向量

import numpy as np 
a=np.array([1,2,3]) 
print(a,a.shape)

結果:

[1 2 3] (3,)

2、Tensorflow中默認的維度

默認爲行向量

import tensorflow as tf
import numpy as np
weights = np.array([[1,2],[2,4]])
one = tf.Variable([3,4])     -----------------------------------注意下,和numpy作對比
result = weights*one
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(one),sess.run(tf.shape(one))) -------------------注意shape的含義
print(weights)
print(sess.run(result))

結果:
[3 4]
shape: [2] ------------------------------------------------------------------
weights 
[[1 2]
 [2 4]]
[[ 3  8]
 [ 6 16]]             ------------------------------------------注意點乘的結果

Process finished with exit code 0

scal = tf.Variable(1)
one = tf.Variable([3,4],tf.int32)
two = tf.Variable([[1],[2]],tf.int32)
print(scal.shape)
print(one.shape)
print(two.shape)

結果:
()
(2,)   ---------------------------------------------------------------
(2, 1)

Process finished with exit code 0

需要注意的是shape(a)與tf.shape(a)的結果中,表示的方法不一樣,一個是括號,一個是中括號


列矩陣的形式

import tensorflow as tf
import numpy as np
weights = np.array([[1,2],[2,4]])
one = tf.Variable([[3],[4]])-----------------------------------注意寫法
result = weights*one
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
print(sess.run(one))
print("shape:",sess.run(tf.shape(one)))------------------------shape
print("weights",weights)

結果:
[[3]
 [4]]
shape: [2 1]
weights 
[[1 2]
 [2 4]]
[[ 3  6]
 [ 8 16]]            ---------------------------------------------注意點乘的結果

Process finished with exit code 0

 

3、點乘和矩陣乘的區別

1)點乘(即“ * ”) ---- 各個矩陣對應元素做乘法

若 w 爲 m*1 的矩陣,x 爲 m*n 的矩陣,那麼通過點乘結果就會得到一個 m*n 的矩陣。

若 w 爲 m*n 的矩陣,x 爲 m*n 的矩陣,那麼通過點乘結果就會得到一個 m*n 的矩陣。

 

注意:點乘至少要滿足w的行向量等於x的行向量或者w的列向量等於x的列向量,或者兩者都滿足。

2)矩陣乘 ---- 按照矩陣乘法規則做運算

若 w 爲 m*p 的矩陣,x 爲 p*n 的矩陣,那麼通過矩陣相乘結果就會得到一個 m*n 的矩陣。

只有 w 的列數 == x的行數 時,才能進行乘法運算

在一個例子:

weights_input_to_hidden = np.random.normal(0.0, 4**-0.5, (4, 1))   #shape(4,1)
weights_hidden_to_output = np.random.normal(0.0, 4**-0.5, (1, 4))  #shape(1,4)
result = weights_input_to_hidden*weights_hidden_to_output
print(result)

 

[[ 0.09427089 -0.02168833 -0.07776069  0.01003196]
 [-0.41501758  0.09548056  0.34233317 -0.04416463]
 [-0.01939067  0.00446109  0.01599467 -0.00206348]
 [-0.7228616   0.16630436  0.5962627  -0.07692424]]

 

總結:從上面可以看出,*是既可以做點乘的運算,也可以做矩陣乘法的運算,相對來說比較靈活, 但用*作爲運算符的時候,自己千萬要注意數據的維度對比,要知道這樣運算的結果是在做點乘還是矩陣乘法。

3.1、numpy

1)點乘

 

1 import numpy as np
2 
3 w = np.array([[0.4], [1.2]])
4 x = np.array([range(1,6), range(5,10)])
5 
6 print w
7 print x
8 print w*x

運行結果如下圖:

 2)矩陣乘

1 import numpy as np
2 
3 w = np.array([[0.4, 1.2]])
4 x = np.array([range(1,6), range(5,10)])
5 
6 print w
7 print x
8 print np.dot(w,x)

運行結果如下:

3.2、tensorflow 

1)點乘

 1 import tensorflow as tf
 2 
 3 w = tf.Variable([[0.4], [1.2]], dtype=tf.float32) # w.shape: [2, 1]
 4 x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5]
 5 y = w * x       等同於 y = tf.multiply(w, x)   y.shape: [2, 5]
 6 
 7 sess = tf.Session()
 8 init = tf.global_variables_initializer()
 9 sess.run(init)
10 
11 print sess.run(w)
12 print sess.run(x)
13 print sess.run(y)

 

2)矩陣乘
 1 # coding:utf-8
 2 import tensorflow as tf
 3 
 4 w = tf.Variable([[0.4, 1.2]], dtype=tf.float32) # w.shape: [1, 2]
 5 x = tf.Variable([range(1,6), range(5,10)], dtype=tf.float32) # x.shape: [2, 5]
 6 y = tf.matmul(w, x) # y.shape: [1, 5]
 7 
 8 sess = tf.Session()
 9 init = tf.global_variables_initializer()
10 sess.run(init)
11 
12 print sess.run(w)
13 print sess.run(x)
14 print sess.run(y)

 

點乘和矩陣乘的區別這部分內容轉自:點乘和矩陣乘的區別

 

參考資料:

https://blog.csdn.net/wintersshi/article/details/80489258

http://www.cnblogs.com/liuq/p/9330134.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章