class ModelEmbeddings(nn.Module): def __init__(self, embed_size, vocab): """ 参数: embed_size (int): Embedding size (dimensionality) 参数: vocab (Vocab): Vocabulary object containing src and tgt languages See vocab.py for documentation. """ super(ModelEmbeddings, self).__init__() self.embed_size = embed_size # default values self.source = None self.target = None src_pad_token_idx = vocab.src['<pad>'] tgt_pad_token_idx = vocab.tgt['<pad>'] """ add your code here --- 2 目标: 初始化下面的两个参数: self.source (Embedding Layer for source language) self.target (Embedding Layer for target langauge)
时间: 2024-01-27 20:06:05 浏览: 80
首届ST_EMBED电子设计大赛.pdf
Here is the code to initialize `self.source` and `self.target`:
```python
self.source = nn.Embedding(num_embeddings=len(vocab.src),
embedding_dim=embed_size,
padding_idx=src_pad_token_idx)
self.target = nn.Embedding(num_embeddings=len(vocab.tgt),
embedding_dim=embed_size,
padding_idx=tgt_pad_token_idx)
```
In the above code, `nn.Embedding` is used to create embeddings for source and target languages. `num_embeddings` is set to the size of the vocabulary of each language, `embedding_dim` is set to the given `embed_size`, and `padding_idx` is set to the index of the `<pad>` token in the vocabulary. This ensures that the embeddings for `<pad>` tokens are always zero.
阅读全文