12、高阶op操作

  • where   得到mask中的Ture在原张量中的位置,根据坐标系根据条件需求(a>0)有目的的选择
  • scatter_nd   根据坐标有目的性的更新
  • meshgrid  生成数据点(x,y)

1、where ,得到mask在Ture在a对应的元素的位置坐标,然后通过gather进行收集

    (1) where(Tensor),只接收一个参数,这个Tensor是一个bool型,里面的元素是Ture or False,返回Ture元素所有的坐标

a = tf.random.normal([3,3])
print(a)

mask = a>0  #生成一个bool型的张量
print(mask)
#①第一种方式
b = tf.boolean_mask(a,mask) #在a中取出mask中对应的Ture的值
print(b)
#②第二种方式,两种方式的结果一样 indices
= tf.where(mask) #得到mask中Ture在a对应的元素的位置坐标,然后通过gather进行收集 print(">>>>>indices:",indices) b = tf.gather(a,indices) # gather是进行维度收集 print(">>>>>>>>",b) b = tf.gather_nd(a,indices) # gather_nd进行元素收集 print(">>>>>>",b)

输出:

tf.Tensor(
[[ 0.48391852  1.4074458   1.0196995 ]
 [ 0.6496246  -1.8962317  -0.80803776]
 [ 1.3511435  -0.717642    0.7378417 ]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[ True  True  True]
 [ True False False]
 [ True False  True]], shape=(3, 3), dtype=bool)
tf.Tensor([0.48391852 1.4074458  1.0196995  0.6496246  1.3511435  0.7378417 ], shape=(6,), dtype=float32)
>>>>>indices: tf.Tensor(
[[0 0]
 [0 1]
 [0 2]
 [1 0]
 [2 0]
 [2 2]], shape=(6, 2), dtype=int64)
>>>>>>>> tf.Tensor(
[[[ 0.48391852  1.4074458   1.0196995 ]
  [ 0.48391852  1.4074458   1.0196995 ]]

 [[ 0.48391852  1.4074458   1.0196995 ]
  [ 0.6496246  -1.8962317  -0.80803776]]

 [[ 0.48391852  1.4074458   1.0196995 ]
  [ 1.3511435  -0.717642    0.7378417 ]]

 [[ 0.6496246  -1.8962317  -0.80803776]
  [ 0.48391852  1.4074458   1.0196995 ]]

 [[ 1.3511435  -0.717642    0.7378417 ]
  [ 0.48391852  1.4074458   1.0196995 ]]

 [[ 1.3511435  -0.717642    0.7378417 ]
  [ 1.3511435  -0.717642    0.7378417 ]]], shape=(6, 2, 3), dtype=float32)
>>>>>> tf.Tensor([0.48391852 1.4074458  1.0196995  0.6496246  1.3511435  0.7378417 ], shape=(6,), dtype=float32)

(2) where(mask,A,B),接收三个参数

  在mask中,如果是Ture的,则在A中选对应的元素,如果是False的,则在B中选对应的元素

 1 a = tf.random.normal([3,3])
 2 print(a)
 3 
 4 mask = a>0  #生成一个bool型的张量
 5 print(mask)
 6 
 7 A = tf.ones([3,3])
 8 B = tf.fill([3,3],4.0)
 9 b = tf.where(mask,A,B)  
10 print(b)

输出:

tf.Tensor(
[[ 1.5847638  -1.4374838  -0.96571   ]
 [ 0.6690132   0.69762254  1.6966511 ]
 [ 0.47084153 -0.22469759  1.0512805 ]], shape=(3, 3), dtype=float32)
tf.Tensor( [[ True False False] [ True True True] [ True False True]], shape
=(3, 3), dtype=bool)
tf.Tensor( [[
1. 4. 4.] [1. 1. 1.] [1. 4. 1.]], shape=(3, 3), dtype=float32)

2、scatter_nd

