-
Notifications
You must be signed in to change notification settings - Fork 206
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
Generic cleanup rest of framework, activations and initializers #231
Open
JimClarke5
wants to merge
24
commits into
tensorflow:master
Choose a base branch
from
JimClarke5:Generic_cleanup_rest_of_fmwork
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.
Open
Changes from 19 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
c57a2e7
Merge pull request #3 from tensorflow/master
JimClarke5 09fc07e
Merge pull request #4 from tensorflow/master
JimClarke5 a99dcb4
Merge pull request #5 from tensorflow/master
JimClarke5 ba294ea
Merge pull request #6 from tensorflow/master
JimClarke5 04f419a
Merge pull request #7 from tensorflow/master
JimClarke5 02e7ebf
Merge pull request #8 from tensorflow/master
JimClarke5 e0c9ed8
Merge pull request #9 from tensorflow/master
JimClarke5 5b0374b
Merge pull request #10 from tensorflow/master
JimClarke5 e038bbd
Merge pull request #11 from tensorflow/master
JimClarke5 28a34dd
Clean up generics, remove generics from class and fix call method to …
JimClarke5 309b834
resynch with master, for some reason when I build on mac, the order f…
JimClarke5 def3051
Merge pull request #13 from tensorflow/master
JimClarke5 3a9ae37
Merge branch 'master' of https://github.com/JimClarke5/java into Gene…
JimClarke5 c5d37bf
Add GeLU activation present in TF 2.4
JimClarke5 11f8ac9
Fix @param<T> and reformat
JimClarke5 40a95af
Fix JavaDoc to add @param <T>
JimClarke5 d0e8de9
Refactor to add generic to base class and change signature of call me…
JimClarke5 478b78a
Add check for scalar.
JimClarke5 f53fa08
Change to accept TString value.
JimClarke5 79594da
Fix GeLU equations with separate Operands
JimClarke5 112c740
Fix Constant to handle TString properly
JimClarke5 61e6206
Added Stddev check for not less than 0.
JimClarke5 3b4b607
Fix fix fill to cast the 1 to the approriate type before the fill
JimClarke5 98df654
Code reformat
JimClarke5 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
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
122 changes: 122 additions & 0 deletions
122
tensorflow-framework/src/main/java/org/tensorflow/framework/activations/GeLU.java
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,122 @@ | ||
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
=======================================================================*/ | ||
package org.tensorflow.framework.activations; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TFloating; | ||
|
||
import static org.tensorflow.framework.utils.CastHelper.cast; | ||
|
||
/** | ||
* Applies the Gaussian error linear unit (GELU) activation function. | ||
* | ||
* <p>Gaussian error linear unit (GELU) computes {@code x * P(X <= x)}, where {@code P(X) ~ N(0, | ||
* 1)}. The (GELU) nonlinearity weights inputs by their value, rather than gates inputs by their | ||
* sign as in ReLU. if <code>approximate</code> is <code>true</code> : | ||
* | ||
* <pre> | ||
* 0.5 * x * (1 + tanh(sqrt(2 / pi) * (x + 0.044715 * x^3))) | ||
* </pre> | ||
* | ||
* <p>or, if <code>approximate</code> is <code>false</code>. | ||
* | ||
* <pre> | ||
* x * P(X <= x) = 0.5 * x * (1 + erf(x / sqrt(2))), | ||
* </pre> | ||
* | ||
* where <code>P(X) ~ N(0, 1)</code>. | ||
* | ||
* @see <a href="https://arxiv.org/abs/1606.08415">Hendrycks, Dan and Gimpel, Kevin, 2016-2020, | ||
* Gaussian Error Linear Units (GELUs)</a> | ||
*/ | ||
public class GeLU extends Activation<TFloating> { | ||
|
||
private final boolean approximate; | ||
|
||
/** | ||
* Creates a e Gaussian error linear unit (GELU) activation, with approximate set to false | ||
* | ||
* @param tf The TensorFlow ops | ||
*/ | ||
public GeLU(Ops tf) { | ||
this(tf, false); | ||
} | ||
|
||
/** | ||
* Creates a e Gaussian error linear unit (GELU) activation | ||
* | ||
* @param tf The TensorFlow ops | ||
* @param approximate indicator whether to enable approximation. | ||
*/ | ||
public GeLU(Ops tf, boolean approximate) { | ||
super(tf); | ||
this.approximate = approximate; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <U extends TFloating> Operand<U> call(Operand<U> input) { | ||
|
||
if (approximate) { | ||
/* | ||
coeff = math_ops.cast(0.044715, features.dtype) | ||
return 0.5 * features * ( | ||
1.0 + math_ops.tanh(0.7978845608028654 * | ||
(features + coeff * math_ops.pow(features, 3)))) | ||
*/ | ||
Operand<U> coeff = cast(tf, tf.constant(0.044715), input.type()); | ||
Operand<U> point5 = cast(tf, tf.constant(0.5), input.type()); | ||
Operand<U> one = cast(tf, tf.constant(1.0), input.type()); | ||
|
||
return tf.math.mul( | ||
point5, | ||
tf.math.mul( | ||
input, | ||
tf.math.add( | ||
one, | ||
tf.math.tanh( | ||
tf.math.mul( | ||
// sqrt(2.0 / PI) | ||
cast(tf, tf.constant(0.7978845608028654), input.type()), | ||
tf.math.add( | ||
input, | ||
tf.math.mul( | ||
coeff, | ||
tf.math.pow(input, cast(tf, tf.constant(3), input.type()))) // mul | ||
) // add | ||
) // mul | ||
) // tanh | ||
) // add | ||
) // mul | ||
); // mul | ||
|
||
} else { | ||
/* | ||
return 0.5 * features * (1.0 + math_ops.erf( | ||
features / math_ops.cast(1.4142135623730951, features.dtype))) | ||
*/ | ||
return tf.math.mul( | ||
cast(tf, tf.constant(0.5), input.type()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe hoist this and the one below out of the if statement and use local variables? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
tf.math.mul( | ||
input, | ||
tf.math.add( | ||
cast(tf, tf.constant(1), input.type()), | ||
tf.math.erf( | ||
tf.math.div( | ||
input, cast(tf, tf.constant(1.4142135623730951), input.type())))))); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
Why isn't this one pulled out like the others?
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.
It was mainly for debugging and keeping the parts of the equation manageable. I will change this one and add one for the constant "three".
BTW: It would be nice if we could pass a
type
totf.constant,
something liketf.constant(3, input.dtype())
to return the correct type.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.
Sounds like a good extension to have. That should be fairly straightforward.