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的行数 时,才能进行乘法运算 |
3.1、numpy1)点乘 |
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)
点乘和矩阵乘的区别这部分内容转自:点乘和矩阵乘的区别
参考资料: