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.

LogCosh.cs 974 B

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Tensorflow.Operations;
  5. using static Tensorflow.Binding;
  6. using static Tensorflow.KerasApi;
  7. namespace Tensorflow.Keras.Losses
  8. {
  9. public class LogCosh : LossFunctionWrapper, ILossFunc
  10. {
  11. public LogCosh(
  12. string reduction = null,
  13. string name = null) :
  14. base(reduction: reduction, name: name == null ? "log_cosh" : name){ }
  15. public override Tensor Apply(Tensor y_true = null, Tensor y_pred =null, bool from_logits = false, int axis = -1)
  16. {
  17. Tensor y_pred_dispatch = ops.convert_to_tensor(y_pred);
  18. Tensor y_true_cast = gen_math_ops.cast(y_true, y_pred_dispatch.dtype);
  19. Tensor x = y_pred_dispatch - y_true_cast;
  20. return gen_math_ops.mean(x + gen_nn_ops.softplus(-2.0 * x) - math_ops.cast(math_ops.log(tf.Variable(2.0)), x.dtype),
  21. ops.convert_to_tensor(-1));
  22. }
  23. }
  24. }