(1)tf.scatter_nd(indices,updates,shape根据indices坐标将updates的值更新到底板shape的张量中

  • indices  坐标
  • updates  更新
  • shape  形状

(2)a是一个shape为8的全0值的tensor,scatter_nd将updates中的数据按照indices中的位置坐标更新到shape为8的a张量中

   代码:

 

①一维张量
1 indices = tf.constant([[4],[3],[1],[7]])
2 updates = tf.constant([9,10,11,12])
3 shape = tf.constant([8])
4 
5 a = tf.scatter_nd(indices,updates,shape)
6 print(a) #tf.Tensor([ 0 11  0 10  9  0  0 12], shape=(8,), dtype=int32)

    ②多维张量,在第0个位置和第2个位置更新

1 indices = tf.constant([[0],[2]])
2 updates = tf.constant([[[5,5,5,5],[6,6,6,6],[7,7,7,7],[8,8,8,8]],
3                        [[5,5,5,5],[6,6,6,6],[7,7,7,7],[8,8,8,8]]])
4 shape = tf.constant([4,4,4])
5 a = tf.scatter_nd(indices,updates,shape)
6 print(a)

输出:

tf.Tensor(
[[[5 5 5 5]
  [6 6 6 6]
  [7 7 7 7]
  [8 8 8 8]]

 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]

 [[5 5 5 5]
  [6 6 6 6]
  [7 7 7 7]
  [8 8 8 8]]

 [[0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]
  [0 0 0 0]]], shape=(4, 4, 4), dtype=int32)

3、meshgrid

(1) 用于三维曲面的分割线坐标,产生格点矩阵,生成一个图片,z=z(x,y)

 

 

(2)np.linspace(-2,2,5)生成范围为-2到2的5个点

(3)meshgrid(x,y)相当于将一个坐标拆分保存到两个Tensor中

 #生成点的两种方式
1
def creatPoints1(): #两个循环 2 points = [] 3 for x in np.linspace(-2,2,5): 4 for y in np.linspace(-2,2,5): 5 points.append([x,y]) 6 return np.array(points) 7 8 def creatPoints2(): 9 x = tf.linspace(-2.,2,5) #tf要设置成浮点型的数据 10 y = tf.linspace(-2.,2,5) 11 points_x, points_y = tf.meshgrid(x,y) #相当于将一个坐标拆分保存到两个Tensor中 12 print(x) 13 print(y) 14 print(points_x) 15 print(points_y) 16 points = tf.stack([points_x,points_y],axis=2) 17 print(points) # shape=(5, 5, 2) 18 19 20 if __name__ == '__main__': 21 creatPoints2()

输出:

tf.Tensor([-2. -1.  0.  1.  2.], shape=(5,), dtype=float32)
tf.Tensor([-2. -1.  0.  1.  2.], shape=(5,), dtype=float32)
tf.Tensor(
[[-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]
 [-2. -1.  0.  1.  2.]], shape=(5, 5), dtype=float32)
tf.Tensor(
[[-2. -2. -2. -2. -2.]
 [-1. -1. -1. -1. -1.]
 [ 0.  0.  0.  0.  0.]
 [ 1.  1.  1.  1.  1.]
 [ 2.  2.  2.  2.  2.]], shape=(5, 5), dtype=float32)
tf.Tensor(
[[[-2. -2.]
  [-1. -2.]
  [ 0. -2.]
  [ 1. -2.]
  [ 2. -2.]]

 [[-2. -1.]
  [-1. -1.]
  [ 0. -1.]
  [ 1. -1.]
  [ 2. -1.]]

 [[-2.  0.]
  [-1.  0.]
  [ 0.  0.]
  [ 1.  0.]
  [ 2.  0.]]

 [[-2.  1.]
  [-1.  1.]
  [ 0.  1.]
  [ 1.  1.]
  [ 2.  1.]]

 [[-2.  2.]
  [-1.  2.]
  [ 0.  2.]
  [ 1.  2.]
  [ 2.  2.]]], shape=(5, 5, 2), dtype=float32)
posted on 2019-11-26 16:55  Luaser  阅读(303)  评论(0编辑  收藏  举报