Conv1D is PyTorch’s one-dimensional convolution layer for scanning a sequence with a kernel. Inputs use three dimensions: samples, channels, and sequence positions. A handcrafted kernel can detect a local pattern such as an adverb–verb–noun sequence. section 7.2.6
Detecting a part-of-speech pattern
Represent each token with four channels—ADV, ADJ, VERB, and NOUN—and unsqueeze the feature matrix to add a batch dimension. A kernel with size 3 and one output channel is loaded into `torch.nn.Conv1d`. Running the tagged sentence produces a maximum value of 3 at “rightly,” where three kernel weights align with the target pattern. 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
Inspect the tensor conversion and batch-dimension insertion used before applying Conv1D. section 7.2.6
