以词性标注为例
# 输入数据封装成Variable
def prepare_sequence(seq, to_ix):
idxs = [to_ix[w] for w in seq]
tensor = torch.LongTensor(idxs)
return autograd.Variable(tensor)
# 输入数据格式,单个的词和对应的词性
training_data = [
("The dog ate the apple".split(), ["DET", "NN", "V", "DET", "NN"]),
("everybody read that book".split(), ["NN", "V", "DET", "NN"])]
word_to_ix = {}
for sent, tags in training_data:
for word in sent:
if word not in word_to_ix:
word_to_ix[word] = len(word_to_ix)
tag_to_ix = {"DET":0, "NN":1, "V":2}
EMBEDDING_DIM=64
HIDDEN_DIM=6
# 模型的定义
class LSTMTagger:
def __init__(self, embedding_dim, hidden_dim, vocab_size, tagset_size):
super(LSTMTagger, self).__init__()
self.hidden_dim = hidden_dim
self.word_embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim)