Skip to content

Commit 033fb7e

Browse files
KevinOfCathayOceania2018
authored andcommitted
Added activations
1 parent a7099db commit 033fb7e

File tree

12 files changed

+275
-19
lines changed

12 files changed

+275
-19
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.ArgsDefinition {
6+
public class SoftmaxArgs : LayerArgs {
7+
public Axis axis { get; set; } = -1;
8+
}
9+
}

src/TensorFlowNET.Keras/Layers/Activation/ELU.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ protected override void build ( Tensors inputs ) {
2424
}
2525
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
2626
Tensor output = inputs;
27-
if ( alpha != 1f ) {
28-
output = tf.where(output > 0f, output, alpha * (tf.exp(output) - 1f));
29-
}
27+
output = tf.where(output > 0f, output,
28+
tf.multiply(alpha, tf.sub(tf.exp(output), 1f)));
3029
return output;
3130
}
32-
3331
public override Shape ComputeOutputShape ( Shape input_shape ) {
3432
return input_shape;
3533
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Exponential : Layer {
10+
public Exponential ( LayerArgs args ) : base(args) {
11+
// Exponential has no args
12+
}
13+
protected override void build ( Tensors inputs ) {
14+
built = true;
15+
}
16+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
17+
Tensor output = inputs;
18+
return tf.exp(output);
19+
}
20+
public override Shape ComputeOutputShape ( Shape input_shape ) {
21+
return input_shape;
22+
}
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class HardSigmoid : Layer {
10+
public HardSigmoid ( LayerArgs args ) : base(args) {
11+
// hard sigmoid has no arguments
12+
}
13+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
14+
Tensor x = inputs;
15+
return tf.clip_by_value(
16+
tf.add(tf.multiply(x, 0.2f), 0.5f), 0f, 1f);
17+
}
18+
public override Shape ComputeOutputShape ( Shape input_shape ) {
19+
return input_shape;
20+
}
21+
}
22+
}

src/TensorFlowNET.Keras/Layers/Activation/SELU.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ protected override void build ( Tensors inputs ) {
2323
}
2424
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
2525
Tensor output = inputs;
26-
return tf.where(output > 0f, scale * output, scale * alpha * (tf.exp(output) - 1f));
26+
return tf.where(output > 0f,
27+
tf.multiply(scale, output),
28+
tf.multiply(scale, tf.multiply(alpha, tf.sub(tf.exp(output), 1f))));
2729
}
2830
public override Shape ComputeOutputShape ( Shape input_shape ) {
2931
return input_shape;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Softmax : Layer {
10+
Axis axis;
11+
public Softmax ( SoftmaxArgs args ) : base(args) {
12+
axis = args.axis;
13+
}
14+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
15+
Tensor x = inputs;
16+
Tensor e = tf.exp(tf.sub(x, tf.reduce_max(x, axis: this.axis, keepdims: true)));
17+
Tensor s = tf.reduce_sum(e, axis: this.axis, keepdims: true);
18+
return tf.div(e, s);
19+
}
20+
public override Shape ComputeOutputShape ( Shape input_shape ) {
21+
return input_shape;
22+
}
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Softplus : Layer {
10+
public Softplus ( LayerArgs args ) : base(args) {
11+
// Softplus has no arguments
12+
}
13+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
14+
Tensor x = inputs;
15+
return tf.log(
16+
tf.add(tf.exp(x), 1f));
17+
}
18+
public override Shape ComputeOutputShape ( Shape input_shape ) {
19+
return input_shape;
20+
}
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Softsign : Layer {
10+
public Softsign ( LayerArgs args ) : base(args) {
11+
// Softsign has no arguments
12+
}
13+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
14+
Tensor x = inputs;
15+
// x / (abs(x) + 1)
16+
return tf.div(x, tf.add(1f, tf.abs(x)));
17+
}
18+
public override Shape ComputeOutputShape ( Shape input_shape ) {
19+
return input_shape;
20+
}
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Swish : Layer {
10+
public Swish ( LayerArgs args ) : base(args) {
11+
// Swish has no arguments
12+
}
13+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
14+
Tensor x = inputs;
15+
16+
// x / (1 + exp(-x))
17+
return tf.div(x, (tf.add(1f, tf.exp(tf.negative(x)))));
18+
}
19+
public override Shape ComputeOutputShape ( Shape input_shape ) {
20+
return input_shape;
21+
}
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Engine;
6+
using static Tensorflow.Binding;
7+
8+
namespace Tensorflow.Keras.Layers {
9+
public class Tanh : Layer {
10+
public Tanh ( LayerArgs args ) : base(args) {
11+
// Tanh has no arguments
12+
}
13+
protected override Tensors Call ( Tensors inputs, Tensor state = null, bool? training = null ) {
14+
Tensor x = inputs;
15+
16+
return tf.tanh(x);
17+
}
18+
public override Shape ComputeOutputShape ( Shape input_shape ) {
19+
return input_shape;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)