From 6b473e4a405d3f2f45db0bc05008b4b804b0fe30 Mon Sep 17 00:00:00 2001 From: lingbai-kong Date: Mon, 29 May 2023 21:29:12 +0800 Subject: [PATCH] make `Activation` as the implementation of `IActivation` and use `IActivation` in `ActivationAdapter` to receive more generally `activation`. --- .../Keras/Activations/Activations.cs | 33 ++++++++++++++----- src/TensorFlowNET.Keras/Activations.cs | 6 ++-- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/TensorFlowNET.Core/Keras/Activations/Activations.cs b/src/TensorFlowNET.Core/Keras/Activations/Activations.cs index e24e42ff..8e54b4c6 100644 --- a/src/TensorFlowNET.Core/Keras/Activations/Activations.cs +++ b/src/TensorFlowNET.Core/Keras/Activations/Activations.cs @@ -3,11 +3,12 @@ using System; using System.Reflection; using System.Runtime.Versioning; using Tensorflow.Keras.Saving.Common; +using Tensorflow.Operations.Activation; namespace Tensorflow.Keras { [JsonConverter(typeof(CustomizedActivationJsonConverter))] - public class Activation + public class Activation : IActivation { public string Name { get; set; } /// @@ -15,7 +16,21 @@ namespace Tensorflow.Keras /// public Func ActivationFunction { get; set; } - public Tensor Apply(Tensor input, string name = null) => ActivationFunction(input, name); + /// + /// The implementation function of `IActivation` + /// + /// + /// + /// + public Tensor Activate(Tensor x, string name = null) => ActivationFunction(x, name); + + /// + /// The function for calling in LayersApi, an alias for `Activate`. + /// + /// + /// + /// + public Tensor Apply(Tensor input, string name = null) => Activate(input, name); public static implicit operator Activation(Func func) { @@ -28,23 +43,23 @@ namespace Tensorflow.Keras } /// - /// The ActivationAdapter is used to store string, Activation, and Func for Laysers Api to accept different types of activation parameters. + /// The `ActivationAdapter` is used to store the string, the `IActivation` implementation class, and the `Func` for LayersApi to accept different types of activation parameters. /// One of the properties must be specified while initializing. /// public class ActivationAdapter { /// - /// The name of activaiton function, such as `tanh`, `sigmoid`. + /// The name of the activaiton function, such as "tanh", "sigmoid". /// public string? Name { get; set; } = null; /// - /// The available Activation instance of activaiton function, such as keras.activations.Tanh, keras.activations.Sigmoid. + /// The available `IActivation` implementation class of the activaiton function, such as the `Activation` instances (keras.activations.Tanh, keras.activations.Sigmoid) and other `IActivation` implementation class. /// - public Activation? Activation { get; set; } = null; + public IActivation? Activation { get; set; } = null; /// - /// The Func definition of activation function, which can be customized. + /// The `Func` definition of the activation function, which can be customized. /// public Func? Func { get; set; } = null; @@ -53,7 +68,7 @@ namespace Tensorflow.Keras Name = name; } - public ActivationAdapter(Activation activation) + public ActivationAdapter(IActivation activation) { Activation = activation; } @@ -83,7 +98,7 @@ namespace Tensorflow.Keras public interface IActivationsApi { Activation GetActivationFromName(string name); - + Activation GetActivationFromAdapter(ActivationAdapter adapter); Activation Linear { get; } diff --git a/src/TensorFlowNET.Keras/Activations.cs b/src/TensorFlowNET.Keras/Activations.cs index 89988667..c68ce204 100644 --- a/src/TensorFlowNET.Keras/Activations.cs +++ b/src/TensorFlowNET.Keras/Activations.cs @@ -94,8 +94,8 @@ namespace Tensorflow.Keras } /// - /// Convert ActivationAdapter to Activation. - /// If more than one properties of ActivationAdapter are specified, the order of priority is `Name`, `Activation`, `Func` + /// Convert `ActivationAdapter` to `Activation`. + /// If more than one properties of `ActivationAdapter` are specified, the order of priority is `Name`, `Activation`, `Func` /// /// /// @@ -112,7 +112,7 @@ namespace Tensorflow.Keras } else if(adapter.Activation != null) { - return adapter.Activation; + return (Activation) adapter.Activation; } else if(adapter.Func != null) {