What does tf cast do

This recipe explains what does tf.cast do

Recipe Objective

What does tf.cast do?

The "tf.cast" function casts a tensor to new type. The operation "cast" support the data types of int32, int64, float16, float32, float64, complex64, complex128, bfloat16, uint8, uint16, uint32, uint64, int8, int16. Only the real part of "x" is returned in case of casting from complex types to real types. Also in other case of casting from real to complex types then the imaginary part of the returned value is set to 0. The behavior of NumPy is matched with the complex types.

Complete Guide to Tensorflow for Deep Learning with Python for Free

Step 1 - Import library

import tensorflow as tf

Step 2 - Take Sample data

Sample_data = tf.constant([2.3, 4.0], dtype=tf.float32) print("This is a Sample data of type float:",Sample_data)

This is a Sample data of type float: tf.Tensor([2.3 4. ], shape=(2,), dtype=float32)

Step 3 - Perform Casting

cast = tf.dtypes.cast(Sample_data, tf.int32) print("This is the result after casting:",cast)

This is the result after casting: tf.Tensor([2 4], shape=(2,), dtype=int32)

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

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

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

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 CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Llama2 Project for MetaData Generation using FAISS and RAGs
In this LLM Llama2 Project, you will automate metadata generation using Llama2, RAGs, and AWS to reduce manual efforts.

Build CI/CD Pipeline for Machine Learning Projects using Jenkins
In this project, you will learn how to create a CI/CD pipeline for a search engine application using Jenkins.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.