What are tensors how are they different from numpy arrays

This recipe explains what are tensors and how are they different from numpy arrays

Recipe Objective

What are tensors?

How are they different from numpy arrays? Tensors, this is nothing but the container which can be house data in n-dimensions along with its linear operations, tensors are the conclusion or generalization of the matrices to the n-dimensional space. Tensors are multidimensional array which is uniform in data type as type, we cannot update a tensor but we can create a new on. These are more than a data container as apart from holding numeric data these also include the descriptions of the valid linear transformation within the tensors. Mathematically the vector, scalar, matrix all are tensors. Numpy Array these are nothing but the matrix, the most important part in this Numpy package is the n-dimensional array or ndarray. These are not similar to others, they are different from the python arrays/list in which the NumPy arrays are homogeneous in nature. These are needed to perform mathematical operations on all the elements.

Step 1 - Import library

import tensorflow as tf import numpy as np

Step 2 - Create a tensor

tensor = tf.constant([[1,2,3],[2,3,4],[4.4,5,6]]) print("This is a simple tensor:","\n",tensor)

This is a simple tensor: 
 tf.Tensor(
[[1.  2.  3. ]
 [2.  3.  4. ]
 [4.4 5.  6. ]], shape=(3, 3), dtype=float32)

So here we can see the simple tensor with shape (3,3) which is 3 columns and 3 rows also the data type as "dtype" is being specified.

Step 3 - Create a Numpy array

array = np.array([[1,2,2],[3,33,3],[4,44,4],[5,55,5]]) print("This is a simple numpy array:","\n",array,"\n") print(array.shape, type(array))

This is a simple numpy array: 
 [[ 1  2  2]
 [ 3 33  3]
 [ 4 44  4]
 [ 5 55  5]] 

(4, 3) <class 'numpy.ndarray'>

Here we can see the simple numpy array, with there shape which is (4,3) means 4 columns and 3 rows also the data type is given as "ndarray".

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.