Skip to content

Commit

Permalink
regular sigmoid
Browse files Browse the repository at this point in the history
  • Loading branch information
markus583 committed Jun 16, 2024
1 parent 0a2f530 commit 15651e2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name="segment-any-text",
version="1.0.0",
packages=["wtpsplit"],
packages=["segment-any-text"],
description="Universal Robust, Efficient and Adaptable Sentence Segmentation",
author="Markus Frohmann, Igor Sterner, Benjamin Minixhofer",
author_email="[email protected]",
Expand Down
25 changes: 1 addition & 24 deletions wtpsplit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,31 +132,8 @@ def get_subword_label_dict(label_args, tokenizer):
return label_dict


# numerically more stable sigmoid taken from
# https://stackoverflow.com/questions/51976461/optimal-way-of-defining-a-numerically-stable-sigmoid-function-for-a-list-in-pyth
def _positive_sigmoid(x):
return 1 / (1 + np.exp(-x))


def _negative_sigmoid(x):
# Cache exp so you won't have to calculate it twice
exp = np.exp(x)
return exp / (exp + 1)


def sigmoid(x):
positive = x >= 0
# Boolean array inversion is faster than another comparison
negative = ~positive

# empty contains junk hence will be faster to allocate
# Zeros has to zero-out the array after allocation, no need for that
# See comment to the answer when it comes to dtype
result = np.empty_like(x, dtype=np.float)
result[positive] = _positive_sigmoid(x[positive])
result[negative] = _negative_sigmoid(x[negative])

return result
return 1 / (1 + np.exp(-x.astype(np.float32))) # fp32 for better precision


def encode(text):
Expand Down

0 comments on commit 15651e2

Please sign in to comment.