1.We first import the data and dataset functions from TorchText:
1.我们首先从TorchText导入数据和数据集函数。
从 torchtext 导入数据
从 torchtext 导入数据
从 torchtext 导入数据集中
从 torchtext 导入数据集中
2.Next, we create a field and label field we can use with the **TorchText** package. These define the initial processing steps our model will use to process our data:
3.We also define the data type for our labels as floats, which will allow us to calculate our losses and gradients. After defining our fields, we can use these to split our input data. Using the **TREC** dataset from **TorchText**, we pass this our questions and labels fields in order to process the dataset accordingly. We then call the **split** function in order to automatically divide our dataset into a training set and a validation set:
2.Our model is initialized with several inputs, all of which will be covered shortly. Next, we individually define the layers within our network, starting with our embedding layer:
3.Next, we define the actual convolutional layers within our network:
3.接下来,我们定义我们网络中的实际卷积层。
self.convs = nn.ModuleList([
self.convs = nn.ModuleList([
...
@@ -273,7 +273,7 @@ batch_size = 64,
...
@@ -273,7 +273,7 @@ batch_size = 64,
])
])
4.We start by using **nn.ModuleList** to define a series of convolutional layers. **ModuleList** takes a list of modules as input and is used when you wish to define a number of separate layers. As we wish to train several different convolutional layers of different sizes on our input data, we can use **ModuleList** to do so. We could theoretically define each layer separately like so:
1.We start by loading our **spacy** tokenizers into Python. We will need to do this once for each language we are using since we will be building two entirely separate vocabularies for this task:
2.Next, we create a function for each of our languages to tokenize our sentences. Note that our tokenizer for our input English sentence reverses the order of the tokens:
3.With our tokenizers constructed, we now need to define the fields for our tokenization. Notice here how we append start and end tokens to our sequences so that our model knows when to begin and end the sequence’s input and output. We also convert all our input sentences into lowercase for the sake of simplicity: