| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9e877d1 commit 77eb6f5
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,6 +54,12 @@ public Tensor inv(Tensor input, bool adjoint = false, string name = null) | |||
| 54 | 54 | public Tensor global_norm(Tensor[] t_list, string name = null) | |
| 55 | 55 | => clip_ops.global_norm(t_list, name: name); | |
| 56 | 56 | ||
| 57 | + public Tensor l2_normalize(Tensor x, | ||
| 58 | + int axis = 0, | ||
| 59 | + float epsilon = 1e-12f, | ||
| 60 | + string name = null) | ||
| 61 | + => nn_impl.l2_normalize(x, axis: axis, epsilon: constant_op.constant(epsilon), name: name); | ||
| 62 | + | ||
| 57 | 63 | public Tensor lstsq(Tensor matrix, Tensor rhs, | |
| 58 | 64 | NDArray l2_regularizer = null, bool fast = true, string name = null) | |
| 59 | 65 | => ops.matrix_solve_ls(matrix, rhs, l2_regularizer: l2_regularizer, fast: fast, name: name); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -31,6 +31,13 @@ Tensor categorical_crossentropy(Tensor y_true, Tensor y_pred, | |||
| 31 | 31 | /// <returns></returns> | |
| 32 | 32 | Tensor top_k_categorical_accuracy(Tensor y_true, Tensor y_pred, int k = 5); | |
| 33 | 33 | ||
| 34 | + /// <summary> | ||
| 35 | + /// Calculates how often predictions equal labels. | ||
| 36 | + /// </summary> | ||
| 37 | + /// <returns></returns> | ||
| 38 | + IMetricFunc Accuracy(string name = "accuracy", | ||
| 39 | + TF_DataType dtype = TF_DataType.TF_FLOAT); | ||
| 40 | + | ||
| 34 | 41 | /// <summary> | |
| 35 | 42 | /// Calculates how often predictions match binary labels. | |
| 36 | 43 | /// </summary> | |
@@ -56,6 +63,14 @@ IMetricFunc CategoricalCrossentropy(string name = "categorical_crossentropy", | |||
| 56 | 63 | IMetricFunc CategoricalAccuracy(string name = "categorical_accuracy", | |
| 57 | 64 | TF_DataType dtype = TF_DataType.TF_FLOAT); | |
| 58 | 65 | ||
| 66 | + /// <summary> | ||
| 67 | + /// Computes the cosine similarity between the labels and predictions. | ||
| 68 | + /// </summary> | ||
| 69 | + /// <returns></returns> | ||
| 70 | + IMetricFunc CosineSimilarity(string name = "cosine_similarity", | ||
| 71 | + TF_DataType dtype = TF_DataType.TF_FLOAT, | ||
| 72 | + Axis? axis = null); | ||
| 73 | + | ||
| 59 | 74 | /// <summary> | |
| 60 | 75 | /// Computes how often targets are in the top K predictions. | |
| 61 | 76 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + namespace Tensorflow.Keras.Metrics; | ||
| 2 | + | ||
| 3 | + public class Accuracy : MeanMetricWrapper | ||
| 4 | + { | ||
| 5 | + public Accuracy(string name = "accuracy", TF_DataType dtype = TF_DataType.TF_FLOAT) | ||
| 6 | + : base((yt, yp) => metrics_utils.accuracy(yt, yp), | ||
| 7 | + name: name, | ||
| 8 | + dtype: dtype) | ||
| 9 | + { | ||
| 10 | + } | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,11 @@ | |||
| 1 | + namespace Tensorflow.Keras.Metrics; | ||
| 2 | + | ||
| 3 | + public class CosineSimilarity : MeanMetricWrapper | ||
| 4 | + { | ||
| 5 | + public CosineSimilarity(string name = "cosine_similarity", TF_DataType dtype = TF_DataType.TF_FLOAT, Axis? axis = null) | ||
| 6 | + : base((yt, yp) => metrics_utils.cosine_similarity(yt, yp, axis: axis ?? -1), | ||
| 7 | + name: name, | ||
| 8 | + dtype: dtype) | ||
| 9 | + { | ||
| 10 | + } | ||
| 11 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,6 +71,9 @@ public Tensor top_k_categorical_accuracy(Tensor y_true, Tensor y_pred, int k = 5 | |||
| 71 | 71 | ); | |
| 72 | 72 | } | |
| 73 | 73 | ||
| 74 | + public IMetricFunc Accuracy(string name = "accuracy", TF_DataType dtype = TF_DataType.TF_FLOAT) | ||
| 75 | + => new Accuracy(name: name, dtype: dtype); | ||
| 76 | + | ||
| 74 | 77 | public IMetricFunc BinaryAccuracy(string name = "binary_accuracy", TF_DataType dtype = TF_DataType.TF_FLOAT, float threshold = 5) | |
| 75 | 78 | => new BinaryAccuracy(); | |
| 76 | 79 | ||
@@ -80,6 +83,9 @@ public IMetricFunc CategoricalAccuracy(string name = "categorical_accuracy", TF_ | |||
| 80 | 83 | public IMetricFunc CategoricalCrossentropy(string name = "categorical_crossentropy", TF_DataType dtype = TF_DataType.TF_FLOAT, bool from_logits = false, float label_smoothing = 0, Axis? axis = null) | |
| 81 | 84 | => new CategoricalCrossentropy(); | |
| 82 | 85 | ||
| 86 | + public IMetricFunc CosineSimilarity(string name = "cosine_similarity", TF_DataType dtype = TF_DataType.TF_FLOAT, Axis? axis = null) | ||
| 87 | + => new CosineSimilarity(name: name, dtype: dtype, axis: axis ?? -1); | ||
| 88 | + | ||
| 83 | 89 | public IMetricFunc TopKCategoricalAccuracy(int k = 5, string name = "top_k_categorical_accuracy", TF_DataType dtype = TF_DataType.TF_FLOAT) | |
| 84 | 90 | => new TopKCategoricalAccuracy(k: k, name: name, dtype: dtype); | |
| 85 | 91 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,12 +4,26 @@ namespace Tensorflow.Keras.Metrics; | |||
| 4 | 4 | ||
| 5 | 5 | public class metrics_utils | |
| 6 | 6 | { | |
| 7 | + public static Tensor accuracy(Tensor y_true, Tensor y_pred) | ||
| 8 | + { | ||
| 9 | + if (y_true.dtype != y_pred.dtype) | ||
| 10 | + y_pred = tf.cast(y_pred, y_true.dtype); | ||
| 11 | + return tf.cast(tf.equal(y_true, y_pred), keras.backend.floatx()); | ||
| 12 | + } | ||
| 13 | + | ||
| 7 | 14 | public static Tensor binary_matches(Tensor y_true, Tensor y_pred, float threshold = 0.5f) | |
| 8 | 15 | { | |
| 9 | 16 | y_pred = tf.cast(y_pred > threshold, y_pred.dtype); | |
| 10 | 17 | return tf.cast(tf.equal(y_true, y_pred), keras.backend.floatx()); | |
| 11 | 18 | } | |
| 12 | 19 | ||
| 20 | + public static Tensor cosine_similarity(Tensor y_true, Tensor y_pred, Axis? axis = null) | ||
| 21 | + { | ||
| 22 | + y_true = tf.linalg.l2_normalize(y_true, axis: axis ?? -1); | ||
| 23 | + y_pred = tf.linalg.l2_normalize(y_pred, axis: axis ?? -1); | ||
| 24 | + return tf.reduce_sum(y_true * y_pred, axis: axis ?? -1); | ||
| 25 | + } | ||
| 26 | + | ||
| 13 | 27 | /// <summary> | |
| 14 | 28 | /// Creates float Tensor, 1.0 for label-prediction match, 0.0 for mismatch. | |
| 15 | 29 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,6 +14,26 @@ namespace TensorFlowNET.Keras.UnitTest; | |||
| 14 | 14 | [TestClass] | |
| 15 | 15 | public class MetricsTest : EagerModeTestBase | |
| 16 | 16 | { | |
| 17 | + /// <summary> | ||
| 18 | + /// https://www.tensorflow.org/api_docs/python/tf/keras/metrics/Accuracy | ||
| 19 | + /// </summary> | ||
| 20 | + [TestMethod] | ||
| 21 | + public void Accuracy() | ||
| 22 | + { | ||
| 23 | + var y_true = np.array(new[,] { { 1 }, { 2 }, { 3 }, { 4 } }); | ||
| 24 | + var y_pred = np.array(new[,] { { 0f }, { 2f }, { 3f }, { 4f } }); | ||
| 25 | + var m = tf.keras.metrics.Accuracy(); | ||
| 26 | + m.update_state(y_true, y_pred); | ||
| 27 | + var r = m.result().numpy(); | ||
| 28 | + Assert.AreEqual(r, 0.75f); | ||
| 29 | + | ||
| 30 | + m.reset_states(); | ||
| 31 | + var weights = np.array(new[] { 1f, 1f, 0f, 0f }); | ||
| 32 | + m.update_state(y_true, y_pred, sample_weight: weights); | ||
| 33 | + r = m.result().numpy(); | ||
| 34 | + Assert.AreEqual(r, 0.5f); | ||
| 35 | + } | ||
| 36 | + | ||
| 17 | 37 | /// <summary> | |
| 18 | 38 | /// https://www.tensorflow.org/api_docs/python/tf/keras/metrics/BinaryAccuracy | |
| 19 | 39 | /// </summary> | |
@@ -23,14 +43,14 @@ public void BinaryAccuracy() | |||
| 23 | 43 | var y_true = np.array(new[,] { { 1 }, { 1 },{ 0 }, { 0 } }); | |
| 24 | 44 | var y_pred = np.array(new[,] { { 0.98f }, { 1f }, { 0f }, { 0.6f } }); | |
| 25 | 45 | var m = tf.keras.metrics.BinaryAccuracy(); | |
| 26 | - /*m.update_state(y_true, y_pred); | ||
| 46 | + m.update_state(y_true, y_pred); | ||
| 27 | 47 | var r = m.result().numpy(); | |
| 28 | 48 | Assert.AreEqual(r, 0.75f); | |
| 29 | 49 | ||
| 30 | - m.reset_states();*/ | ||
| 50 | + m.reset_states(); | ||
| 31 | 51 | var weights = np.array(new[] { 1f, 0f, 0f, 1f }); | |
| 32 | 52 | m.update_state(y_true, y_pred, sample_weight: weights); | |
| 33 | - var r = m.result().numpy(); | ||
| 53 | + r = m.result().numpy(); | ||
| 34 | 54 | Assert.AreEqual(r, 0.5f); | |
| 35 | 55 | } | |
| 36 | 56 | ||
@@ -74,6 +94,26 @@ public void CategoricalCrossentropy() | |||
| 74 | 94 | Assert.AreEqual(r, 1.6271976f); | |
| 75 | 95 | } | |
| 76 | 96 | ||
| 97 | + /// <summary> | ||
| 98 | + /// https://www.tensorflow.org/api_docs/python/tf/keras/metrics/CosineSimilarity | ||
| 99 | + /// </summary> | ||
| 100 | + [TestMethod] | ||
| 101 | + public void CosineSimilarity() | ||
| 102 | + { | ||
| 103 | + var y_true = np.array(new[,] { { 0, 1 }, { 1, 1 } }); | ||
| 104 | + var y_pred = np.array(new[,] { { 1f, 0f }, { 1f, 1f } }); | ||
| 105 | + var m = tf.keras.metrics.CosineSimilarity(axis: 1); | ||
| 106 | + m.update_state(y_true, y_pred); | ||
| 107 | + var r = m.result().numpy(); | ||
| 108 | + Assert.AreEqual(r, 0.49999997f); | ||
| 109 | + | ||
| 110 | + m.reset_states(); | ||
| 111 | + var weights = np.array(new[] { 0.3f, 0.7f }); | ||
| 112 | + m.update_state(y_true, y_pred, sample_weight: weights); | ||
| 113 | + r = m.result().numpy(); | ||
| 114 | + Assert.AreEqual(r, 0.6999999f); | ||
| 115 | + } | ||
| 116 | + | ||
| 77 | 117 | /// <summary> | |
| 78 | 118 | /// https://www.tensorflow.org/api_docs/python/tf/keras/metrics/TopKCategoricalAccuracy | |
| 79 | 119 | /// </summary> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments