In [18]:
import tensorflow as tf
t = tf.zeros(shape=(2,3,4,5))
tOut [18]:
In [19]:
t.shapeOut [19]:
In [20]:
t.ndimOut [20]:
In [21]:
tf.size(t)Out [21]:
In [22]:
t.dtypeOut [22]:
In [23]:
# get element at index
t[0]Out [23]:
In [24]:
t[-1]Out [24]:
In [25]:
# get first 2 elements of EACH DIMENSION
t[:2, :2, :2, :2]Out [25]:
In [26]:
# get 1st element from EACH dimension EXCEPT the last dimension
t[:1, :1, :1]Out [26]:
Add A dimension to a tensor
using tf.newaxis
In [27]:
t2 = tf.constant([
[1,2],
[3,4]
])
t2Out [27]:
In [35]:
t2.shape, t2.ndimOut [35]:
In [29]:
t2[:, -1]Out [29]:
In [32]:
t3 = t2[...,tf.newaxis]
t3Out [32]:
In [34]:
t3.shape, t3.ndimOut [34]:
In [38]:
t4 = tf.expand_dims(t2, axis=-1)In [40]:
t4, t4.shape, t4.ndimOut [40]: