Tensorflow 入门回顾——Tensor

一、张量的基本操作

a、常量转换成张量

x = tf.convert_to_tensor([[[[1,2],[1,2]]],[[[3,4],[3,4]]],[[[5,6],[5,6]]]])
tf.Tensor(
[[[[1 2]
   [1 2]]]
 [[[3 4]
   [3 4]]]
 [[[5 6]
   [5 6]]]], shape=(3, 1, 2, 2), dtype=int32)

b、维度变换

a = tf.range(96)
a = tf.reshape(a,[2,4,1,12]) #维度变换
tf.Tensor(
[[[[ 0  1  2  3  4  5  6  7  8  9 10 11]]
  [[12 13 14 15 16 17 18 19 20 21 22 23]]
  [[24 25 26 27 28 29 30 31 32 33 34 35]]
  [[36 37 38 39 40 41 42 43 44 45 46 47]]]
 [[[48 49 50 51 52 53 54 55 56 57 58 59]]
  [[60 61 62 63 64 65 66 67 68 69 70 71]]
  [[72 73 74 75 76 77 78 79 80 81 82 83]]
  [[84 85 86 87 88 89 90 91 92 93 94 95]]]], shape=(2, 4, 1, 12), dtype=int32)

c、维度交换

a = tf.transpose(a,[0,3,1,2]) #维度交换
print(a)
tf.Tensor(
[[[[ 0]
   [12]
   [24]
   [36]]
...
  [[59]
   [71]
   [83]
   [95]]]], shape=(2, 12, 4, 1), dtype=int32)

d、新增维度

a = tf.expand_dims(a,axis=2) #新增维度
print(a)
tf.Tensor(
[[[[[ 0]
    [12]
    [24]
    [36]]]
...
  [[[59]
    [71]
    [83]
    [95]]]]], shape=(2, 12, 1, 4, 1), dtype=int32)

e、删除维度

a = tf.squeeze(a,axis=4) #删除维度,只能删除dim=1的维度号
print("删除维度",a)
tf.Tensor(
[[[[ 0 12 24 36]]


  [[ 1 13 25 37]]


  [[ 2 14 26 38]]


  [[ 3 15 27 39]]


  [[ 4 16 28 40]]


  [[ 5 17 29 41]]


  [[ 6 18 30 42]]


  [[ 7 19 31 43]]


  [[ 8 20 32 44]]


  [[ 9 21 33 45]]


  [[10 22 34 46]]


  [[11 23 35 47]]]




 [[[48 60 72 84]]


  [[49 61 73 85]]


  [[50 62 74 86]]


  [[51 63 75 87]]


  [[52 64 76 88]]


  [[53 65 77 89]]


  [[54 66 78 90]]


  [[55 67 79 91]]


  [[56 68 80 92]]


  [[57 69 81 93]]


  [[58 70 82 94]]


  [[59 71 83 95]]]], shape=(2, 12, 1, 4), dtype=int32)

f、复制张量

x = tf.tile(x,multiples=[1,1,2,2]) #对应轴的位置数字为1表示不复制,2表示复制一倍
print("复制张量",x)
tf.Tensor(
[[[[1 2 1 2]
   [1 2 1 2]
   [1 2 1 2]
   [1 2 1 2]]]




 [[[3 4 3 4]
   [3 4 3 4]
   [3 4 3 4]
   [3 4 3 4]]]




 [[[5 6 5 6]
   [5 6 5 6]
   [5 6 5 6]
   [5 6 5 6]]]], shape=(3, 1, 4, 4), dtype=int32)

二、张量的进阶操作

a、张量的叠加

a = tf.random.normal([38,2])
b = tf.random.normal([38,2])
c = tf.stack([a,b],axis=0) #tensor的堆叠,会增加一个新的维度
print(c)
张量叠加 tf.Tensor(
[[[ 2.0166867   0.34882063]
  [ 0.6328258   0.33560666]]


 [[ 1.9970915   0.26975086]
  [ 2.0534565  -0.05931673]]], shape=(2, 2, 2), dtype=float32)

2、张量的连接

d = tf.concat([a,b],axis=0) #tensro的连接,会改变对应axis的维度
print("张量连接",d)
张量连接 tf.Tensor(
[[ 2.0166867   0.34882063]
 [ 0.6328258   0.33560666]
 [ 1.9970915   0.26975086]
 [ 2.0534565  -0.05931673]], shape=(4, 2), dtype=float32)

3、张量分割

x = tf.random.normal([10,5,5])
x = tf.split(x,num_or_size_splits=10,axis=0) #tensor的分割,分割后还是会保留对应轴为一维
print("张量分割",x[0])
张量分割 tf.Tensor(
[[[ 0.61486477 -0.14688173  0.7133295   0.6682942   0.3265664 ]
  [ 1.1279719   0.2353544  -1.2520853  -0.36985752 -0.7572246 ]
  [-0.61306417 -1.0996789  -1.4453558   0.53549516  0.09335665]
  [ 0.48308775  1.2270738   1.6141179   0.48003426 -1.7820065 ]
  [-0.8549648  -0.2284165  -0.57135314  0.24757865 -0.8732566 ]]], shape=(1, 5, 5), dtype=float32)

