You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

SELU.cs 1.4 kB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow.Keras.ArgsDefinition;
  5. using Tensorflow.Keras.Engine;
  6. using Tensorflow.Keras.Saving;
  7. using static Tensorflow.Binding;
  8. namespace Tensorflow.Keras.Layers {
  9. /// <summary>
  10. /// SELU Layer:
  11. /// similar to ELU, but has pre-defined alpha and scale
  12. /// </summary>
  13. public class SELU : Layer {
  14. protected const float alpha = 1.67326324f, scale = 1.05070098f;
  15. public SELU ( LayerArgs args ) : base(args) {
  16. // SELU has no arguments
  17. }
  18. public override void build(KerasShapesWrapper input_shape) {
  19. if ( alpha < 0f ) {
  20. throw new ValueError("Alpha must be a number greater than 0.");
  21. }
  22. base.build(input_shape);
  23. }
  24. protected override Tensors Call(Tensors inputs, Tensor mask = null, bool? training = null, Tensors initial_state = null, Tensors constants = null)
  25. {
  26. Tensor output = inputs;
  27. return tf.where(output > 0f,
  28. tf.multiply(scale, output),
  29. tf.multiply(scale, tf.multiply(alpha, tf.sub(tf.exp(output), 1f))));
  30. }
  31. public override Shape ComputeOutputShape ( Shape input_shape ) {
  32. return input_shape;
  33. }
  34. }
  35. }