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.

ELU.cs 1.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. namespace Tensorflow.Keras.Layers {
  8. /// <summary>
  9. /// ELU Layer:
  10. /// x = 0 when x > 0, x = alpha( e^x-1 ) elsewhere
  11. /// </summary>
  12. public class ELU : Layer
  13. {
  14. ELUArgs args;
  15. float alpha => args.Alpha;
  16. public ELU(ELUArgs args) : base(args)
  17. {
  18. this.args = args;
  19. }
  20. public override void build(Shape input_shape)
  21. {
  22. if (alpha < 0f)
  23. {
  24. throw new ValueError("Alpha must be a number greater than 0.");
  25. }
  26. base.build(input_shape);
  27. }
  28. protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
  29. {
  30. Tensor output = inputs;
  31. output = tf.where(output > 0f, output,
  32. tf.multiply(alpha, tf.sub(tf.exp(output), 1f)));
  33. return output;
  34. }
  35. public override Shape ComputeOutputShape(Shape input_shape)
  36. {
  37. return input_shape;
  38. }
  39. }
  40. }