Onepagecode

Onepagecode

Tensorflow Expert In 5 Minutes

In this post, we will dive into the details of TensorFlow Tensors.

Onepagecode's avatar
Onepagecode
May 23, 2022
∙ Paid
Share

Tensors are TensorFlow’s multi-dimensional arrays with uniform types. They are very similar to NumPy arrays, and they are immutable, which means that they cannot be altered once created. You can only create a new copy with the edits.

Let’s see how Tensors work with a code example. But first, to work with TensorFlow objects, we need to import the TensorFlow library. We often use NumPy with TensorFlow, so let’s also import NumPy with the following lines:

import tensorflow as tf
import numpy as np

Creation of Tensors: Creating Tensor Objects

There are several ways to create a tf.Tensor object. Let's start with a few examples. You can create Tensor objects with several TensorFlow functions, as shown in the below examples:

# You can create Tensor objects with the `tf.constant` function:
x = tf.constant([[1, 2, 3, 4 ,5]])
# You can create Tensor objects only consisting of 1s with the `tf.ones` function:
y = tf.ones((1,5))
# You can create Tensor objects only consisting of 0s with the `tf.zeros` function:
z = tf.zeros((1,5))
# You can use the `tf.range()` function to create Tensor objects:
q = tf.range(start=1, limit=6, delta=1)

print(x)
print(y)
print(z)
print(q)

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 Onepagecode
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture