Skip to content

Commit 6f3ec0f

Browse files
authored
Add Activations (#123)
* Initial checkin of Keras Optimzers and helper classes. Fixed dependencies in pom.xml * Added static final NAME to replace hardcoded String in the create method. This allows the NAME to be used elsewhere instead of hardcoding the string. * Changed of method to use the DataType NAME attribute rather than hardcoding the string. added methods isFloating(), isInteger(), isNUmeric(), isBoolean() and isString() * Added method WriteFieldWithInitializer to output a "final static String OP_NAME" to each generated operation. * Added tf.nn.softmaxCrossEntropyWitLogits() and tf.nn.raw.softmaxCrossEntropyWitLogits() Added tf.nn.sparesSoftmaxCrossEntropyWithLogits() and tf.nn.raw.sparesSoftmaxCrossEntropyWithLogits() Added tf.nn.sigmoidCrossEntropyWithLogits() * Moved SoftmaxCrossEntropyWithLogits and SparseSoftmaxCrossEntropyWithLogits to org.tensorflow.op.nn.raw * Generated classes now have public static final String OP_NAME = "XXXXXXXX"; * Generated classes now have public static final String OP_NAME = "XXXXXXXX"; * fix dependencies for other Tensorflow Java modules * formatting fix * Fix ctors with name to properly pass the name to the the super ctor. * change asserts to IllegalArgumentException fix javadoc, fix casts * change asserts to IllegalArgumentException * Moved back to tests * Moved SoftmaxCrossEntropyWithLogits.java and SparseSoftmaxCrossEntropyWithLogits.java to nn.raw, added new versions of these to NnOps * Deleted files that are not necessary yet * Added nn.raw group for softmaxCrossEntropyWithLogits() and sparseSoftmaxCrossEntropyWithLogits() * Added nn.raw group for softmaxCrossEntropyWithLogits() and sparseSoftmaxCrossEntropyWithLogits() * Refactor NN into individual operations under org.tensorflow.op.nn. Fix JavaDoc. Change from snake case to camel case. * Refactor NN into individual operations under org.tensorflow.op.nn. Fix JavaDoc. Change from snake case to camel case. * Reformatted code * Added sub scope * Miscellaneous fixes based on review comments. * Fixed op_generator.cc to remove a spurious new line in the generated Java files for some Ops. This also resulted in new generated source that are also committed. * Changed back to non-generic Operand until we resolve how to handle generics. * Regenerated due to creation of SoftmaxCrossEntropyWithLogits.java, SigmoidCrossEntropyWithLogits.java, and SparseSoftmaxCrossEntropyWithLogits.java under package org.tensorflow.op.nn in * change snake case to camel case. format code * clean upd warning, format code * Added Adamax, Ftrl, and Nadam Optimizers. Added Optimizers enum for easy inclusion of a default optimizer. Cleaned up JavaDoc * Removed optimize classes from tensorflow-keras, moved optimizer test cases to framework. Created Tests for GradientDescent and Momentum * Fixed generics * Fixed from Unit test results * added @SuppressWarnings("unchecked") on Variable array * Added Support for evaluating TFloat16 * Add Activations * Remove no-arg CTORs * Fix Unit Tests to include positive and negative numbers on input. * Modify JavaDoc indicating Linear activation is also known as Identity activation * Changed DEFAULT values from private to public * Fixed last sum to be over 'e' instead of 'input' * Added tests for various parameter constructs. * added tests for 1D and 3D input * Change snake case to camel case * JavaDoc fixes * Add TFloating family * Add JavaDoc * Changed to TFloating where appropriate. Misc fixes to JavaDoc. In ReLU, change to assign to new variable 'lInput' rather than change the 'input' parameter. * Remove the test of int arguments for those classes changed to TFloating type, as they would no longer compile. * Remove the test of int arguments for those classes changed to TFloating type, as they would no longer compile. * Make LeakyRelu visible so that it is included in tf.nn. * Remove TNumber import * Add tf.nn.leakyRelu operation
1 parent e181d03 commit 6f3ec0f

35 files changed

+2162
-8
lines changed

tensorflow-core/tensorflow-core-api/src/bazel/api_def/api_def_LeakyRelu.pbtxt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
op {
22
graph_op_name: "LeakyRelu"
3+
visibility: VISIBLE
34
endpoint {
45
name: "nn.LeakyRelu"
56
}

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/NnOps.java

+14
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.tensorflow.op.nn.FusedResizeAndPadConv2d;
6060
import org.tensorflow.op.nn.InTopK;
6161
import org.tensorflow.op.nn.L2Loss;
62+
import org.tensorflow.op.nn.LeakyRelu;
6263
import org.tensorflow.op.nn.LearnedUnigramCandidateSampler;
6364
import org.tensorflow.op.nn.LocalResponseNormalization;
6465
import org.tensorflow.op.nn.LogSoftmax;
@@ -1226,6 +1227,19 @@ public <T extends TNumber> L2Loss<T> l2Loss(Operand<T> t) {
12261227
return L2Loss.create(scope, t);
12271228
}
12281229

1230+
/**
1231+
* Computes rectified linear: `max(features, features * alpha)`.
1232+
*
1233+
* @param <T> data type for {@code activations()} output
1234+
* @param features
1235+
* @param options carries optional attributes values
1236+
* @return a new instance of LeakyRelu
1237+
*/
1238+
public <T extends TNumber> LeakyRelu<T> leakyRelu(Operand<T> features,
1239+
LeakyRelu.Options... options) {
1240+
return LeakyRelu.create(scope, features, options);
1241+
}
1242+
12291243
/**
12301244
* Generates labels for candidate sampling with a learned unigram distribution.
12311245
* <p>

tensorflow-core/tensorflow-core-api/src/gen/java/org/tensorflow/op/nn/LeakyRelu.java

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*
3434
* @param <T> data type for {@code activations()} output
3535
*/
36+
@Operator(group = "nn")
3637
public final class LeakyRelu<T extends TNumber> extends RawOp implements Operand<T> {
3738

3839
/**

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/types/TBfloat16.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.tensorflow.ndarray.NdArray;
3131
import org.tensorflow.ndarray.StdArrays;
3232
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
33-
import org.tensorflow.types.family.TNumber;
33+
import org.tensorflow.types.family.TFloating;
3434

3535
/**
3636
* Brain 16-bit float tensor type.
@@ -48,7 +48,7 @@
4848
* <p>Note that some CPUs support the bfloat16 format natively, which can result in faster
4949
* computation compared to {@link TFloat16} when GPUs are not used.
5050
*/
51-
public interface TBfloat16 extends FloatNdArray, TNumber {
51+
public interface TBfloat16 extends FloatNdArray, TFloating {
5252
/** readable-name for the data type */
5353
static final String NAME = "BFLOAT16";
5454

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/types/TFloat16.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import org.tensorflow.ndarray.NdArray;
3131
import org.tensorflow.ndarray.StdArrays;
3232
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
33-
import org.tensorflow.types.family.TNumber;
33+
import org.tensorflow.types.family.TFloating;
3434

3535
/**
3636
* IEEE-754 half-precision 16-bit float tensor type.
@@ -45,7 +45,7 @@
4545
* most CPUs do not support this format natively. For CPU computation on 16-bit floats, the {@link
4646
* TBfloat16} tensor type might be a better option.
4747
*/
48-
public interface TFloat16 extends FloatNdArray, TNumber {
48+
public interface TFloat16 extends FloatNdArray, TFloating {
4949

5050
/** readable-name for the data type */
5151
static final String NAME = "FLOAT16";

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/types/TFloat32.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import org.tensorflow.ndarray.NdArray;
3030
import org.tensorflow.ndarray.StdArrays;
3131
import org.tensorflow.ndarray.impl.dense.FloatDenseNdArray;
32-
import org.tensorflow.types.family.TNumber;
32+
import org.tensorflow.types.family.TFloating;
3333

3434
/** IEEE-754 single-precision 32-bit float tensor type. */
35-
public interface TFloat32 extends FloatNdArray, TNumber {
35+
public interface TFloat32 extends FloatNdArray, TFloating {
3636

3737
/** readable-name for the data type */
3838
static final String NAME = "FLOAT";

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/types/TFloat64.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
import org.tensorflow.ndarray.NdArray;
3030
import org.tensorflow.ndarray.StdArrays;
3131
import org.tensorflow.ndarray.impl.dense.DoubleDenseNdArray;
32-
import org.tensorflow.types.family.TNumber;
32+
import org.tensorflow.types.family.TFloating;
33+
3334

3435
/** IEEE-754 double-precision 64-bit float tensor type. */
35-
public interface TFloat64 extends DoubleNdArray, TNumber {
36+
public interface TFloat64 extends DoubleNdArray, TFloating {
3637

3738
/** readable-name for the data type */
3839
static final String NAME = "DOUBLE";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.tensorflow.types.family;
2+
3+
/**
4+
* Marker interface for floating point tensor types.
5+
*
6+
* <p>Operations that only accepts floating point values as some of their operands enforce that the tensor
7+
* types for these operands to be bound to this interface. For example:
8+
*
9+
* <pre>{@code
10+
* TFloat32 tensor1 = TFloat32.vectorOf(1, 2, 3);
11+
* TBool tensor2 = TBool.vectorOf(true, false, true);
12+
*
13+
* Ops tf = Ops.create();
14+
* Exponential<TFloat32> exp = new Exponential<>(tf);
15+
* exp.call(tf.constant(tensor1)); // OK
16+
* exp.call(tf.constant(tensor2)); // Compilation failure
17+
* }</pre>
18+
*/
19+
public interface TFloating extends TNumber {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
=======================================================================*/
15+
package org.tensorflow.framework.activations;
16+
17+
import org.tensorflow.Operand;
18+
import org.tensorflow.op.Ops;
19+
import org.tensorflow.types.family.TNumber;
20+
21+
/**
22+
* Abstract base class for Activations
23+
*
24+
* <p><b>Note:</b> The {@link #tf} attribute must be set prior to invoking the call method. See
25+
* {@link #setTF(Ops)} and the constructor {@link #Activation(Ops)}.
26+
*
27+
* @param <T> the data type of the activation
28+
*/
29+
public abstract class Activation<T extends TNumber> {
30+
31+
/** The TensorFlow Ops */
32+
protected Ops tf;
33+
34+
/**
35+
* Creates the abstract class for an Activation
36+
*
37+
* @param tf the TensorFlow Ops
38+
*/
39+
protected Activation(Ops tf) {
40+
this.tf = tf;
41+
}
42+
43+
/**
44+
* Sets the TensorFlow Ops
45+
*
46+
* @param tf the TensorFlow Ops
47+
*/
48+
protected void setTF(Ops tf) {
49+
this.tf = tf;
50+
}
51+
52+
/**
53+
* Gets the TensorFlow Ops
54+
*
55+
* @return the TensorFlow Ops
56+
*/
57+
protected Ops getTF() {
58+
return this.tf;
59+
}
60+
61+
/**
62+
* Gets the calculation operation for the activation.
63+
*
64+
* @param input the input tensor
65+
* @return The operand for the activation
66+
*/
67+
public abstract Operand<T> call(Operand<T> input);
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
=======================================================================*/
15+
package org.tensorflow.framework.activations;
16+
17+
import org.tensorflow.DataType;
18+
import org.tensorflow.Operand;
19+
import org.tensorflow.op.Ops;
20+
import org.tensorflow.types.TBool;
21+
import org.tensorflow.types.family.TFloating;
22+
23+
/**
24+
* Exponential linear unit.
25+
*
26+
* <p>The exponential linear unit (ELU) with <code>alpha &gt; 0</code> is:
27+
*
28+
* <p><code>x</code> if <code>x &gt; 0</code> and <code>alpha * (exp(x) -
29+
* 1)</code> if <code>x &lt; 0</code>.
30+
*
31+
* <p>The ELU hyperparameter <code>alpha</code> controls the value to which an ELU saturates for
32+
* negative net inputs. ELUs diminish the vanishing gradient effect.
33+
*
34+
* <p>ELUs have negative values which pushes the mean of the activations closer to zero. Mean
35+
* activations that are closer to zero enable faster learning as they bring the gradient closer to
36+
* the natural gradient. ELUs saturate to a negative value when the argument gets smaller.
37+
* Saturation means a small derivative which decreases the variation and the information that is
38+
* propagated to the next layer.
39+
*
40+
* <p>Example Usage:
41+
*
42+
* <pre>
43+
* Operand&lt;TFloat32&gt; input = &#46;&#46;&#46;;
44+
* ELU&lt;TFloat32&gt; elu = new ELU&lt;&gt;(tf, 2.0f);
45+
* Operand&lt;TFloat32&gt; result = elu.call(input);
46+
* </pre>
47+
*
48+
* @param <T> the data type of the activation
49+
* @see <a href="https://arxiv.org/abs/1511.07289">Clevert et al, 2016, Fast and Accurate Deep
50+
* Network Learning by Exponential Linear Units (ELUs)</a>
51+
*/
52+
public class ELU<T extends TFloating> extends Activation<T> {
53+
54+
private static final double ALPHA_DEFAULT = 1.0;
55+
56+
/** A scalar, slope of negative section. */
57+
private final double alpha;
58+
59+
/**
60+
* Creates a new ELU with alpha={@link #ALPHA_DEFAULT}.
61+
*
62+
* @param tf the TensorFlow Ops
63+
*/
64+
public ELU(Ops tf) {
65+
this(tf, ALPHA_DEFAULT);
66+
}
67+
68+
/**
69+
* Creates a new ELU
70+
*
71+
* @param tf the TensorFlow Ops
72+
* @param alpha A scalar, slope of negative section. It controls the value to which an ELU
73+
* saturates for negative net inputs.
74+
*/
75+
public ELU(Ops tf, double alpha) {
76+
super(tf);
77+
this.alpha = alpha;
78+
}
79+
80+
/**
81+
* Gets the calculation operation for the activation.
82+
*
83+
* @param input the input tensor
84+
* @return The operand for the activation
85+
*/
86+
@Override
87+
public Operand<T> call(Operand<T> input) {
88+
89+
Operand<T> result = tf.nn.elu(input);
90+
if (alpha == 1.0) return result;
91+
else {
92+
DataType<T> dataType = input.asOutput().dataType();
93+
Operand<T> y = tf.math.mul(result, tf.dtypes.cast(tf.constant(alpha), dataType));
94+
Operand<TBool> cond = tf.math.greater(result, tf.dtypes.cast(tf.constant(0), dataType));
95+
return tf.select(cond, result, y);
96+
}
97+
}
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
=======================================================================*/
15+
package org.tensorflow.framework.activations;
16+
17+
import org.tensorflow.Operand;
18+
import org.tensorflow.op.Ops;
19+
import org.tensorflow.types.family.TFloating;
20+
21+
/**
22+
* Exponential activation function.
23+
*
24+
* <p>For example:
25+
*
26+
* <pre>
27+
* Operand&lt;TFloat32&gt; input = tf.constant(
28+
* new float[] {-3.0f,-1.0f, 0.0f,1.0f,3.0f});
29+
* Exponential&lt;TFloat32&gt; exp = new Exponential&lt;&gt;(tf);
30+
* Operand&lt;TFloat32&gt; result = exp.call(input);
31+
* // result is [0.04978707f, 0.36787945f, 1.f, 2.7182817f, 20.085537f]
32+
* </pre>
33+
*
34+
* @param <T> the data type of the activation
35+
*/
36+
public class Exponential<T extends TFloating> extends Activation<T> {
37+
38+
/**
39+
* Creates an Exponential activation.
40+
*
41+
* @param tf the TensorFlow Ops
42+
*/
43+
public Exponential(Ops tf) {
44+
super(tf);
45+
}
46+
47+
/**
48+
* Calculates the Exponential activation.
49+
*
50+
* @param input the input tensor
51+
* @return an Operand for the exponential activation: <code>exp(x)</code>.
52+
*/
53+
@Override
54+
public Operand<T> call(Operand<T> input) {
55+
return tf.math.exp(input);
56+
}
57+
}

0 commit comments

Comments
 (0)