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 2.9 kB

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