Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 226
Add Activations#123
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
Uh oh!
There was an error while loading. Please reload this page.
Add Activations #123
Changes from all commits
ef0ce679c113a7824d48707a83a53d2683111cda5f9c7dfaa84f49db208b84a3913161b5a7c0ffcba0a5960cfc3d37298ac68812c6b8eb266515c2476d0fe5d2201dfab379d1889d67e515b7995a9fe378d21dd74c3cc7844f530fb8d3ac2c32fc5b171cd2fe9c31345c30a72ebefc2e7915e63ec4f679c86d09b1a670ec0cc9b9cca77a0b73091be946d1d57c5cc4ae32fe440130914c7d0477de0e61063c1f002302cc54c44c62ef29af9751943627c1126b83f94fc59e905ebbcc4fFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| op { | ||
| graph_op_name: "LeakyRelu" | ||
| visibility: VISIBLE | ||
| endpoint { | ||
| name: "nn.LeakyRelu" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| package org.tensorflow.types.family; | ||
| /** | ||
| * Marker interface for floating point tensor types. | ||
| * | ||
| * <p>Operations that only accepts floating point values as some of their operands enforce that the tensor | ||
| * types for these operands to be bound to this interface. For example: | ||
| * | ||
| * <pre>{@code | ||
| * TFloat32 tensor1 = TFloat32.vectorOf(1, 2, 3); | ||
| * TBool tensor2 = TBool.vectorOf(true, false, true); | ||
| * | ||
| * Ops tf = Ops.create(); | ||
| * Exponential<TFloat32> exp = new Exponential<>(tf); | ||
| * exp.call(tf.constant(tensor1)); // OK | ||
| * exp.call(tf.constant(tensor2)); // Compilation failure | ||
| * }</pre> | ||
| */ | ||
| public interface TFloating extends TNumber {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* 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.TNumber; | ||
| /** | ||
| * 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> { | ||
| /** The TensorFlow Ops */ | ||
| protected Ops tf; | ||
| /** | ||
| * Creates the abstract class for an Activation | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| protected Activation(Ops tf) { | ||
karllessard marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| this.tf = tf; | ||
| } | ||
| /** | ||
| * Sets the TensorFlow Ops | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| protected void setTF(Ops tf) { | ||
| this.tf = tf; | ||
| } | ||
| /** | ||
| * Gets the TensorFlow Ops | ||
| * | ||
| * @return the TensorFlow Ops | ||
| */ | ||
| protected Ops getTF() { | ||
| return this.tf; | ||
| } | ||
karllessard marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /** | ||
| * Gets the calculation operation for the activation. | ||
| * | ||
| * @param input the input tensor | ||
| * @return The operand for the activation | ||
| */ | ||
| public abstract Operand<T> call(Operand<T> input); | ||
karllessard marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* 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.DataType; | ||
| import org.tensorflow.Operand; | ||
| import org.tensorflow.op.Ops; | ||
| import org.tensorflow.types.TBool; | ||
| import org.tensorflow.types.family.TFloating; | ||
| /** | ||
| * Exponential linear unit. | ||
| * | ||
| * <p>The exponential linear unit (ELU) with <code>alpha > 0</code> is: | ||
| * | ||
| * <p><code>x</code> if <code>x > 0</code> and <code>alpha * (exp(x) - | ||
| * 1)</code> if <code>x < 0</code>. | ||
| * | ||
| * <p>The ELU hyperparameter <code>alpha</code> controls the value to which an ELU saturates for | ||
| * negative net inputs. ELUs diminish the vanishing gradient effect. | ||
| * | ||
| * <p>ELUs have negative values which pushes the mean of the activations closer to zero. Mean | ||
| * activations that are closer to zero enable faster learning as they bring the gradient closer to | ||
| * the natural gradient. ELUs saturate to a negative value when the argument gets smaller. | ||
| * Saturation means a small derivative which decreases the variation and the information that is | ||
| * propagated to the next layer. | ||
| * | ||
| * <p>Example Usage: | ||
| * | ||
| * <pre> | ||
| * Operand<TFloat32> input = ...; | ||
| * ELU<TFloat32> elu = new ELU<>(tf, 2.0f); | ||
| * Operand<TFloat32> 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> { | ||
| private static final double ALPHA_DEFAULT = 1.0; | ||
| /** A scalar, slope of negative section. */ | ||
| private final double alpha; | ||
| /** | ||
| * Creates a new ELU with alpha={@link #ALPHA_DEFAULT}. | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| public ELU(Ops tf) { | ||
| this(tf, ALPHA_DEFAULT); | ||
| } | ||
| /** | ||
| * Creates a new ELU | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| * @param alpha A scalar, slope of negative section. It controls the value to which an ELU | ||
| * saturates for negative net inputs. | ||
| */ | ||
| public ELU(Ops tf, double alpha) { | ||
| super(tf); | ||
| this.alpha = alpha; | ||
| } | ||
| /** | ||
| * Gets the calculation operation for the activation. | ||
| * | ||
| * @param input the input tensor | ||
| * @return The operand for the activation | ||
| */ | ||
| @Override | ||
| public Operand<T> call(Operand<T> input) { | ||
| Operand<T> result = tf.nn.elu(input); | ||
| if (alpha == 1.0) return result; | ||
| else { | ||
| DataType<T> dataType = input.asOutput().dataType(); | ||
| Operand<T> y = tf.math.mul(result, tf.dtypes.cast(tf.constant(alpha), dataType)); | ||
| Operand<TBool> cond = tf.math.greater(result, tf.dtypes.cast(tf.constant(0), dataType)); | ||
| return tf.select(cond, result, y); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* 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; | ||
| /** | ||
| * Exponential activation function. | ||
| * | ||
| * <p>For example: | ||
| * | ||
| * <pre> | ||
| * Operand<TFloat32> input = tf.constant( | ||
| * new float[] {-3.0f,-1.0f, 0.0f,1.0f,3.0f}); | ||
| * Exponential<TFloat32> exp = new Exponential<>(tf); | ||
| * Operand<TFloat32> 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> { | ||
| /** | ||
| * Creates an Exponential activation. | ||
| * | ||
| * @param tf the TensorFlow Ops | ||
| */ | ||
| public Exponential(Ops tf) { | ||
| super(tf); | ||
| } | ||
| /** | ||
| * Calculates the Exponential activation. | ||
| * | ||
| * @param input the input tensor | ||
| * @return an Operand for the exponential activation: <code>exp(x)</code>. | ||
| */ | ||
| @Override | ||
| public Operand<T> call(Operand<T> input) { | ||
| return tf.math.exp(input); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.