Skip to content

Generic cleanup rest of framework, activations and initializers #231

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

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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 Oct 8, 2020
09fc07e
Merge pull request #4 from tensorflow/master
JimClarke5 Oct 27, 2020
a99dcb4
Merge pull request #5 from tensorflow/master
JimClarke5 Nov 17, 2020
ba294ea
Merge pull request #6 from tensorflow/master
JimClarke5 Nov 19, 2020
04f419a
Merge pull request #7 from tensorflow/master
JimClarke5 Dec 30, 2020
02e7ebf
Merge pull request #8 from tensorflow/master
JimClarke5 Jan 29, 2021
e0c9ed8
Merge pull request #9 from tensorflow/master
JimClarke5 Feb 1, 2021
5b0374b
Merge pull request #10 from tensorflow/master
JimClarke5 Feb 11, 2021
e038bbd
Merge pull request #11 from tensorflow/master
JimClarke5 Feb 23, 2021
28a34dd
Clean up generics, remove generics from class and fix call method to …
JimClarke5 Mar 3, 2021
309b834
resynch with master, for some reason when I build on mac, the order f…
JimClarke5 Mar 3, 2021
def3051
Merge pull request #13 from tensorflow/master
JimClarke5 Mar 3, 2021
3a9ae37
Merge branch 'master' of https://github.com/JimClarke5/java into Gene…
JimClarke5 Mar 3, 2021
c5d37bf
Add GeLU activation present in TF 2.4
JimClarke5 Mar 4, 2021
11f8ac9
Fix @param<T> and reformat
JimClarke5 Mar 4, 2021
40a95af
Fix JavaDoc to add @param <T>
JimClarke5 Mar 6, 2021
d0e8de9
Refactor to add generic to base class and change signature of call me…
JimClarke5 Mar 6, 2021
478b78a
Add check for scalar.
JimClarke5 Mar 6, 2021
f53fa08
Change to accept TString value.
JimClarke5 Mar 7, 2021
79594da
Fix GeLU equations with separate Operands
JimClarke5 Mar 9, 2021
112c740
Fix Constant to handle TString properly
JimClarke5 Mar 9, 2021
61e6206
Added Stddev check for not less than 0.
JimClarke5 Mar 9, 2021
3b4b607
Fix fix fill to cast the 1 to the approriate type before the fill
JimClarke5 Mar 9, 2021
98df654
Code reformat
JimClarke5 Mar 9, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@

