-
Notifications
You must be signed in to change notification settings - Fork 219
Constraints - 1 #215
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
Merged
Merged
Constraints - 1 #215
Changes from all commits
Commits
Show all changes
11 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 d6b237d
Initial Checkin
JimClarke5 dc9fdb8
Clean up JavaDoc
JimClarke5 c04eeb6
Refactor Constraint to only have Generic parameter on call method.
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 hidden or 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
110 changes: 110 additions & 0 deletions
110
tensorflow-framework/src/main/java/org/tensorflow/framework/constraints/Constraint.java
This file contains hidden or 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,110 @@ | ||
/* 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.constraints; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.op.core.ReduceSum; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
import static org.tensorflow.framework.utils.CastHelper.cast; | ||
|
||
/** Base class for Constraints. Constraint subclasses impose constraints on weight values */ | ||
public abstract class Constraint { | ||
|
||
public static final float EPSILON = 1e-7f; | ||
|
||
private final Ops tf; | ||
|
||
/** | ||
* Creates a Constraint | ||
* | ||
* @param tf the TensorFlow Ops | ||
*/ | ||
public Constraint(Ops tf) { | ||
this.tf = tf; | ||
} | ||
|
||
/** | ||
* Applies the constraint against the provided weights | ||
* | ||
* @param weights the weights | ||
* @return the constrained weights | ||
*/ | ||
public abstract <T extends TNumber> Operand<T> call(Operand<T> weights); | ||
|
||
/** | ||
* Gets the TensorFlow Ops | ||
* | ||
* @return the TensorFlow Ops | ||
*/ | ||
public Ops getTF() { | ||
return tf; | ||
} | ||
|
||
/** | ||
* Gets the element-wise square root. | ||
* | ||
* @param x the input Operand. | ||
* @return the element-wise square root. | ||
* @param <T> The data type for the operand and result. | ||
* @throws IllegalArgumentException if x is null | ||
*/ | ||
protected <T extends TNumber> Operand<T> sqrt(Operand<T> x) { | ||
if (x == null) throw new IllegalArgumentException("Operand x must not be null"); | ||
Class<T> type = x.type(); | ||
Operand<T> zero = cast(tf, tf.constant(0), type); | ||
Operand<T> inf = cast(tf, tf.constant(Double.POSITIVE_INFINITY), type); | ||
return tf.math.sqrt(tf.clipByValue(x, zero, inf)); | ||
} | ||
|
||
/** | ||
* Gets the element-wise value clipping. | ||
* | ||
* @param x the Operand to clip | ||
* @param minValue the minimum value | ||
* @param maxValue the maximum value | ||
* @return the operand with clipped values | ||
* @param <T> The data type for the operand and result. | ||
* @throws IllegalArgumentException if x is null | ||
*/ | ||
protected <T extends TNumber> Operand<T> clip(Operand<T> x, double minValue, double maxValue) { | ||
if (x == null) throw new IllegalArgumentException("Operand x must not be null"); | ||
Ops tf = getTF(); | ||
Class<T> type = x.type(); | ||
|
||
double min = Math.min(minValue, maxValue); | ||
double max = Math.max(minValue, maxValue); | ||
|
||
Operand<T> minValueConstant = cast(tf, tf.constant(min), type); | ||
Operand<T> maxValueConstant = cast(tf, tf.constant(max), type); | ||
return tf.clipByValue(x, minValueConstant, maxValueConstant); | ||
} | ||
|
||
/** | ||
* Calculates the norm of the weights along the axes | ||
* | ||
* @param weights the weights used to calculate the norms | ||
* @param axes the axes along which to calculate weight norms. | ||
* @param <T> the data type for the weights and the result | ||
* @return the norms | ||
* @throws IllegalArgumentException if weights is null | ||
*/ | ||
protected <T extends TNumber> Operand<T> norm(Operand<T> weights, int[] axes) { | ||
if (weights == null) throw new IllegalArgumentException("weights must not be null"); | ||
return sqrt( | ||
tf.reduceSum(tf.math.square(weights), tf.constant(axes), ReduceSum.keepDims(Boolean.TRUE))); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
tensorflow-framework/src/main/java/org/tensorflow/framework/constraints/MaxNorm.java
This file contains hidden or 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,109 @@ | ||
/* 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.constraints; | ||
|
||
import org.tensorflow.Operand; | ||
import org.tensorflow.op.Ops; | ||
import org.tensorflow.types.family.TNumber; | ||
|
||
import static org.tensorflow.framework.utils.CastHelper.cast; | ||
|
||
/** | ||
* Constrains the weights incident to each hidden unit to have a norm less than or equal to a | ||
* desired value. | ||
*/ | ||
public class MaxNorm extends Constraint { | ||
public static final double MAX_VALUE_DEFAULT = 2.0; | ||
public static final int AXIS_DEFAULT = 0; | ||
|
||
/** the maximum norm for the incoming weights. */ | ||
private final double maxValue; | ||
/** integer, axis along which to calculate weight norms. */ | ||
private final int[] axes; | ||
|
||
/** | ||
* Create a MaxNorm constraint using {@link #MAX_VALUE_DEFAULT} for the max value and {@link | ||
* #AXIS_DEFAULT} for the axis. | ||
* | ||
* @param tf the TensorFlow Ops | ||
*/ | ||
public MaxNorm(Ops tf) { | ||
this(tf, MAX_VALUE_DEFAULT, AXIS_DEFAULT); | ||
} | ||
|
||
/** | ||
* Create a MaxNorm constraint using {@link #AXIS_DEFAULT} for the axis. | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param maxValue the maximum norm for the incoming weights. | ||
*/ | ||
public MaxNorm(Ops tf, double maxValue) { | ||
this(tf, maxValue, AXIS_DEFAULT); | ||
} | ||
|
||
/** | ||
* Create a MaxNorm constraint | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param maxValue the maximum norm for the incoming weights. | ||
* @param axis axis along which to calculate weight norms. | ||
*/ | ||
public MaxNorm(Ops tf, double maxValue, int axis) { | ||
this(tf, maxValue, new int[] {axis}); | ||
} | ||
|
||
/** | ||
* Create a MaxNorm constraint | ||
* | ||
* @param tf the TensorFlow Ops | ||
* @param maxValue the maximum norm for the incoming weights. | ||
* @param axes axes along which to calculate weight norms. | ||
*/ | ||
public MaxNorm(Ops tf, double maxValue, int[] axes) { | ||
super(tf); | ||
this.maxValue = maxValue; | ||
this.axes = axes; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public <T extends TNumber> Operand<T> call(Operand<T> weights) { | ||
Ops tf = getTF(); | ||
Class<T> type = weights.type(); | ||
Operand<T> norms = norm(weights, getAxes()); | ||
Operand<T> desired = clip(norms, 0f, this.getMaxValue()); | ||
|
||
return tf.math.mul( | ||
weights, tf.math.div(desired, tf.math.add(cast(tf, tf.constant(EPSILON), type), norms))); | ||
} | ||
|
||
/** | ||
* Gets the max value | ||
* | ||
* @return the maxValue | ||
*/ | ||
public double getMaxValue() { | ||
return maxValue; | ||
} | ||
|
||
/** | ||
* Gets the axes | ||
* | ||
* @return the axes | ||
*/ | ||
public int[] getAxes() { | ||
return axes; | ||
} | ||
} |
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.
Verify
x
notnull
as we do inclip
?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.
Fixed
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.
👍 done