How to shuffle and repeat datapoints using tf

This recipe helps you shuffle and repeat datapoints using tf

Recipe Objective

How to shuffle and repeat datapoints using tf?

This is achieved by using the function "tf.data.experimental.shuffle_and_repeat" available in tensorflow. The function will shuffle and repeats a dataset, reshuffling the dataset with each repetition. In this the dataset fills the buffere with the "buffer_size" element in each repetition after that from this buffer it samples the elements randomly, as replacing with new elements for the selected elements. Set the buffer size equal to the full size of the dataset for perfect shuffling.

MNIST Handwritten Digit Classification using Machine Learning

Step 1 - Import library

import tensorflow as tf

Step 2 - Take Sample data

Sample_data = tf.data.Dataset.from_tensor_slices([10, 11, 12])

Here we are creating a Dataset whose elements are slices of the given tensors, also the tensors which are given are sliced along their first dimension. The operation will preserves the structure of input tensors removing the first dimension of each tensor and using it as the dataset dimension. All input tensors must have the same size in their first dimensions.

Step 3 - Perform shuffle and repeat

shuffle_repeat = Sample_data.apply(tf.data.experimental.shuffle_and_repeat(4, count=4)) print("This is the result after performing shuffle and repeat:",[values.numpy() for values in shuffle_repeat])

This is the result after performing shuffle and repeat: [10, 11, 12, 11, 12, 10, 12, 10, 11, 12, 11, 10]

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

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

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

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.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

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.

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.

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

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.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.