tensorflow tensor运算

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维度、数据类型必须一致,

output`[..., i, j] = sum_k (`a`[..., i, k] * `b`[..., k, j]):

参与运算的是最后两维形成的矩阵,如果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 = 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) 
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]]

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值