multiply 等同與* ,用于計算矩陣之間的element-wise 乘法,要求矩陣的形狀必須一致(或者是其中一個維度為1),否則會報錯:
import tensorflow as tf
a = tf.constant([1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12], shape=[2, 3, 2])
b = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3, 1])
c = a*b
e = tf.multiply(a, a)
with tf.Session():
print(a.eval())
print(b.eval())
print(c.eval())
print(d.eval())
print(e.eval())
>> a
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
>>b
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
>>a*b
[[[ 1 2]
[ 6 8]
[15 18]]
[[28 32]
[45 50]
[66 72]]]
>>multiply(a, b)
[[[ 1 2]
[ 6 8]
[15 18]]
[[28 32]
[45 50]
[66 72]]]
>>multiply(a,a)
[[[ 1 4]
[ 9 16]
[ 25 36]]
[[ 49 64]
[ 81 100]
[121 144]]]
更改b的形狀:
b=tf.constant([1,2,3,4,5,6], shape= [1,3,2])
d = a* b
with tf.Session():
print(a.eval())
print(b.eval())
print(d.eval())
>>a
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
>>b
[[[1 2]
[3 4]
[5 6]]]
>>c
[[[ 1 4]
[ 9 16]
[25 36]]
[[ 7 16]
[27 40]
[55 72]]]
b=tf.constant([1,2,3,4], shape= [2,1,2])
d = a* b
with tf.Session():
print(a.eval())
print(b.eval())
print(d.eval())
>>a
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
>>b
[[[1 2]]
[[3 4]]]
>>d
[[[ 1 4]
[ 3 8]
[ 5 12]]
[[21 32]
[27 40]
[33 48]]]
matmul 是tensor的矩陣乘法, 參與運算的兩個tensor維度、數據類型必須一致,
參與運算的是最后兩維形成的矩陣,如果tensor是二維矩陣,則等同于矩陣乘法:
# 二維tensor
a = tf.constant([1,2,3,4,5,6], shape=[2,3])
b = tf.constant([1,2,3,4,5,6], shape=[3,2])
c = tf.matmul(a,b)
with tf.Session():
print(a.eval())
print(b.eval())
print(c.eval())
>>a
[[1 2 3]
[4 5 6]]
>>b
[[1 2]
[3 4]
[5 6]]
>>c
[[22 28]
[49 64]]
# 三維tensor
a = tf.constant([i for i in range(1, 25)], shape=[2, 3, 4])
b = tf.constant([i for i in range(1, 25)], shape=[2, 4, 3])
c = tf.matmul(a, b)
>>a
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
>>b
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]]]
>>c
[[[ 70 80 90]
[ 158 184 210]
[ 246 288 330]]
[[1030 1088 1146]
[1310 1384 1458]
[1590 1680 1770]]]
# c形狀[2,3,3],因為a的后兩維是[3,4],b的后兩維是[4,3],乘積為[3,3]
# 四維tensor
a = tf.constant([i for i in range(1, 25)], shape=[2, 2,2,3])
b = tf.constant([i for i in range(1, 25)], shape=[2, 2,3,2])
c = tf.matmul(a,b)
>>a
[[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
[[[13 14 15]
[16 17 18]]
[[19 20 21]
[22 23 24]]]]
>>b
[[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]]
[[[13 14]
[15 16]
[17 18]]
[[19 20]
[21 22]
[23 24]]]]
>>c
[[[[ 22 28]
[ 49 64]]
[[ 220 244]
[ 301 334]]]
[[[ 634 676]
[ 769 820]]
[[1264 1324]
[1453 1522]]]
# c的形狀 [2,2,2,2]
tensordot:矩陣乘法運算,參與運算的兩個tensor的維度可以不一樣:
a?和?b?沿特定軸的張量收縮.
Tensordot(也稱為張量收縮)對從?a?和?b?所指定的索引?a_axes?和?b_axes?的元素的乘積進行求和.列表?a_axes?和?b_axes?指定沿其收縮張量的那些軸對.對于所有?range(0,?len(a_axes))?中的?i,a?的軸?a_axes[i]?必須與?b?的軸?b_axes[i]?具有相同的維度.列表?a_axes?和?b_axes?必須具有相同的長度,并由唯一的整數組成,用于為每個張量指定有效的坐標軸.
該操作對應于?numpy.tensordot(a,?b,?axes).
示例1:當?a?和?b?是矩陣(2階)時,axes?=?1?相當于矩陣乘法.
示例2:當?a?和?b?是矩陣(2階)時,axes?=?[[1],?[0]]?相當于矩陣乘法.
函數參數:
?a:float32?或?float64?類型的?Tensor.
?b:Tensor,與?a?具有相同的類型.
?axes:可以是標量?N,也可以是具有形狀?[2,k]?的?int32?Tensor?的列表.如果軸是標量,則按順序對?a?的最后?N?個軸和?b?的前?N?個軸進行求和.如果軸是一個列表或?Tensor,則分別對于軸?a?和?b,在第一和第二行包含該組唯一整數指定沿該收縮被計算.a?和?b?的坐標軸數必須相等.
?name:操作的名稱(可選).
函數返回值:
函數返回與?a?具有相同類型的?Tensor.
可能引發的異常:
?ValueError:如果?a,b?和?axes?的形狀是不相容的.
?IndexError:如果軸上的值超過相應張量的等級.
a = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24], shape=[2,3,4])
b = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12], shape=[4,3])
c = tf.tensordot(a, b, axes=1)
d = tf.tensordot(a, b, axes=2) # 對a的后2個軸乘積進行加和[2,3X4],即a的shape變成[2,12]; # 對b的前兩個軸進行加和,b的shape變成[12]
e = tf.tensordot(a, b, axes=([1,2],[0,1]))
f = tf.tensordot(a, b, axes=([1,2],[1,0])) # 分別指定兩個軸,對tensor進行展開,a展開成[2,12],
# b展開成[12,1],軸的順序不同,展開方式不同
# 此處b展開成[1,4,7,10,2,5,8,11,3,6,9,12],上面展開成[1,2,3,4,5,6,7,8,9,10,11,12]
g = tf.tensordot(a, b, axes=([1],[1])) #指定任何軸,指定的軸形狀一致
with tf.Session():
print(a.eval())
print(b.eval())
print(c.eval())
>>a
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
>>b
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
>>c
[[[ 70 80 90]
[158 184 210]
[246 288 330]]
[[334 392 450]
[422 496 570]
[510 600 690]]]
# c的形狀 [2,3,3] [2,3,4] * [4,3]
>>d
[ 650 1586]
>>e
[ 650 1586]
>>f
[ 584 1520]
>>g
[[[ 38 83 128 173]
[ 44 98 152 206]
[ 50 113 176 239]
[ 56 128 200 272]]
[[110 263 416 569]
[116 278 440 602]
[122 293 464 635]
[128 308 488 668]]]
a = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24], shape=[2,3,4])
b = tf.constant([1,2,3,4,5,6,7,8,9,10,11,12], shape=[4,3])
c = tf.constant([1,2,3,4], shape=[4,1])
d = tf.tensordot(a, b, axes=1)
e = tf.tensordot(a, c, axes=1)
>>a
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
>>b
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
>>c
[[1]
[2]
[3]
[4]]
>>d
[[[ 70 80 90]
[158 184 210]
[246 288 330]]
[[334 392 450]
[422 496 570]
[510 600 690]]]
# d的形狀[2,3,3] [2,3,4] * [4, 3] = [2,3,3]
>>e
[[[ 30]
[ 70]
[110]]
[[150]
[190]
[230]]]
# e的形狀 [2,3,1] [2,3,4] * [4,1] = [2,3,1]
a = tf.constant([i for i in range(1, 25)], shape=[2,3,4])
b = tf.constant([i for i in range(1, 25)], shape=[2,2,6])
c = tf.tensordot(a,b,([1,2],[1,2]))
>>a
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
>>b
[[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[[13 14 15 16 17 18]
[19 20 21 22 23 24]]]
>>c
[[ 650 1586]
[1586 4250]]