目錄python
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>
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])
start🔚stepdom
::stepcode
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])
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])
data: [classes, students, subjects]orm
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])
[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])
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])