def tensorize(sentence, max_length):
""" Input:
- sentence: The sentence is a tuple of lists (s1, s2, ..., sk)
s1 is always a sequence of word ids.
sk is always a sequence of label ids.
s2 ... sk-1 are sequences of feature ids,
such as predicate or supertag features.
- max_length: The maximum length of sequences, used for padding.
"""
x = np.array([t for t in zip(*sentence[:-1])])
y = np.array(sentence[-1])
weights = (y >= 0).astype(float)
x.resize([max_length, x.shape[1]])
y.resize([max_length])
weights.resize([max_length])
return x, np.absolute(y), len(sentence[0]), weights
评论列表
文章目录