From 2fea6adafda57d7c935a23ada881252634aa9658 Mon Sep 17 00:00:00 2001 From: Beacontownfc <89081023+Beacontownfc@users.noreply.github.com> Date: Mon, 5 Jun 2023 19:57:28 +0800 Subject: [PATCH 1/3] Update LayersApi.cs --- src/TensorFlowNET.Keras/Layers/LayersApi.cs | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.cs index 3b095bc2..9b0505e3 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.cs @@ -303,6 +303,41 @@ namespace Tensorflow.Keras.Layers Units = units, Activation = keras.activations.GetActivationFromName("linear") }); + /// + /// Just your regular densely-connected NN layer. + /// + /// Dense implements the operation: output = activation(dot(input, kernel) + bias) where activation is the + /// element-wise activation function passed as the activation argument, kernel is a weights matrix created by the layer, + /// and bias is a bias vector created by the layer (only applicable if use_bias is True). + /// + /// Positive integer, dimensionality of the output space. + /// Activation function to use. If you don't specify anything, no activation is applied (ie. "linear" activation: a(x) = x). + /// Initializer for the kernel weights matrix. + /// Boolean, whether the layer uses a bias vector. + /// Initializer for the bias vector. + /// A regularizer that applies a L1 regularization penalty for kernel. + /// A regularizer that applies a L1 regularization penalty for bias. + /// N-D tensor with shape: (batch_size, ..., input_dim). The most common situation would be a 2D input with shape (batch_size, input_dim). + /// N-D tensor with shape: (batch_size, ..., units). For instance, for a 2D input with shape (batch_size, input_dim), the output would have shape (batch_size, units). + + public ILayer Dense(int units, + string activation = null, + IInitializer kernel_initializer = null, + bool use_bias = true, + IInitializer bias_initializer = null, + IRegularizer kernel_regularizer = null, + IRegularizer bias_regularizer = null, + Shape input_shape = null) + => new Dense(new DenseArgs + { + Units = units, + Activation = keras.activations.GetActivationFromName(activation), + KernelInitializer = kernel_initializer ?? tf.glorot_uniform_initializer, + BiasInitializer = bias_initializer ?? (use_bias ? tf.zeros_initializer : null), + InputShape = input_shape, + KernelRegularizer = kernel_regularizer, + BiasRegularizer = bias_regularizer + }); /// /// Just your regular densely-connected NN layer. From d61e20fb47aae5dcfd12413af4126d1be4b63e3b Mon Sep 17 00:00:00 2001 From: Beacontownfc <89081023+Beacontownfc@users.noreply.github.com> Date: Mon, 5 Jun 2023 19:59:11 +0800 Subject: [PATCH 2/3] Update ILayersApi.cs --- src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs index 6a29f9e5..b4422fb1 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ILayersApi.cs @@ -104,6 +104,15 @@ namespace Tensorflow.Keras.Layers bool use_bias = true, IInitializer bias_initializer = null, Shape input_shape = null); + + public ILayer Dense(int units, + string activation = null, + IInitializer kernel_initializer = null, + bool use_bias = true, + IInitializer bias_initializer = null, + IRegularizer kernel_regularizer = null, + IRegularizer bias_regularizer = null, + Shape input_shape = null); public ILayer Dropout(float rate, Shape noise_shape = null, int? seed = null); From abe33a3829d75151bf94575982dc8d06a5517125 Mon Sep 17 00:00:00 2001 From: Beacontownfc <89081023+Beacontownfc@users.noreply.github.com> Date: Mon, 5 Jun 2023 20:00:01 +0800 Subject: [PATCH 3/3] Update Dense.cs --- src/TensorFlowNET.Keras/Layers/Core/Dense.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs index aa6617dd..8a1938f8 100644 --- a/src/TensorFlowNET.Keras/Layers/Core/Dense.cs +++ b/src/TensorFlowNET.Keras/Layers/Core/Dense.cs @@ -56,6 +56,7 @@ namespace Tensorflow.Keras.Layers "kernel", shape: new Shape(last_dim, args.Units), initializer: args.KernelInitializer, + regularizer: args.KernelRegularizer, dtype: DType, trainable: true); if (args.UseBias) @@ -63,6 +64,7 @@ namespace Tensorflow.Keras.Layers "bias", shape: new Shape(args.Units), initializer: args.BiasInitializer, + regularizer: args.BiasRegularizer, dtype: DType, trainable: true);