

If you need a Tensor use the tf.rank or tf.shape function. Print("Total number of elements (3*2*4*5): ", tf.size(rank_4_tensor).numpy())īut note that the Tensor.ndim and Tensor.shape attributes don't return Tensor objects. Print("Elements along the last axis of tensor:", rank_4_tensor.shape) Print("Elements along axis 0 of tensor:", rank_4_tensor.shape) Print("Shape of tensor:", rank_4_tensor.shape) Print("Number of axes:", rank_4_tensor.ndim) Print("Type of every element:", rank_4_tensor.dtype) Tensors and tf.TensorShape objects have convenient properties for accessing these: rank_4_tensor = tf.zeros() Note: Although you may see reference to a "tensor of two dimensions", a rank-2 tensor does not usually describe a 2D space.

Print(a + b, "\n") # element-wise addition ]) # Could have also said `tf.ones(, dtype=tf.int32)` You can do basic math on tensors, including addition, element-wise multiplication, and matrix multiplication. Sparse tensors (see SparseTensor below).Ragged tensors (see RaggedTensor below).However, there are specialized types of tensors that can handle different shapes: The base tf.Tensor class requires tensors to be "rectangular"-that is, along each axis, every element is the same size. Tensors often contain floats and ints, but have many other types, including: You can convert a tensor to a NumPy array either using np.array or the tensor.numpy method: np.array(rank_2_tensor) There are many ways you might visualize a tensor with more than two axes. Tensors may have more axes here is a tensor with three axes: # There can be an arbitrary number of Rank_1_tensor = tf.constant()Ī "matrix" or "rank-2" tensor has two axes: # If you want to be specific, you can set the dtype (see below) at creation time A vector has one axis: # Let's make this a float tensor. # This will be an int32 tensor by default see "dtypes" below.Ī "vector" or "rank-1" tensor is like a list of values. A scalar contains a single value, and no "axes".

#GET SELECTION OF NON SEQUENTIAL COLUMNS AS MATRIX NP UPDATE#
If you're familiar with NumPy, tensors are (kind of) like np.arrays.Īll tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one. You can see all supported dtypes at tf.dtypes.DType. Tensors are multi-dimensional arrays with a uniform type (called a dtype).
