While axes are often referred to by their indices, you should always keep track of the meaning of each. This difference is subtle, but it can be important when building graphs (later). 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. Size: The total number of items in the tensor, the product of the shape vector's elements.Axis or Dimension: A particular dimension of a tensor.A scalar has rank 0, a vector has rank 1, a matrix is rank 2. Shape: The length (number of elements) of each of the axes of a tensor.Note: Typically, anywhere a TensorFlow function expects a Tensor as input, the function will also accept anything that can be converted to a Tensor using tf.convert_to_tensor. Tensors are used in all kinds of operations (or "Ops"). Print(a * b, "\n") # element-wise multiplication 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". 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).
0 Comments
Leave a Reply. |