-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add progress bar #75
Draft
realjanpaulus
wants to merge
1
commit into
master
Choose a base branch
from
add/progress-bar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add progress bar #75
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import chaine | ||
|
||
TEST_1 = False | ||
|
||
if TEST_1: | ||
tokens = [[{"index": 0, "text": "John"}, {"index": 1, "text": "Lennon"}]] | ||
labels = [["B-PER", "I-PER"]] | ||
new_tokens = [] | ||
new_labels = [] | ||
|
||
c = 0 | ||
while c < 100000: | ||
new_tokens.append(tokens[0]) | ||
new_labels.append(labels[0]) | ||
c+=1 | ||
|
||
model = chaine.train(new_tokens, new_labels, verbose=0) | ||
print(model.predict(tokens)) | ||
else: | ||
import datasets | ||
import pandas as pd | ||
from seqeval.metrics import classification_report | ||
|
||
import chaine | ||
from chaine.typing import Dataset, Features, Sentence, Tags | ||
|
||
dataset = datasets.load_dataset("conll2003") | ||
|
||
print(f"Number of sentences for training: {len(dataset['train']['tokens'])}") | ||
print(f"Number of sentences for evaluation: {len(dataset['test']['tokens'])}") | ||
|
||
def featurize_token(token_index: int, sentence: Sentence, pos_tags: Tags) -> Features: | ||
"""Extract features from a token in a sentence. | ||
|
||
Parameters | ||
---------- | ||
token_index : int | ||
Index of the token to featurize in the sentence. | ||
sentence : Sentence | ||
Sequence of tokens. | ||
pos_tags : Tags | ||
Sequence of part-of-speech tags corresponding to the tokens in the sentence. | ||
|
||
Returns | ||
------- | ||
Features | ||
Features representing the token. | ||
""" | ||
token = sentence[token_index] | ||
pos_tag = pos_tags[token_index] | ||
features = { | ||
"token.lower()": token.lower(), | ||
"token[-3:]": token[-3:], | ||
"token[-2:]": token[-2:], | ||
"token.isupper()": token.isupper(), | ||
"token.istitle()": token.istitle(), | ||
"token.isdigit()": token.isdigit(), | ||
"pos_tag": pos_tag, | ||
} | ||
if token_index > 0: | ||
previous_token = sentence[token_index - 1] | ||
previous_pos_tag = pos_tags[token_index - 1] | ||
features.update( | ||
{ | ||
"-1:token.lower()": previous_token.lower(), | ||
"-1:token.istitle()": previous_token.istitle(), | ||
"-1:token.isupper()": previous_token.isupper(), | ||
"-1:pos_tag": previous_pos_tag, | ||
} | ||
) | ||
else: | ||
features["BOS"] = True | ||
if token_index < len(sentence) - 1: | ||
next_token = sentence[token_index + 1] | ||
next_pos_tag = pos_tags[token_index + 1] | ||
features.update( | ||
{ | ||
"+1:token.lower()": next_token.lower(), | ||
"+1:token.istitle()": next_token.istitle(), | ||
"+1:token.isupper()": next_token.isupper(), | ||
"+1:pos_tag": next_pos_tag, | ||
} | ||
) | ||
else: | ||
features["EOS"] = True | ||
return features | ||
|
||
|
||
def featurize_sentence(sentence: Sentence, pos_tags: Tags) -> list[Features]: | ||
"""Extract features from tokens in a sentence. | ||
|
||
Parameters | ||
---------- | ||
sentence : Sentence | ||
Sequence of tokens. | ||
pos_tags : Tags | ||
Sequence of part-of-speech tags corresponding to the tokens in the sentence. | ||
|
||
Returns | ||
------- | ||
list[Features] | ||
List of features representing tokens of a sentence. | ||
""" | ||
return [ | ||
featurize_token(token_index, sentence, pos_tags) for token_index in range(len(sentence)) | ||
] | ||
|
||
|
||
def featurize_dataset(dataset: Dataset) -> list[list[Features]]: | ||
"""Extract features from sentences in a dataset. | ||
|
||
Parameters | ||
---------- | ||
dataset : Dataset | ||
Dataset to featurize. | ||
|
||
Returns | ||
------- | ||
list[list[Features]] | ||
Featurized dataset. | ||
""" | ||
return [ | ||
featurize_sentence(sentence, pos_tags) | ||
for sentence, pos_tags in zip(dataset["tokens"], dataset["pos_tags"]) | ||
] | ||
|
||
|
||
def preprocess_labels(dataset: Dataset) -> list[list[str]]: | ||
"""Translate raw labels (i.e. integers) to the respective string labels. | ||
|
||
Parameters | ||
---------- | ||
dataset : Dataset | ||
Dataset to preprocess labels. | ||
|
||
Returns | ||
------- | ||
list[list[Features]] | ||
Preprocessed labels. | ||
""" | ||
labels = dataset.features["ner_tags"].feature.names | ||
return [[labels[index] for index in indices] for indices in dataset["ner_tags"]] | ||
|
||
train_sentences = featurize_dataset(dataset["train"]) | ||
train_labels = preprocess_labels(dataset["train"]) | ||
|
||
train_sentences = train_sentences[:100] | ||
train_labels = train_labels[:100] | ||
|
||
model = chaine.train(train_sentences, train_labels, verbose=0) | ||
|
||
test_sentences = featurize_dataset(dataset["test"]) | ||
test_labels = preprocess_labels(dataset["test"]) | ||
|
||
predictions = model.predict(test_sentences) | ||
|
||
print(classification_report(test_labels, predictions)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't want any third-party dependencies. Since
progress
is a quite lightweight project, can you just migrate tochaine
? As a submodule?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A refactoring of
progress
is necessary in the first place, because we don't need all these different stylings of the progress bar:Just a single, simple and plain progress bar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the
Bar
oneThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer the
Bar
one, the integration ofprogress
is currently temporary and for testing purposes. The main reason for this very drafty PR is, that I'm continue to work on this PR on another computer.I'm currently not sure if I will integrate this directly into the C code or as an outer layer within the Python code, but I would prefer the first one to have more control over the bar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice: https://github.com/doches/progressbar