4、张量降维

#如果希望在某个维度上全部按照长度1的方式切割,可以使用tf.unstack(x,axis),会变成一个降维的tensor
x = tf.random.normal([10,5,5])
x = tf.unstack(x,axis=0)
print("张量降维",x[0])
张量降维 tf.Tensor(
[[ 0.22386584 -0.70573205 -0.8329945  -1.1417854  -0.07950575]
 [-1.0521472   0.75167847 -0.6712898  -0.43965402 -0.12728883]
 [ 0.28462592  1.2429935  -0.9316726   0.38548484  1.3959352 ]
 [ 1.3602802  -1.5601577   0.02255491 -0.92195195 -0.4613802 ]
 [-0.08492377 -1.8158523  -0.43123505  0.6094767  -0.46144685]], shape=(5, 5), dtype=float32)

5、向量的范数

#向量的范数,L1范数表示向量x的所有元素绝对值之和
#L2范数表示向量X所有元素的平方和,再开更号
#L3范数表示向量x所有元素绝对值的最大值
x = tf.ones([2,2])
y = tf.random.normal([2,2])
z = tf.ones([2,3,3])
price = tf.random.uniform([4,25,6],maxval=100,dtype=tf.int32)


print("L1范数",tf.norm(x,ord=1)) #L1范数
print("L2范数",tf.norm(x,ord=2)) #L2范数
print("L3范数",tf.norm(x,ord=np.inf)) #L3范数
print("统计该维度上的最大值",tf.reduce_max(y,axis=1)) #统计该维度上的最大值
print("统计该维度上的最小值",tf.reduce_min(y, axis=1))  # 统计该维度上的最小值
print("统计该维度上的均值",tf.reduce_mean(y, axis=1))  # 统计该维度上的均值
print("统计该维度上的和",tf.reduce_sum(y, axis=1))  # 统计该维度上的和
print("没有维度表示在全局上进行统计",tf.reduce_sum(y)) #没有维度表示在全局上进行统计
print("找出最大值的索引",tf.argmax(y,axis=1))#找出最大值的索引
print("在x上方填充1行,在下方填充2行,在左边填充3列,在右边填充4列",tf.pad(x,[[1,2],[3,4]]))#在x上方填充1行,在下方填充2行,在左边填充3列,在右边填充4列
print(tf.pad(x,[[1,2],[2,1]]))
L1范数 tf.Tensor(4.0, shape=(), dtype=float32)
L2范数 tf.Tensor(2.0, shape=(), dtype=float32)
L3范数 tf.Tensor(1.0, shape=(), dtype=float32)
统计该维度上的最大值 tf.Tensor([-0.2239719  1.2946209], shape=(2,), dtype=float32)
统计该维度上的最小值 tf.Tensor([-1.6314374  -0.24824949], shape=(2,), dtype=float32)
统计该维度上的均值 tf.Tensor([-0.9277047  0.5231857], shape=(2,), dtype=float32)
统计该维度上的和 tf.Tensor([-1.8554094  1.0463713], shape=(2,), dtype=float32)
没有维度表示在全局上进行统计 tf.Tensor(-0.80903804, shape=(), dtype=float32)
找出最大值的索引 tf.Tensor([1 1], shape=(2,), dtype=int64)
在x上方填充1行,在下方填充2行,在左边填充3列,在右边填充4列 tf.Tensor(
[[0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 1. 1. 0. 0. 0. 0.]
 [0. 0. 0. 1. 1. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0. 0. 0. 0.]], shape=(5, 9), dtype=float32)
tf.Tensor(
[[0. 0. 0. 0. 0.]
 [0. 0. 1. 1. 0.]
 [0. 0. 1. 1. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]], shape=(5, 5), dtype=float32)

6、张量数据收集

print("数据限制幅度",tf.clip_by_value(y,0,0.5)) #数据限制幅度
print("axis=0表示第一个维度班级,收集0-1号班级的成绩",tf.gather(price, [0, 1], axis=0))  # axis=0表示第一个维度班级,收集0-1号班级的成绩
print("根据多维度坐标收集数据",tf.gather_nd(price, [[1, 2], [2, 2], [3, 3]]))  # 根据多维度坐标收集数据
print("用掩码的方式收集数据",tf.boolean_mask(price, [False, True, False, True], axis=0))  # 用掩码的方式收集数据
数据限制幅度 tf.Tensor(
[[0.  0. ]
 [0.  0.5]], shape=(2, 2), dtype=float32)
