NLP models use text to produce a basic form of natural language understanding. Applications ranging from document classification, sentiment analysis, or even question answering. In this tutorial we will learn how to use TensorFlow’s Dataset API to build pipelines for text.

Download Dataset

First, head to https://www.kaggle.com/quora/question-pairs-dataset/kernels and download the raw Question Pairs Dataset. Uncompress it. It is related to the problem of identifying duplicate questions

Reading input data

If all of your input data fit in memory, the simplest way to create a Dataset from them is to convert them to tf.Tensorobjects and use Dataset.from_tensor_slices().

Tokenize Data

Natural language processing is pattern recognition applied to words, sentences, and paragraphs. NLP models don’t take as input raw text: they only work with numeric tensors. The vectorizing text is the process of transforming text into numeric tensors.

You can break down text into words, characters or N-grams are called “tokens”, and breaking down text into such tokens is called “tokenization”. All text vectorization processes consist in applying some tokenization scheme, then associating numeric vectors with the generated tokens. These vectors, packed into sequence tensors, are what get fed into deep neural networks.

NLP rely on ids as input for the words, that means you need to convert your sentence into a sequence of ids. We have to transform sequence of words into a sequence of ids

Tensorflow has a built-in tool to take care of the mapping. We simply define lookup tables.

Now that we initialized this lookup table, we are going to transform.

Batching with padding

Sequence models work with input data that can have varying size (e.g. sequences of different lengths). To handle this case, theDataset.padded_batch() transformation enables you to batch tensors of different shape by specifying one or more dimensions in which they may be padded.

It allows you to set different padding for each dimension of each component, and it may be variable-length (signified by None in the example above) or constant-length. It is also possible to override the padding value, which defaults to 0.