numpy, Tensorflow一个矩阵与多维矩阵相乘(不同维度的Tensor相乘)
张量相乘在深度学习中经常遇到,这里我们简单介绍一下numpy和tensorflow中的张量相乘。
1、对于维数一样的张量相乘。A.shape =(b,m,n), B.shape = (b,n,k).
numpy.matmul(A,B) 结果shape为(b,m,k)
这里要求第一维度相同。后两个维度能满足矩阵相乘条件。
2、但是对于维数不一样的张量如何处理呢?比如 A.shape =(m,n), B.shape = (b,n,k). C.shape=(k,l)
numpy.matmul(A,B) 结果shape为(b,m,k)
numpy.matmul(B,C) 结果shape为(b,n,l)
2D张量要和3D张量的后两个维度满足矩阵相乘条件。
下满我们看例子:
batch=2
row=4
column=5
x = np.random.rand(batch, row, column)
W0 = np.random.rand(column, 2)
y = np.matmul(x, W0)
W1 = np.random.rand(2, row)
z = np.matmul(W1, x)
print('x shape:', np.shape(x))
print('x:', x)
print('y shape:', np.shape(y))
print('y:', y)
print('z shape:', np.shape(z))
print('z:', z)
运行结果为:
x shape: (2, 4, 5)
x: [[[0.42931102 0.92586624 0.09862473 0.80523419 0.04585206]
[0.09597962 0.64380025 0.33493834 0.039011 0.6914464 ]
[0.08074233 0.13044447 0.38733431 0.40418332 0.36051089]
[0.5031046 0.98119291 0.06610466 0.87555935 0.73664137]]
[[0.90367473 0.60169644 0.31033839 0.76062646 0.32684213]
[0.29496026 0.90308702 0.72475022 0.40864873 0.41657423]
[0.16473435 0.47775601 0.40023049 0.49730946 0.29264442]
[0.49780871 0.85317193 0.57029562 0.11522794 0.91292808]]]
y shape: (2, 4, 2)
y: [[[0.88902547 0.82836952]
[0.778818 0.53672641]
[0.57048392 0.41105902]
[1.27525349 0.93693198]]
[[1.36887652 1.28064364]
[1.18377756 1.05706932]
[0.74357789 0.61173377]
[1.39058822 1.12161148]]]
z shape: (2, 2, 5)
z: [[[0.39425868 1.20143725 0.60319759 0.77661392 0.90066186]
[0.44429955 0.91445949 0.48437548 1.02690473 0.8865076 ]]
[[0.88299781 1.47217584 1.09290061 1.10367561 0.77635204]
[0.61850149 1.17738376 0.87149811 0.72496302 0.92217606]]]
TensorFlow的 matmul 已经支持了batch,对于高维向量的相乘(比如两个三维矩阵),tensorflow把前面的维度当成是batch,对最后两维进行普通的矩阵乘法。也就是说,最后两维之前的维度,都需要相同。比如 A.shape=(a, b, n, m),B.shape=(a, b, m, k),tf.matmul(A,B) 的结果 shape=(a,b,n,k)
有时候需要一个矩阵与多个矩阵相乘,也就是一个 2D Tensor 与一个 3D Tensor 相乘,比如 A.shape=(m, n)
,B.shape=(k, n, p)
,希望计算 A*B 得到一个 C.shape=(k, m, p)
的 Tensor.
对于2D张量乘以3D张量,tensorflow支持自动适应张量相乘操作,但是要求2D张量的维度和3D张量后两维满足矩阵相乘条件。
下满我们看例子:
batch1 = 2
row1 = 4
column1 = 5
TX = tf.ones(shape=[batch1, row1, column1], dtype=tf.float32) # TX (2,4,5)
W2 = tf.random.normal(shape=[column1, 3]) # W2 (5,3)
Ty = tf.matmul(TX, W2) # Ty (2,4,3)
W3 = tf.random.normal(shape=[3, row1]) # W3 (3,4)
Tz = tf.matmul(W3, TX) # Tz (2,3,5)
with tf.Session() as sess:
ty = sess.run(Ty)
print('ty shape', np.shape(ty))
print('ty:', ty)
tz = sess.run(Tz)
print('tz shape', np.shape(tz))
print('tz:', tz)
运行结果为:
ty shape (2, 4, 3)
ty: [[[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]]
[[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]
[ 4.1668005 -0.2795292 0.6333635]]]
tz shape (2, 3, 5)
tz: [[[ 1.9986432 1.9986432 1.9986432 1.9986432 1.9986432]
[-2.1654766 -2.1654766 -2.1654766 -2.1654766 -2.1654766]
[ 1.5506022 1.5506022 1.5506022 1.5506022 1.5506022]]
[[ 1.9986432 1.9986432 1.9986432 1.9986432 1.9986432]
[-2.1654766 -2.1654766 -2.1654766 -2.1654766 -2.1654766]
[ 1.5506022 1.5506022 1.5506022 1.5506022 1.5506022]]]
更多推荐
所有评论(0)