深度学习(机器学习)tensorflow学习第三课——索引及切片

1、索引

import os

os.environ['TF_CPP_MIN_LOG_LEVEL'] = "2"

import tensorflow as tf

"""
索引
"""
a = tf.ones([1, 5, 5, 3])
"""
print(a)
tf.Tensor(
[[[[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]

  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]

  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]

  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]

  [[1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]
   [1. 1. 1.]]]], shape=(1, 5, 5, 3), dtype=float32)
"""
print(a)

"""
print(a[0])
tf.Tensor(
[[[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]

 [[1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]
  [1. 1. 1.]]], shape=(5, 5, 3), dtype=float32)
"""
print(a[0])

# tf.Tensor(
# [[1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]], shape=(5, 3), dtype=float32)
print(a[0][0])
# tf.Tensor(
# [[1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]
#  [1. 1. 1.]], shape=(5, 3), dtype=float32)
print(a[0][1])


print(a[0][0][0]) # tf.Tensor([1. 1. 1.], shape=(3,), dtype=float32)

print(a[0][0][0][2])  # tf.Tensor(1.0, shape=(), dtype=float32)

2、切片(单冒号切片和双冒号切片)

"""
单冒号切片:记住一个顺口溜,冒号在后,就切去当前位置前面的数据,冒号在前,就切去当前位置以及后面的数据
冒号在后切前,冒号在前切后
start:end
"""

b = tf.range(10)
print(b)  # tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

print(b[-1:])  # tf.Tensor([9], shape=(1,), dtype=int32)
print(b[-2:])  # tf.Tensor([8 9], shape=(2,), dtype=int32)

print(b[:-1])  # tf.Tensor([0 1 2 3 4 5 6 7 8], shape=(9,), dtype=int32)
print(b[:-2])  # tf.Tensor([0 1 2 3 4 5 6 7], shape=(8,), dtype=int32)
print(b[:2])  # tf.Tensor([0 1], shape=(2,), dtype=int32)

print(b[:])  # tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)

print(b[1:5])  # tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)
print(b[1:7])  # tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
"""
start:end:step
小技巧:从start开始数(算上start,start的索引为0),数到step,记录。记录的索引位置再次成为start,也就是0,数完位置
例子:
print(b[1:7])  # tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
start为1,索引为0,往后面数两个,数到了3,把3索引看为0,在数两个,就到了5
所以 print(b[1:7:2])  # tf.Tensor([1 3 5], shape=(3,), dtype=int32)
"""
print(b[1:7:2])  # tf.Tensor([1 3 5], shape=(3,), dtype=int32)
print(b[1:7:3])  # tf.Tensor([1 4], shape=(2,), dtype=int32)
print(b[1:7:1])  # tf.Tensor([1 2 3 4 5 6], shape=(6,), dtype=int32)
"""
双冒号,是搁着采样,双冒号可以实现一个倒序的功能(::-1,::-2)
-1就是倒序,-2就是隔一个采样一个,-3就是隔两个采样
"""
print(b[::2])  # tf.Tensor([0 2 4 6 8], shape=(5,), dtype=int32)
print(b[::-1])  # tf.Tensor([9 8 7 6 5 4 3 2 1 0], shape=(10,), dtype=int32)
print(b[::-2])  # tf.Tensor([9 7 5 3 1], shape=(5,), dtype=int32)
print(b[::-3])  # tf.Tensor([9 6 3 0], shape=(4,), dtype=int32)

print(b[2::-2]) # 从索引为2的元素开始,按照-2流程走
print(b[3::-2]) # 从索引为3的元素开始,按照-2流程走


"""

3、tf.gather() 收集功能

"""
tf.gather() 收集功能
"""
"""
首先创建一个tensor, tf.Tensor([ 4 35  8], shape=(3,), dtype=int32),拥有四个三个维度
有四个班级,每个班级有35名学生,没门学生有八门课程,先要用gather(),收集第一个维度

