How to convert a numpy array to tensor

This recipe helps you convert a numpy array to tensor

Recipe Objective

How to convert a numpy array to tensor?

To achieve this we have a function in tensorflow called "convert_to_tensor", this will convert the given value into a tensor. The value can be a numpy array, python list and python scalars, for the following the function will return a tensor.

Step 1 - Import library

import tensorflow as tf import numpy as np

Step 2 - Take a Sample data

array = np.array([[1,2,3],[3,4,5],[5,6,7]]) print("This is a numpy array:") print(array, array.shape, type(array))

This is a numpy array:
[[1 2 3]
 [3 4 5]
 [5 6 7]] (3, 3) <class 'numpy.ndarray'>

Step 3 - Convert to Tensor

tensor = tf.convert_to_tensor(array) print("Converted numpy array into tensor:") print(tensor)

Converted numpy array into tensor:
tf.Tensor(
[[1 2 3]
 [3 4 5]
 [5 6 7]], shape=(3, 3), dtype=int64)

Step 4 - Method 2

def con_ten(convert_func): convert_func = tf.convert_to_tensor(convert_func, dtype=tf.int32) return convert_func first_value = con_ten(tf.constant([[1,2,3,4],[5,6,7,8]])) print(first_value)

tf.Tensor(
[[1 2 3 4]
 [5 6 7 8]], shape=(2, 4), dtype=int32)

In the second method, we can directly make a function and call that function whenever we want to perform the task of conversion of array into tensor for several times.

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

 

Join Millions of Satisfied Developers and Enterprises to Maximize Your Productivity and ROI with ProjectPro - Read ProjectPro Reviews Now!

 

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

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using 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.

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.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS