What are tensor slices in tf

This recipe explains what are tensor slices in tf

Recipe Objective

What are tensor_slices in tf?

Slice operation performs the extraction of a slice of particular size from a tensor input, the starting location is specified by begin. The tensor shape represents size of the slice where the "size[i]" represents the number of elements of the i'th dimension of the input that we want to slice. An offset in each dimension of input is represented by the starting location which is "begin" for the slice. Lets understand this with practical implementation.

Build a Multi Touch Attribution Model in Python with Source Code

Step 1 - Import library

import tensorflow as tf

Step 2 - Take Sample data

Sample_tensor = tf.constant([[[2,3,4],[5,6,7]],[[7,8,9],[1,2,3]],[[4,5,6],[2,6,9]]]) print("This is a Sample tensor:",Sample_tensor)

This is a Sample tensor: tf.Tensor(
[[[2 3 4]
  [5 6 7]]

 [[7 8 9]
  [1 2 3]]

 [[4 5 6]
  [2 6 9]]], shape=(3, 2, 3), dtype=int32)

Step 3 - Perform Slicing

Slice_tensor = tf.slice(Sample_tensor, [1,0,0],[1,1,3]) print("This is the output for sliced tensor:",Slice_tensor)

This is the output for sliced tensor: tf.Tensor([[[7 8 9]]], shape=(1, 1, 3), dtype=int32)

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

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

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.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

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.