Skip to content

Add tanh Chebyshev approx#3004

Merged
iamtrask merged 5 commits intoOpenMined:masterfrom
gmuraru:gm-improve-tanh
Feb 26, 2020
Merged

Add tanh Chebyshev approx#3004
iamtrask merged 5 commits intoOpenMined:masterfrom
gmuraru:gm-improve-tanh

Conversation

@gmuraru
Copy link
Member

@gmuraru gmuraru commented Feb 3, 2020

This implementation is taken from the FacebookResearch Crypten project[1].
The PR that adds this functionality in Crypten is here[2].

[1] https://github.com/facebookresearch/CrypTen
[2] facebookresearch/CrypTen#45

@gmuraru gmuraru force-pushed the gm-improve-tanh branch 2 times, most recently from 77b9dd5 to 1db29b5 Compare February 3, 2020 21:49
@gmuraru gmuraru requested review from LaRiffle, andrelmfarias, iamtrask and robert-wagner and removed request for andrelmfarias February 3, 2020 22:56
Copy link
Member

@iamtrask iamtrask left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good - needs a few tests 👍

# truncate outside [-maxval, maxval]
out = torch.where(tensor > maxval, 1.0, out)
out = torch.where(tensor < -maxval, -1.0, out)
return out
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, 2 * sigmoid(2 * tensor) - 1 wasn't precise enough or too slow?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the PR, they say this:

Implemented improved tanh approximation that's ~43.5% more accurate (measured by total relative error) and ~33% faster (see n196073).

Before this, they also had the implementation using the sigmoid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool!

@gmuraru
Copy link
Member Author

gmuraru commented Feb 6, 2020

@iamtrask added the tests from here

@gmuraru gmuraru requested a review from iamtrask February 6, 2020 01:11
Copy link
Contributor

@LaRiffle LaRiffle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind also add a test for tanh in the same way I did for exp/log etc (ie so that people can have an idea of the error rate just by looking at the test)

Copy link
Member

@iamtrask iamtrask left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to Theo's request for readable test but I won't block for it. :)

@iamtrask iamtrask merged commit bfeafe2 into OpenMined:master Feb 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants