索引和切片

TensorFlow2教程完整教程目录(更有python、go、pytorch、tensorflow、爬虫、人工智能教学等着你):https://www.cnblogs.com/nickchen121/p/10840284.html

索引和切片

索引

numpy [ ] 索引

import tensorflow as tf
a = tf.ones([1, 5, 5, 3])
a.shape
TensorShape([1, 5, 5, 3])
a[0][0]
<tf.Tensor: id=767, shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]], dtype=float32)>
a[0][0][0]
<tf.Tensor: id=780, shape=(3,), dtype=float32, numpy=array([1., 1., 1.], dtype=float32)>
a[0][0][0][2]
<tf.Tensor: id=797, shape=(), dtype=float32, numpy=1.0>

numpy : 索引

a = tf.random.normal([4, 28, 28, 3])
a.shape
TensorShape([4, 28, 28, 3])
a[1].shape
TensorShape([28, 28, 3])
a[1, 2].shape
TensorShape([28, 3])
a[1][2][3].shape
TensorShape([3])
a[1, 2, 3, 2].shape
TensorShape([])

切片

一维切片

a = tf.range(10)
a
<tf.Tensor: id=832, shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int32)>
a[-1:]
<tf.Tensor: id=837, shape=(1,), dtype=int32, numpy=array([9], dtype=int32)>
a[-2:]
<tf.Tensor: id=842, shape=(2,), dtype=int32, numpy=array([8, 9], dtype=int32)>
a[:2]
<tf.Tensor: id=847, shape=(2,), dtype=int32, numpy=array([0, 1], dtype=int32)>
a[:-1]
<tf.Tensor: id=852, shape=(9,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int32)>

多维切片

a = tf.random.normal([4, 28, 28, 3])
a.shape
TensorShape([4, 28, 28, 3])
a[0].shape
TensorShape([28, 28, 3])
a[0, :, :, :].shape
TensorShape([28, 28, 3])
a[0, 1, :, :].shape
TensorShape([28, 3])
a[:, :, :, 0].shape
TensorShape([4, 28, 28])
a[:, :, :, 2].shape
TensorShape([4, 28, 28])
a[:, 0, :, :]    .shape
TensorShape([4, 28, 3])

步长::step

start🔚step

::step

a = tf.random.normal([4, 28, 28, 3])
a.shape
TensorShape([4, 28, 28, 3])
a[0:2, :, :, :].shape
TensorShape([2, 28, 28, 3])
a[:, 0:28:2, 0:28:2, :].shape
TensorShape([4, 14, 14, 3])
a[:, :14, :14, :].shape
TensorShape([4, 14, 14, 3])
a[:, 14:, 14:, :].shape
TensorShape([4, 14, 14, 3])
a[:, ::2, ::2, :].shape
TensorShape([4, 14, 14, 3])

倒序::-1

a = tf.range(4)
a
<tf.Tensor: id=913, shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>
a[::-1]
<tf.Tensor: id=918, shape=(4,), dtype=int32, numpy=array([3, 2, 1, 0], dtype=int32)>
a[::-2]
<tf.Tensor: id=923, shape=(2,), dtype=int32, numpy=array([3, 1], dtype=int32)>
a[2::-2]
<tf.Tensor: id=928, shape=(2,), dtype=int32, numpy=array([2, 0], dtype=int32)>

省略号...

a = tf.random.normal([2, 4, 28, 28, 3])
a.shape
TensorShape([2, 4, 28, 28, 3])
a[0].shape
TensorShape([4, 28, 28, 3])
a[0, :, :, :, :].shape
TensorShape([4, 28, 28, 3])
a[0, ...].shape
TensorShape([4, 28, 28, 3])
a[:, :, :, :, 0].shape
TensorShape([2, 4, 28, 28])
a[..., 0].shape
TensorShape([2, 4, 28, 28])
a[0, ..., 2].shape
TensorShape([4, 28, 28])
a[1, 0, ..., 0].shape
TensorShape([28, 28])

Selective Indexing

  • tf.gather:收集特定行
  • tf.gather_nd
  • tf.boolean_mask

data: [classes, students, subjects]

gather

a = tf.random.normal([4, 35, 8])
a.shape
TensorShape([4, 35, 8])
tf.gather(a, axis=0, indices=[2, 3]).shape
TensorShape([2, 35, 8])
a[2:4].shape
TensorShape([2, 35, 8])
tf.gather(a, axis=0, indices=[2, 1, 3, 0]).shape
TensorShape([4, 35, 8])
tf.gather(a, axis=1, indices=[2, 3, 7, 9, 16]).shape
TensorShape([4, 5, 8])
tf.gather(a, axis=2, indices=[2, 3, 7]).shape
TensorShape([4, 35, 3])
aa = tf.gather(a,axis,[several students])
aaa = tf.gather(aa,axis,[several subjects])

gather_nd

[class1_student1,class2_student2,class3_student3,class4_student4]
a = tf.random.normal([4, 35, 8])
a.shape
TensorShape([4, 35, 8])
tf.gather_nd(a, [0]).shape  # [[0],[],[]]
TensorShape([35, 8])
tf.gather_nd(a, [0, 1]).shape
TensorShape([8])
tf.gather_nd(a, [0, 1, 2]).shape
TensorShape([])
tf.gather_nd(a, [[0, 1, 2]]).shape
TensorShape([1])
tf.gather_nd(a, [[0, 0], [1, 1]]).shape
TensorShape([2, 8])
tf.gather_nd(a, [[0, 0], [1, 1], [2, 2]]).shape
TensorShape([3, 8])
# 第一个班级第一个学生的第一门课
# 第二个班级第二个学生的第二门课
# 第三个班级第三个学生的第三门课
tf.gather_nd(a, [[0, 0, 0], [1, 1, 1], [2, 2, 2]]).shape
TensorShape([3])
tf.gather_nd(a, [[[0, 0, 0], [1, 1, 1], [2, 2, 2]]]).shape
TensorShape([1, 3])

boolean_mask

a = tf.random.normal([4, 28, 28, 3])
a.shape
TensorShape([4, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True, False, False]).shape
TensorShape([2, 28, 28, 3])
tf.boolean_mask(a, mask=[True, True, False], axis=3).shape
TensorShape([4, 28, 28, 2])
a = tf.ones([2, 3, 4])
a.shape
TensorShape([2, 3, 4])
# [2,3],还剩下4,三个True,因此是3*4True
tf.boolean_mask(a, mask=[[True, False, False], [False, True, True]]).shape
TensorShape([3, 4])
posted @ 2019-05-09 18:35  B站-水论文的程序猿  阅读(1696)  评论(0编辑  收藏  举报