This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Add erfinv operator for calculating inverse error function #13811
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
64e9f1a
add default behaviour for argmax
dc10b8d
Merge branch 'master' of /~https://github.com/apache/incubator-mxnet
d610bfa
prototype of erfvin
dc163c5
Merge branch 'master' of /~https://github.com/apache/incubator-mxnet
09c2117
Merge branch 'master' into add-erfinv
57d2bba
add test
08aec73
gpu support
006b2c8
Revert "add default behaviour for argmax"
5545e67
move erfinv to contrib
722a573
edit copyright
d878e07
remove atof
cb3a217
use std and update license
9cc6c46
add license exclude file
03357b9
Merge branch 'master' of /~https://github.com/apache/incubator-mxnet
2f2ff5c
resolve conflict in rat_excludes
b012d84
fix per eric's comments
647747b
change license header
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -659,6 +659,7 @@ The `ndarray` package provides several classes: | |
relu | ||
sigmoid | ||
erf | ||
erfinv | ||
``` | ||
|
||
### More | ||
|
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 |
---|---|---|
|
@@ -659,6 +659,7 @@ Composite multiple symbols into a new one by an operator. | |
relu | ||
sigmoid | ||
erf | ||
erfinv | ||
``` | ||
|
||
### More | ||
|
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,105 @@ | ||
/* | ||
* Copyright (c) 2014 Indiana University | ||
* All rights reserved. | ||
* Written by Prof. Gary L. Pavlis, Dept. of Geol. Sci., | ||
* Indiana University, Bloomington, IN | ||
* This software is licensed under the New BSD license: | ||
* Redistribution and use in source and binary forms, | ||
* with or without modification, are permitted provided | ||
* that the following conditions are met: | ||
* Redistributions of source code must retain the above | ||
* copyright notice, this list of conditions and the | ||
* following disclaimer. | ||
* Redistributions in binary form must reproduce the | ||
* above copyright notice, this list of conditions and | ||
* the following disclaimer in the documentation and/or | ||
* other materials provided with the distribution. | ||
* Neither the name of Indiana University nor | ||
* the names of its contributors may be used to endorse | ||
* or promote products derived from this software without | ||
* specific prior written permission. | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED | ||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | ||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | ||
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
/* | ||
* The next function is taken from | ||
* /~https://github.com/antelopeusersgroup/antelope_contrib/blob/master/lib/location/libgenloc/erfinv.c. | ||
* Output was modified to be inf or -inf when input is 1 or -1. | ||
*/ | ||
#ifndef MXNET_OPERATOR_CONTRIB_ERFINV_INL_H_ | ||
#define MXNET_OPERATOR_CONTRIB_ERFINV_INL_H_ | ||
|
||
#define _USE_MATH_DEFINES | ||
|
||
#include <mxnet/base.h> | ||
#include <limits> | ||
#include "math.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
namespace mshadow_op { | ||
|
||
/*! \brief inverse gauss error function */ | ||
struct erfinv : public mxnet_op::tunable { | ||
template<typename DType> | ||
MSHADOW_XINLINE static DType Map(DType v) { | ||
/* Function to calculate inverse error function. Rational approximation | ||
is used to generate an initial approximation, which is then improved to | ||
full accuracy by two steps of Newton's method. Code is a direct | ||
translation of the erfinv m file in matlab version 2.0. | ||
Author: Gary L. Pavlis, Indiana University | ||
Date: February 1996 | ||
*/ | ||
const double central_range = 0.7; | ||
double y = static_cast<double>(v); | ||
double y_fab = std::fabs(y); | ||
/*working variables */ | ||
double x = 0.0; | ||
double z, num, dem; | ||
/* coefficients in rational expansion */ | ||
double a[4]={ 0.886226899, -1.645349621, 0.914624893, -0.140543331}; | ||
double b[4]={-2.118377725, 1.442710462, -0.329097515, 0.012229801}; | ||
double c[4]={-1.970840454, -1.624906493, 3.429567803, 1.641345311}; | ||
double d[2]={ 3.543889200, 1.637067800}; | ||
if (y_fab > 1.0) { | ||
/* This needs IEEE constant*/ | ||
return DType(std::numeric_limits<double>::quiet_NaN()); | ||
} else if (y_fab == 1.0) { | ||
return DType((std::copysign(1.0, y))*std::numeric_limits<double>::infinity()); | ||
} else if (y_fab <= central_range) { | ||
z = y*y; | ||
num = (((a[3]*z + a[2])*z + a[1])*z + a[0]); | ||
dem = ((((b[3]*z + b[2])*z + b[1])*z +b[0])*z + 1.0); | ||
x = y*num/dem; | ||
} else { | ||
z = std::sqrt(-std::log((1.0-y_fab)/2.0)); | ||
num = ((c[3]*z + c[2])*z + c[1])*z + c[0]; | ||
dem = (d[1]*z + d[0])*z + 1.0; | ||
x = (std::copysign(1.0, y))*num/dem; | ||
} | ||
/* Two steps of Newton-Raphson correction */ | ||
x = x - (std::erf(x) - y)/((2.0/std::sqrt(M_PI))*std::exp(-x*x)); | ||
x = x - (std::erf(x) - y)/((2.0/std::sqrt(M_PI))*std::exp(-x*x)); | ||
|
||
return DType(x); | ||
} | ||
}; | ||
|
||
} // namespace mshadow_op | ||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_OPERATOR_CONTRIB_ERFINV_INL_H_ |
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
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
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
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
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.
@rondogency do you remember where this license came from?