axis=0表示第一个维度班级,收集0-1号班级的成绩 tf.Tensor(
[[[98 75 96 48  6 99]
  [92 58 48 41 78 38]
  [18 16 86  9 40 28]
  [44 99 31 84 65 97]
  [11 72 45 70 82 99]
  [43 66 94 70  2 73]
  [34 31 11  4 43 40]
  [30 42 59 39 56 38]
  [74 63 79 52 16 68]
  [50 71 70 14 86 78]
  [23 83 88  4 83 85]
  [22 96 30 59 97 97]
  [81 16 32 43 47 13]
  [45 99  8 65  3 11]
  [14 50 86 16 81 15]
  [45 22 60 64 95 15]
  [10 57 75 49  2 16]
  [37 21 80  0 81 26]
  [13 49 60 81 80  4]
  [97 59 77 39 23 16]
  [ 5 89 62 48  7 96]
  [ 6 61 86 57  3 11]
  [49 40 90 46 84 28]
  [87 50 16 14 44  1]
  [ 1 47  4  6 85 24]]


 [[46 79 36 93 53 72]
  [56 63 48  7 11 33]
  [37 49 41  7  9 57]
  [75 27 92 70 84  1]
  [43 75 93 18 81  9]
  [32 49 78 51 65 10]
  [57 29 97 90  7 43]
  [80 84 77  0 35  8]
  [ 8 47 41 51 24 36]
  [85 98 32 65 79 97]
  [12 91  3  2 29 78]
  [76 97 25  4 77 38]
  [48 54 12 13 68 24]
  [27  3 33 34 81 73]
  [96 85 15 68 51 22]
  [23 35  4 69 20  4]
  [94 89 31 15 74 67]
  [71 72 31 99 91 99]
  [66 25 14 35 28 71]
  [56 28 11 85 87 31]
  [17 19 20 59 47 92]
  [75 42  9 25 71 88]
  [85 37 45  4 97  4]
  [70 47 14 53 83 86]
  [32 96  6 65  6 87]]], shape=(2, 25, 6), dtype=int32)
根据多维度坐标收集数据 tf.Tensor(
[[37 49 41  7  9 57]
 [55  8 69 68 74 56]
 [61 12 40 94 35  9]], shape=(3, 6), dtype=int32)
用掩码的方式收集数据 tf.Tensor(
[[[46 79 36 93 53 72]
  [56 63 48  7 11 33]
  [37 49 41  7  9 57]
  [75 27 92 70 84  1]
  [43 75 93 18 81  9]
  [32 49 78 51 65 10]
  [57 29 97 90  7 43]
  [80 84 77  0 35  8]
  [ 8 47 41 51 24 36]
  [85 98 32 65 79 97]
  [12 91  3  2 29 78]
  [76 97 25  4 77 38]
  [48 54 12 13 68 24]
  [27  3 33 34 81 73]
  [96 85 15 68 51 22]
  [23 35  4 69 20  4]
  [94 89 31 15 74 67]
  [71 72 31 99 91 99]
  [66 25 14 35 28 71]
  [56 28 11 85 87 31]
  [17 19 20 59 47 92]
  [75 42  9 25 71 88]
  [85 37 45  4 97  4]
  [70 47 14 53 83 86]
  [32 96  6 65  6 87]]


 [[67 64 74 99 35 71]
  [ 6 43 64 60 64 43]
  [52 37  4 19 76 89]
  [61 12 40 94 35  9]
  [63 46 67 76  0 59]
  [15 15 21 52 91 30]
  [53 80 78 17 69 27]
  [27 54 85 93 69 69]
  [ 1  5 59 36 17 69]
  [ 0 28 37 79 33 17]
  [60 17 55 20 23 50]
  [56  3 26 84 78 67]
  [63 93 64 12 56 98]
  [ 2 55 52 99 72 98]
  [35 49 48 83 41 82]
  [74 60  3  4 19 66]
  [ 2 73 92 35 76 21]
  [76 70 75 11  9 56]
  [85 24  7 11 40 37]
  [92 74 48 28 75 93]
  [65 28 18 23 88 32]
  [71 84 12 14 31 65]
  [39 64 76 55 54 12]
  [65 57 37 51 46 52]
  [92 78  2 45 74 38]]], shape=(2, 25, 6), dtype=int32)

7、根据掩码收集张量数据

where_a = tf.zeros([3,3])
where_b = tf.ones([3,3])
condition_flag = tf.constant([[True,False,True],[False,True,False],[True,False,True]])
print("根据conditon_flag条件进行采样,True从a中取值,False从b中取值",tf.where(condition_flag, where_a, where_b))  # 根据conditon_flag条件进行采样,True从a中取值,False从b中取值
根据conditon_flag条件进行采样,True从a中取值,False从b中取值 tf.Tensor(
[[0. 1. 0.]
 [1. 0. 1.]
 [0. 1. 0.]], shape=(3, 3), dtype=float32)

听说关注公众号的都是大牛

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值