/**
* Abstract base class for Activations
*
* <p><b>Note:</b> The {@link #tf} attribute must be set prior to invoking the call method. See
* {@link #setTF(Ops)} and the constructor {@link #Activation(Ops)}.
*
* @param <T> the data type of the activation
*/
public abstract class Activation<T extends TNumber> {
public abstract class Activation {

/** The TensorFlow Ops */
protected Ops tf;
Expand All @@ -41,28 +36,29 @@ protected Activation(Ops tf) {
}

/**
* Sets the TensorFlow Ops
* Gets the TensorFlow Ops
*
* @param tf the TensorFlow Ops
* @return the TensorFlow Ops
*/
protected void setTF(Ops tf) {
this.tf = tf;
protected Ops getTF() {
return this.tf;
}

/**
* Gets the TensorFlow Ops
* Sets the TensorFlow Ops
*
* @return the TensorFlow Ops
* @param tf the TensorFlow Ops
*/
protected Ops getTF() {
return this.tf;
protected void setTF(Ops tf) {
this.tf = tf;
}

/**
* Gets the calculation operation for the activation.
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the input and result
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you reorder these tags so that the @param are all before @return? I think the generic parameter should be the first one, I'm not sure if there is a standard for this. Or, if you prefer, I would also be totally comfortable not documenting at all the generic parameter of these method, which is quite explicit. Your choice.

This comment applies to other places in that PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did find this document from Oracle, How to Write Doc Comments for the Javadoc Tool.
@param should be before @return, but it doesn't mention anything about ordering the generic within the params.
It just says Multiple @param tags should be listed in argument-declaration order. This makes it easier to visually match the list to the declaration.

I will fix by moving the @param<T> to before the @return

Copy link
Collaborator

Choose a reason for hiding this comment

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

Javadoc will warn on any missing parameters, generic or otherwise, so I think it's better to have all parameters documented.

*/
public abstract Operand<T> call(Operand<T> input);
public abstract <T extends TNumber> Operand<T> call(Operand<T> input);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.TBool;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Exponential linear unit.
Expand All @@ -44,11 +44,10 @@
* Operand&lt;TFloat32&gt; result = elu.call(input);
* </pre>
*
* @param <T> the data type of the activation
* @see <a href="https://arxiv.org/abs/1511.07289">Clevert et al, 2016, Fast and Accurate Deep
* Network Learning by Exponential Linear Units (ELUs)</a>
*/
public class ELU<T extends TFloating> extends Activation<T> {
public class ELU extends Activation {

private static final double ALPHA_DEFAULT = 1.0;

Expand Down Expand Up @@ -83,11 +82,12 @@ public ELU(Ops tf, double alpha) {
* @return The operand for the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {

Operand<T> result = tf.nn.elu(input);
if (alpha == 1.0) return result;
else {
if (alpha == 1.0) {
return result;
} else {
Class<T> inputType = input.type();
Operand<T> y = tf.math.mul(result, tf.dtypes.cast(tf.constant(alpha), inputType));
Operand<TBool> cond = tf.math.greater(result, tf.dtypes.cast(tf.constant(0), inputType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Exponential activation function.
Expand All @@ -30,10 +30,8 @@
* Operand&lt;TFloat32&gt; result = exp.call(input);
* // result is [0.04978707f, 0.36787945f, 1.f, 2.7182817f, 20.085537f]
* </pre>
*
* @param <T> the data type of the activation
*/
public class Exponential<T extends TFloating> extends Activation<T> {
public class Exponential extends Activation {

/**
* Creates an Exponential activation.
Expand All @@ -49,9 +47,10 @@ public Exponential(Ops tf) {
*
* @param input the input tensor
* @return an Operand for the exponential activation: <code>exp(x)</code>.
* @param <T> the data type of the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return tf.math.exp(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Hard sigmoid activation.
Expand All @@ -40,10 +40,8 @@
* Operand&lt;TFloat32&gt; result = hardSigmoid.call(input);
* // result is [0.f , 0.3f, 0.5f, 0.7f, 1.f]
* </pre>
*
* @param <T> the data type of the result
*/
public class HardSigmoid<T extends TFloating> extends Activation<T> {
public class HardSigmoid extends Activation {

/**
* Creates Hard sigmoid activation.
Expand All @@ -59,9 +57,10 @@ public HardSigmoid(Ops tf) {
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the result
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
Class<T> inputType = input.type();
Operand<T> point2 = tf.dtypes.cast(tf.constant(0.2), inputType);
Operand<T> point5 = tf.dtypes.cast(tf.constant(0.5), inputType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import org.tensorflow.types.family.TNumber;

/**
* Linear activation function (pass-through).
* Linear activation function (pass-through).
*
* <p>The linear activation returns its input. It is also known as the Identity activation function.</p>
* <p>The linear activation returns its input. It is also known as the Identity activation function.
*
* <p>For example:
*
Expand All @@ -33,7 +33,7 @@
* // result is [-3.0f,-1.0f, 0.0f,1.0f,3.0f]
* </pre>
*/
public class Linear<U extends TNumber> extends Activation<U> {
public class Linear extends Activation {

/**
* Creates a linear activation.
Expand All @@ -46,7 +46,7 @@ public Linear(Ops tf) {

/** {@inheritDoc} */
@Override
public Operand<U> call(Operand<U> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return input;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@
* result = relu.call(input);
* // result is [-0.f, -0.f, 0.f, 0.f, 10.f]
* </pre>
*
* @param <T> the data type of the result
*/
public class ReLU<T extends TNumber> extends Activation<T> {
public class ReLU extends Activation {

public static final float ALPHA_DEFAULT = 0.0f;
public static final float MAX_VALUE_DEFAULT = Float.NaN;
Expand Down Expand Up @@ -96,7 +94,7 @@ public ReLU(Ops tf, float alpha, float maxValue, float threshold) {

/** {@inheritDoc} */
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
Class<T> inputType = input.type();

boolean clipMax = !Float.isNaN(maxValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Scaled Exponential Linear Unit (SELU).
Expand All @@ -42,10 +42,9 @@
* <p><b>Notes: </b> To be used together with the {@link
* org.tensorflow.framework.initializers.LeCun} initializer with Normal Distribution.
*
* @param <T> the data type of the activation
* @see <a href="https://arxiv.org/abs/1706.02515">Klambauer et al., 2017</a>
*/
public class SELU<T extends TFloating> extends Activation<T> {
public class SELU extends Activation {

/**
* Creates a Scaled Exponential Linear Unit (SELU) activation.
Expand All @@ -61,9 +60,10 @@ public SELU(Ops tf) {
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return tf.nn.selu(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Sigmoid activation. <code>sigmoid(x) = 1 / (1 + exp(-x))</code>.
Expand All @@ -38,10 +38,8 @@
* // result is [2.0611537e-09f, 2.6894143e-01f,
* // 5.0000000e-01f,7.3105860e-01f, 1.f]
* </pre>
*
* @param <T> the data type of the activation
*/
public class Sigmoid<T extends TFloating> extends Activation<T> {
public class Sigmoid extends Activation {

/**
* Creates a Sigmoid activation.
Expand All @@ -57,9 +55,10 @@ public Sigmoid(Ops tf) {
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return tf.math.sigmoid(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.ReduceMax;
import org.tensorflow.op.core.ReduceSum;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Softmax converts a real vector to a vector of categorical probabilities.
Expand All @@ -35,10 +35,8 @@
* <p>The softmax of each vector x is computed as: <code>exp(x) / tf.sum(exp(x))</code>.
*
* <p>The input values in are the log-odds of the resulting probability.
*
* @param <T> the data type of the activation
*/
public class Softmax<T extends TFloating> extends Activation<T> {
public class Softmax extends Activation {

private static final int AXIS_DEFAULT = -1;

Expand Down Expand Up @@ -70,9 +68,10 @@ public Softmax(Ops tf, int axis) {
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
Shape shape = input.shape();
int numDimensions = shape.numDimensions();
if (numDimensions == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Softplus activation function, <code>softplus(x) = log(exp(x) + 1)</code>.
Expand All @@ -32,7 +32,7 @@
* // 1.3132616e+00f, 2.0000000e+01f]
* </pre>
*/
public class Softplus<T extends TFloating> extends Activation<T> {
public class Softplus extends Activation {

/**
* Creates a Softplus activation function.
Expand All @@ -50,7 +50,7 @@ public Softplus(Ops tf) {
* @return The operand for the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return tf.math.softplus(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Softsign activation function, <code>softsign(x) = x / (abs(x) + 1)</code>.
Expand All @@ -30,10 +30,8 @@
* Operand&lt;TFloat32&gt; result = softsign.call(input);
* // result is [-0.5f, 0.f, 0.5f]
* </pre>
*
* @param <T> the data type of the activation
*/
public class Softsign<T extends TFloating> extends Activation<T> {
public class Softsign extends Activation {

/**
* Creates a Softsign activation.
Expand All @@ -49,9 +47,10 @@ public Softsign(Ops tf) {
*
* @param input the input tensor
* @return The operand for the activation
* @param <T> the data type of the activation
*/
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {
return tf.nn.softsign(input);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import org.tensorflow.Operand;
import org.tensorflow.op.Ops;
import org.tensorflow.types.family.TFloating;
import org.tensorflow.types.family.TNumber;

/**
* Swish activation function. <code>swish(x) = x * sigmoid(x)</code>.
Expand All @@ -37,10 +37,9 @@
*
* </pre>
*
* @param <T> the data type of the activation
* @see <a href="https://arxiv.org/abs/1710.05941">Ramachandran et al., 2017</a>
*/
public class Swish<T extends TFloating> extends Activation<T> {
public class Swish extends Activation {

/**
* Creates a Swish activation, <code>swish(x) = x * sigmoid(x)</code>.
Expand All @@ -57,7 +56,7 @@ public Swish(Ops tf) {

/** {@inheritDoc} */
@Override
public Operand<T> call(Operand<T> input) {
public <T extends TNumber> Operand<T> call(Operand<T> input) {

// TODO Python Keras returns a "grad", which is an optimization not implemented in Java.
return tf.math.mul(input, tf.math.sigmoid(input));
Expand Down
Loading