Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1190 Fix OneHotEmbeddings #1191

Merged
merged 1 commit into from
Oct 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions flair/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,6 @@ def __str__(self):
def extra_repr(self):
return f"'{self.embeddings}'"


class OneHotEmbeddings(TokenEmbeddings):
"""One-hot encoded embeddings."""

Expand All @@ -465,6 +464,7 @@ def __init__(
self.name = "one-hot"
self.static_embeddings = False
self.min_freq = min_freq
self.field = field

tokens = list(map((lambda s: s.tokens), corpus.train))
tokens = [token for sublist in tokens for token in sublist]
Expand All @@ -473,7 +473,7 @@ def __init__(
most_common = Counter(list(map((lambda t: t.text), tokens))).most_common()
else:
most_common = Counter(
list(map((lambda t: t.get_tag(field)), tokens))
list(map((lambda t: t.get_tag(field).value), tokens))
).most_common()

tokens = []
Expand Down Expand Up @@ -508,9 +508,15 @@ def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:

one_hot_sentences = []
for i, sentence in enumerate(sentences):
context_idxs = [
self.vocab_dictionary.get_idx_for_item(t.text) for t in sentence.tokens
]

if self.field == "text":
context_idxs = [
self.vocab_dictionary.get_idx_for_item(t.text) for t in sentence.tokens
]
else:
context_idxs = [
self.vocab_dictionary.get_idx_for_item(t.get_tag(self.field).value) for t in sentence.tokens
]

one_hot_sentences.extend(context_idxs)

Expand Down