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.

CosineSimilarity.Test.cs 3.0 kB

4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using NumSharp;
  3. using Tensorflow;
  4. using Tensorflow.Keras.Losses;
  5. using static Tensorflow.Binding;
  6. using static Tensorflow.KerasApi;
  7. namespace TensorFlowNET.Keras.UnitTest
  8. {
  9. [TestClass]
  10. public class CosineSimilarity
  11. {
  12. //https://keras.io/api/losses/regression_losses/
  13. NDArray y_true_float = new float[,] { { 0.0f, 1.0f }, { 1.0f, 1.0f } };
  14. NDArray y_pred_float = new float[,] { { 1.0f, 0.0f }, { 1.0f, 1.0f } };
  15. [TestMethod]
  16. public void _Default()
  17. {
  18. //>>> # Using 'auto'/'sum_over_batch_size' reduction type.
  19. //>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis = 1)
  20. //>>> # l2_norm(y_true) = [[0., 1.], [1./1.414], 1./1.414]]]
  21. //>>> # l2_norm(y_pred) = [[1., 0.], [1./1.414], 1./1.414]]]
  22. //>>> # l2_norm(y_true) . l2_norm(y_pred) = [[0., 0.], [0.5, 0.5]]
  23. //>>> # loss = mean(sum(l2_norm(y_true) . l2_norm(y_pred), axis=1))
  24. //>>> # = -((0. + 0.) + (0.5 + 0.5)) / 2
  25. //-0.5
  26. var loss = keras.losses.CosineSimilarity(axis : 1);
  27. var call = loss.Call(y_true_float, y_pred_float);
  28. Assert.AreEqual((NDArray)(-0.49999997f), call.numpy());
  29. }
  30. [TestMethod]
  31. public void _Sample_Weight()
  32. {
  33. //>>> # Calling with 'sample_weight'.
  34. //>>> cosine_loss(y_true, y_pred, sample_weight =[0.8, 0.2]).numpy()
  35. //- 0.0999
  36. var loss = keras.losses.CosineSimilarity();
  37. var call = loss.Call(y_true_float, y_pred_float, sample_weight: (NDArray)new float[] { 0.8f, 0.2f });
  38. Assert.AreEqual((NDArray) (- 0.099999994f), call.numpy());
  39. }
  40. [TestMethod]
  41. public void _SUM()
  42. {
  43. //>>> # Using 'sum' reduction type.
  44. //>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis = 1,
  45. //... reduction = tf.keras.losses.Reduction.SUM)
  46. //>>> cosine_loss(y_true, y_pred).numpy()
  47. //- 0.999
  48. var loss = keras.losses.CosineSimilarity(axis: 1,reduction : ReductionV2.SUM);
  49. var call = loss.Call(y_true_float, y_pred_float);
  50. Assert.AreEqual((NDArray)(-0.99999994f), call.numpy());
  51. }
  52. [TestMethod]
  53. public void _None()
  54. {
  55. //>>> # Using 'none' reduction type.
  56. //>>> cosine_loss = tf.keras.losses.CosineSimilarity(axis = 1,
  57. //... reduction = tf.keras.losses.Reduction.NONE)
  58. //>>> cosine_loss(y_true, y_pred).numpy()
  59. //array([-0., -0.999], dtype = float32)
  60. var loss = keras.losses.CosineSimilarity(axis :1, reduction: ReductionV2.NONE);
  61. var call = loss.Call(y_true_float, y_pred_float);
  62. Assert.AreEqual((NDArray)new float[] { -0f, -0.99999994f }, call.numpy());
  63. }
  64. }
  65. }