文章目录
TensorFlow2 Index & slice
-
indexing
-
Basic indexing
[idx][idx][idx]
-
Same with Numpy
[idx, idx, …]
-
-
slice
- start:end (😃
- start🔚step (:😃
- …
import tensorflow as tf
import numpy as np
Basic indexing
a = tf.random.uniform([2, 5, 5, 3], seed=2333) # [n, h, w, c]
a
<tf.Tensor: shape=(2, 5, 5, 3), dtype=float32, numpy=
array([[[[0.07041156, 0.8905022 , 0.6216873 ],
[0.21505892, 0.4979725 , 0.46020842],
[0.65414095, 0.20010316, 0.53874445],
[0.58445954, 0.496145 , 0.49120915],
[0.63314736, 0.34663606, 0.10259581]],
[[0.11686707, 0.91998386, 0.51255167],
[0.579185 , 0.14591908, 0.3473047 ],
[0.05141556, 0.10163951, 0.84329593],
[0.84977293, 0.65196884, 0.29003954],
[0.7205261 , 0.25438452, 0.06029487]],
[[0.5822334 , 0.62231576, 0.9710605 ],
[0.07146776, 0.2591331 , 0.13276327],
[0.7667595 , 0.5042423 , 0.3706689 ],
[0.39641988, 0.8596374 , 0.16389298],
[0.6250875 , 0.5708239 , 0.8311932 ]],
[[0.05696642, 0.1532799 , 0.6918578 ],
[0.3383143 , 0.51761115, 0.61913586],
[0.7259209 , 0.9582299 , 0.5157602 ],
[0.57788205, 0.38194263, 0.6794008 ],
[0.23590899, 0.39751816, 0.8117993 ]],
[[0.8092195 , 0.42989707, 0.2631619 ],
[0.22554791, 0.9572815 , 0.9741839 ],
[0.18659306, 0.32266033, 0.4722123 ],
[0.85999477, 0.8539176 , 0.10803545],
[0.76716816, 0.33625448, 0.19057953]]],
[[[0.92845273, 0.25391316, 0.790344 ],
[0.9043237 , 0.8727969 , 0.14354527],
[0.7343507 , 0.96790564, 0.24570918],
[0.7609067 , 0.93686616, 0.5444143 ],
[0.176139 , 0.46293545, 0.6443963 ]],
[[0.26876187, 0.9026631 , 0.92430747],
[0.51341975, 0.14648497, 0.9482733 ],
[0.445796 , 0.4757012 , 0.26133525],
[0.8501693 , 0.7251086 , 0.14755058],
[0.54302335, 0.5745591 , 0.52269447]],
[[0.5917747 , 0.5329877 , 0.28812265],
[0.2584529 , 0.14065826, 0.393144 ],
[0.15639925, 0.03443658, 0.3544873 ],
[0.73176014, 0.820879 , 0.3850819 ],
[0.33274925, 0.5683056 , 0.9336487 ]],
[[0.00620544, 0.9369093 , 0.5079032 ],
[0.16955161, 0.29554403, 0.02863216],
[0.22004664, 0.13030851, 0.6525897 ],
[0.08574426, 0.891763 , 0.06320643],
[0.02958846, 0.33901882, 0.9253638 ]],
[[0.55194664, 0.33885276, 0.6979345 ],
[0.96229804, 0.96465445, 0.17471063],
[0.7496917 , 0.7406851 , 0.5581598 ],
[0.78590655, 0.6984439 , 0.42078125],
[0.37219024, 0.97534156, 0.87748015]]]], dtype=float32)>
a[0][0]
<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[0.07041156, 0.8905022 , 0.6216873 ],
[0.21505892, 0.4979725 , 0.46020842],
[0.65414095, 0.20010316, 0.53874445],
[0.58445954, 0.496145 , 0.49120915],
[0.63314736, 0.34663606, 0.10259581]], dtype=float32)>
a[0][0][0]
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.07041156, 0.8905022 , 0.6216873 ], dtype=float32)>
a[0][0][0][2]
<tf.Tensor: shape=(), dtype=float32, numpy=0.6216873>
Numpy-style indexing
a[0].shape # shape=(5, 5, 3)
TensorShape([5, 5, 3])
a[0, 0] # shape=(5, 3)
<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[0.07041156, 0.8905022 , 0.6216873 ],
[0.21505892, 0.4979725 , 0.46020842],
[0.65414095, 0.20010316, 0.53874445],
[0.58445954, 0.496145 , 0.49120915],
[0.63314736, 0.34663606, 0.10259581]], dtype=float32)>
a[0, 0, 0] # shape=(3,)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([0.07041156, 0.8905022 , 0.6216873 ], dtype=float32)>
a[0, 0, 0, 2