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.

MeanSquaredLogarithmicError.Test.cs 2.6 kB

4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Tensorflow.Keras.Losses;
  3. using Tensorflow.NumPy;
  4. using static Tensorflow.KerasApi;
  5. namespace Tensorflow.Keras.UnitTest.Layers
  6. {
  7. [TestClass]
  8. public class MeanSquaredLogarithmicError
  9. {
  10. //https://keras.io/api/losses/regression_losses/
  11. NDArray y_true_float = new float[,] { { 0.0f, 1.0f }, { 0.0f, 0.0f } };
  12. NDArray y_pred_float = new float[,] { { 1.0f, 1.0f }, { 1.0f, 0.0f } };
  13. [TestMethod]
  14. public void _Default()
  15. {
  16. //>>> # Using 'auto'/'sum_over_batch_size' reduction type.
  17. //>>> msle = tf.keras.losses.MeanSquaredLogarithmicError()
  18. //>>> msle(y_true, y_pred).numpy()
  19. //0.240
  20. var loss = keras.losses.MeanSquaredLogarithmicError();
  21. var call = loss.Call(y_true_float, y_pred_float);
  22. Assert.AreEqual((NDArray)(0.24022643f), call.numpy());
  23. }
  24. [TestMethod]
  25. public void _Sample_Weight()
  26. {
  27. //>>> # Calling with 'sample_weight'.
  28. //>>> msle(y_true, y_pred, sample_weight =[0.7, 0.3]).numpy()
  29. //0.120
  30. var loss = keras.losses.MeanSquaredLogarithmicError();
  31. var call = loss.Call(y_true_float, y_pred_float, sample_weight: (NDArray)new float[] { 0.7f, 0.3f });
  32. Assert.AreEqual((NDArray)(0.12011322f), call.numpy());
  33. }
  34. [TestMethod]
  35. public void _SUM()
  36. {
  37. //>>> # Using 'sum' reduction type.
  38. //>>> msle = tf.keras.losses.MeanSquaredLogarithmicError(
  39. //... reduction = tf.keras.losses.Reduction.SUM)
  40. //>>> msle(y_true, y_pred).numpy()
  41. //0.480
  42. var loss = keras.losses.MeanSquaredLogarithmicError(reduction: ReductionV2.SUM);
  43. var call = loss.Call(y_true_float, y_pred_float);
  44. Assert.AreEqual((NDArray)(0.48045287f), call.numpy());
  45. }
  46. [TestMethod]
  47. public void _None()
  48. {
  49. //>>> # Using 'none' reduction type.
  50. //>>> msle = tf.keras.losses.MeanSquaredLogarithmicError(
  51. //... reduction = tf.keras.losses.Reduction.NONE)
  52. //>>> msle(y_true, y_pred).numpy()
  53. //array([0.240, 0.240], dtype = float32)
  54. var loss = keras.losses.MeanSquaredLogarithmicError(reduction: ReductionV2.NONE);
  55. var call = loss.Call(y_true_float, y_pred_float);
  56. Assert.AreEqual((NDArray)new float[] { 0.24022643f, 0.24022643f }, call.numpy());
  57. }
  58. }
  59. }