Pytorch__序列编码

为了给RNN与LSTM打基础,需要了解关于时间序列的相关知识,我们在对输入的数据进行维度编码时候,pytorch提供了word2vec和GloVe的编码库,但是我们还是需要手动写一段代码来理解时间序列编码的过程。

import torch
word_to_ix={
    
    'hello':0,'world':1}#给我们的语言序列加上label
lookup_tensor=torch.tensor([word_to_ix['hello']],dtype=torch.long)
#选出索引名为'hello'的序列字段
embeds=torch.nn.Embedding(2,5)
#进行(2,5)维度编码,方便我们存入所需的序列字段
hello_embed=embeds(lookup_tensor)#在(2,5)维度中进行编码
print(hello_embed)#打印出其feature——vector
tensor([[-0.4491,  0.4688, -1.5705,  0.1171, -1.2012]],
       grad_fn=<EmbeddingBackward>)

猜你喜欢

转载自blog.csdn.net/soulproficiency/article/details/107234394