"""
"""
data = tf.random.uniform([4, 35, 8], minval=0, maxval=20, dtype=tf.int32)
tf.gather(要收集的tensor,axis=要收集该tensor中的维度索引,indices=[该维度所对应的编号])
In [8]: tf.gather(data,axis=0,indices=[0,1,2,3]).shape  # 收集data,中班级维度,的0,1,2,3,号班级
Out[8]: TensorShape([4, 35, 8])  # 收集了四个班级

In [9]: tf.gather(data,axis=0,indices=[0]).shape
Out[9]: TensorShape([1, 35, 8]) # 收集了一个班级

In [10]: tf.gather(data,axis=0,indices=[0,1,2]).shape
Out[10]: TensorShape([3, 35, 8]) # 收集了三个班级

In [11]: tf.gather(data,axis=1,indices=[8,9,15,18]).shape   # 收集data,中学生维度中,第8,9,15,18个学生
Out[11]: TensorShape([4, 4, 8])

In [12]: tf.gather(data,axis=1,indices=[10,12]).shape  
Out[12]: TensorShape([4, 2, 8])

In [13]: tf.gather(data,axis=2,indices=[4,5,6,7]).shape     # 收集data,中学科维度中,第4,5,6,7门课程
Out[13]: TensorShape([4, 35, 4])

In [14]: tf.gather(data,axis=2,indices=[4]).shape
Out[14]: TensorShape([4, 35, 1])

"""

4、tf.gather_nd() 收集功能

"""
#############      tf.gather_nd()

data = tf.random.uniform([4, 35, 8], minval=0, maxval=20, dtype=tf.int32)
In [26]: data[0].shape
Out[26]: TensorShape([35, 8])

In [27]: data[0,1].shape
Out[27]: TensorShape([8])

In [28]: data[0,1,2].shape
Out[28]: TensorShape([])

In [29]: tf.gather_nd(data,[0]).shape
Out[29]: TensorShape([35, 8])

In [30]: tf.gather_nd(data,[0,1]).shape
Out[30]: TensorShape([8])

In [31]: tf.gather_nd(data,[0,1,2]).shape
Out[31]: TensorShape([])



In [32]: tf.gather_nd(data,[[0,0],[1,1]]).shape  
# 取第0号班级第0号学生,就是一个八维的tensor,取第1号班级第1号学生,也是一个八维,所以是两个八维 shape(2,8)
Out[32]: TensorShape([2, 8])

In [33]: tf.gather_nd(data,[[0,0],[1,1],[2,2]]).shape
Out[33]: TensorShape([3, 8])

In [34]: tf.gather_nd(data,[[0,0,0],[1,1,1],[2,2,2]]).shape 
# 采样0号班级0号学生0号课程,采样1号班级1号学生1号课程,采样2号班级2号学生3号课程,
# 看成一个整体,就是三个标量
Out[34]: TensorShape([3])

In [35]: tf.gather_nd(data,[[[0,0,0],[1,1,1],[2,2,2]]]).shape  
Out[35]: TensorShape([1, 3])


"""

5、tf.boolean_mask()

"""
#############      tf.boolean_mask
data = tf.random.uniform([4,28,28,3], minval=0, maxval=20, dtype=tf.int32)

In [40]: data.shape
Out[40]: TensorShape([4, 28, 28, 3])

In [41]: tf.boolean_mask(data,mask=[True,True,False,False]).shape 
# 如果不加axis参数,那么就是默认为0,要对应起来,axis=0,对应的是第一个维度4,所以要有[要有四个]
# [True,True,False,False],就是第一个维度中,前两号元素取,后面两个不取,所以shape[2,28,28,3],因为取了2个,所以是2
Out[41]: TensorShape([2, 28, 28, 3])

In [42]: tf.boolean_mask(data,mask=[True,True,False],axis=3).shape
Out[42]: TensorShape([4, 28, 28, 2])


"""

 

posted @ 2020-07-28 16:44  PY憨  阅读(320)  评论(0编辑  收藏  举报