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

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. _buildInputShape = input_shape;
  27. built = true;
  28. }
  29. protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
  30. {
  31. Tensor output = inputs;
  32. output = tf.where(output > 0f, output,
  33. tf.multiply(alpha, tf.sub(tf.exp(output), 1f)));
  34. return output;
  35. }
  36. public override Shape ComputeOutputShape(Shape input_shape)
  37. {
  38. return input_shape;
  39. }
  40. }
  41. }