liveBook · Manning

Vectorization

As explained in Deep Learning with Python, Third Edition

The process of converting data into vector form so it can be used by machine-learning code.

Neural networks typically need inputs and targets as tensors, not raw data. Vectorization converts data such as sound, images, or text into that form; some datasets already arrive vectorized. section 6.2.1

Encoding text and labels

Integer sequences representing text can be converted into multi-hot tensors. Categorical labels can remain integers or be represented as one-hot vectors, with a 1 at the label’s index. section 6.2.1 section 4.2.2

Listing 4.13 Encoding the input data
x_train = multi_hot_encode(train_data, num_classes=10000)            #1
x_test = multi_hot_encode(test_data, num_classes=10000)              #2

See the input-encoding example for converting integer-sequence text into multi-hot training and test arrays. section 4.2.2

Listing 4.14 Encoding the labels
def one_hot_encode(labels, num_classes=46):
    results = np.zeros((len(labels), num_classes))
    for i, label in enumerate(labels):
        results[i, label] = 1.0
    return results

y_train = one_hot_encode(train_labels)                             #1
y_test = one_hot_encode(test_labels)                               #2

See a one-hot encoding implementation that places a 1 at each label’s index. section 4.2.2

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines conversion to tensors and illustrates multi-hot and one-hot encoding, while deferring domain-specific preprocessing.
Examples
Uses integer-sequence text encoded as multi-hot arrays and categorical labels encoded as one-hot vectors.
What you can do after
Apply multi-hot encoding to text inputs or one-hot encoding to categorical labels.
How it connects
Connects vectorization with preprocessing, tensors, multi-hot encoding, and one-hot encoding.

Also explained in

Compare how 18 books explain vectorization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.