How to convert a tensor into numpy array

This recipe helps you convert a tensor into numpy array

Recipe Objective

How to convert a tensor into numpy array?

To achieve this we have a function in tensorflow which is "make_ndarray", it will create a array from a tensor. The size or shape and data of the numpy created will be same as tensor.

Step 1 - Import library

import tensorflow as tf

Step 2 - Take a Sample data

Sample_tensor = tf.constant([[1,23,34,45],[56,78,4,3]]) print("This is a tensor:",Sample_tensor)

This is a tensor: tf.Tensor(
[[ 1 23 34 45]
 [56 78  4  3]], shape=(2, 4), dtype=int32)

Step 3 - Convert to numpy array

proto = tf.make_tensor_proto(Sample_tensor) numpy_array = tf.make_ndarray(proto) print("Converted tensor into numpy array is:","\n",numpy_array, "\n","The Shape of the array",numpy_array.shape, numpy_array.dtype)

Converted tensor into numpy array is: 
 [[ 1 23 34 45]
 [56 78  4  3]] 
 The Shape of the array (2, 4) int32

{"mode":"full","isActive":false}

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

Build a Review Classification Model using Gated Recurrent Unit
In this Machine Learning project, you will build a classification model in python to classify the reviews of an app on a scale of 1 to 5 using Gated Recurrent Unit.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build a Multi ClassText Classification Model using Naive Bayes
Implement the Naive Bayes Algorithm to build a multi class text classification model in Python.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python