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

implement erf #59

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion src/unary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import {Tensor} from './lib/tensor.js';
import {Scalar, Tensor} from './lib/tensor.js';
import {add, div, mul, sub} from '../src/binary.js';

/**
* Compute the element-wise unary operation for input tensor.
Expand Down Expand Up @@ -30,3 +31,25 @@ export const tan = (input) => unary(input, Math.tan);
export const copy = (input) => unary(input, (x) => x);
export const reciprocal = (input) => unary(input, (x) => 1 / x);
export const sqrt = (input) => unary(input, Math.sqrt);
export const sign = (input) => unary(input, Math.sign);

export const erf = (input) => {
/**
*reference 1:https://en.wikipedia.org/wiki/Error_function
Copy link
Contributor

Choose a reason for hiding this comment

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

FYI, thanks.

Here we refer to an "Approximation with elementary function" with maximum error: 1.5×10−7 of Error function in Wikipedia as below figure:

image

and erf function of WebGL backend for TensorFlow.js.

const ERF = `
  // Error function is calculated approximately with elementary function.
  // See "Handbook of Mathematical Functions with Formulas,
  // Graphs, and Mathematical Tables", Abramowitz and Stegun.
  float p = ${backend_util.ERF_P};
  float a1 = ${backend_util.ERF_A1};
  float a2 = ${backend_util.ERF_A2};
  float a3 = ${backend_util.ERF_A3};
  float a4 = ${backend_util.ERF_A4};
  float a5 = ${backend_util.ERF_A5};

  float sign = sign(x);
  x = abs(x);
  float t = 1.0 / (1.0 + p * x);
  return sign * (1.0 - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x));
`;

*reference 2:https://github.com/tensorflow/tfjs/blob/master/tfjs-backend-webgl/src/kernels/Erf.ts
*/
const a1 = new Scalar(0.254829592);
const a2 = new Scalar(-0.284496736);
const a3 = new Scalar(1.421413741);
const a4 = new Scalar(-1.453152027);
const a5 = new Scalar(1.061405429);
const p = new Scalar(0.3275911);
const ones = new Scalar(1);
input = input.shape.length === 0 ? new Scalar(input.data) : input;
const signInput = sign(input);
input = abs(input);
const t = div(ones, add(ones, mul(p, input)));
const y = mul(add(mul(add(mul(add(mul(add(mul(a5, t), a4), t), a3), t), a2), t), a1), t);
const result = mul(signInput, sub(ones, mul(y, exp(neg(mul(input, input))))));
return result;
mei1127 marked this conversation as resolved.
Show resolved Hide resolved
};
27 changes: 27 additions & 0 deletions test/unary_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,33 @@ describe('test unary', function() {
[3, 2, 2, 1]);
});

it('erf', function() {
// erf 1D
testUnary(
'erf',
[-0.25, 0.25, 0.5, 0.75, -0.4],
[
-0.2763262613535272,
0.2763262613535272,
0.5205000163047472,
0.7111555696366565,
-0.42839242346728446,
],
[5]);
// erf 2D
testUnary(
'erf',
[
0.2, 0.3,
0.4, 0.5,
],
[
0.22270245785831588, 0.32862668272901174,
0.42839242346728446, 0.5205000163047472,
],
[2, 2]);
});

it('exp', function() {
testUnary('exp', [-1, 0, 1], [0.36787944117144233, 1, 2.718281828459045], [3]);
testUnary(
Expand Down