liveBook · Manning

Tensor

As explained in Natural Language Processing in Action, Second Edition

A tensor is a multidimensional numerical array used to represent and transform structured data in machine-learning computations.

A tensor is a numerical array whose shape tells a neural-network layer how its values are organized. In text processing, those values might represent tokens, word features, predictions, or learned weights. Shape is a practical constraint: a layer must receive the arrangement of values it expects, much like one LEGO brick must fit the next. section 7.5.1

Consider a sentence represented by four part-of-speech channels: adverb, adjective, verb, and noun. With one column per word, its feature values form a two-dimensional array: channels × words. To pass one sentence to the PyTorch convolutional layer, convert the numerical values to a `torch.float32` tensor and add a leading dimension for the example. PyTorch requires suitable tensors rather than arbitrary pandas or NumPy objects as layer inputs. section 7.2.6

Listing 7.7 Converting a DataFrame to a tensor with the correct size
>>> import torch
>>> x = torch.tensor(
...     df.iloc[1:].astype(float).values,
...     dtype=torch.float32)             #1
>>> x = x.unsqueeze(0)        #2

The conversion selects numerical DataFrame rows, creates a float tensor, then uses `unsqueeze(0)` to add the leading dimension. section 7.2.6

A pattern across words and channels

A convolutional filter for “adverb, verb, noun” spans three successive words and all four channels. Its weights begin as a 4 × 3 matrix; adding a dimension prepares them for a `Conv1d` layer with four input channels, one output channel, and a kernel size of three. Applied to the sentence, the filter produces a score of 3 at the three-word match. The output sequence is shorter because the filter cannot start at every original word position. section 7.2.6

Tracking shape remains important beyond this small example. A CNN can move from padded token IDs to embedding vectors, convolutions, pooling, and class predictions; kernel size and stride affect the lengths available to later layers. An RNN instead passes a hidden state between token steps and can provide an output tensor after each step. Transformer encoder layers keep their encoding tensors the same shape from layer to layer, making them easier to stack; an NLP encoding commonly stores a sequence of embedding vectors in a two-dimensional tensor. section 7.5.1 section 8.1.4 section 9.1.2

When inspecting predictions with NumPy, detach a PyTorch output tensor before converting it to an array. The useful habit is to check both the values and the shape at each handoff: a plausible-looking array is not necessarily an input the next layer can use. section 5.2.5 section 7.5.1

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Works through conversion, dimensions, and layer-shape calculations, without developing tensor storage internals.
Examples
Uses part-of-speech sentence features, a three-word convolutional filter, and printed CNN layer shapes.
What it assumes
Builds from tabular word features and Python array operations while explaining channels and layer shapes through examples.
What you can do after
Build it from scratch by converting numerical data to PyTorch tensors and checking shapes across CNN layers.
How it connects
Connects tensors to word embeddings, convolutional neural networks, recurrent neural networks, and transformer encodings.

Also explained in

Compare how 7 books explain tensor →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.