FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add mean_absolute_error to metrics. · feliwir/TensorFlow.NET@a994a86 · GitHub

Repository navigation

Commit a994a86

Browse files
committed
Add mean_absolute_error to metrics.
1 parent 500f0c0 commit a994a86

3 files changed

Lines changed: 19 additions & 14 deletions

File tree

‎src/TensorFlowNET.Core/Tensors/tensor_util.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -589,23 +589,17 @@ public static string to_numpy_string(Tensor tensor)
589589
{
590590
return "<unprintable>";
591591
}
592+
else if (dtype == TF_DataType.TF_RESOURCE)
593+
{
594+
return "<unprintable>";
595+
}
592596

593597
var nd = tensor.numpy();
594598

595599
if (nd.size == 0)
596600
return "[]";
597601

598-
switch (dtype)
599-
{
600-
case TF_DataType.TF_STRING:
601-
return string.Join(string.Empty, nd.ToArray<byte>()
602-
.Select(x => x < 32 || x > 127 ? "\\x" + x.ToString("x") : Convert.ToChar(x).ToString()));
603-
case TF_DataType.TF_VARIANT:
604-
case TF_DataType.TF_RESOURCE:
605-
return "<unprintable>";
606-
default:
607-
return nd.ToString();
608-
}
602+
return nd.ToString();
609603
}
610604

611605
public static ParsedSliceArgs ParseSlices(Slice[] slices)

‎src/TensorFlowNET.Keras/Engine/MetricsContainer.cs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Tensorflow.Keras.Losses;
45
using Tensorflow.Keras.Metrics;
56
using static Tensorflow.KerasApi;
67

@@ -74,11 +75,15 @@ Metric _get_metric_object(string metric, Tensor y_t, Tensor y_p)
7475
metric_obj = keras.metrics.sparse_categorical_accuracy;
7576
else
7677
metric_obj = keras.metrics.categorical_accuracy;
77-
78-
return new MeanMetricWrapper(metric_obj, metric);
7978
}
79+
else if(metric == "mean_absolute_error" || metric == "mae")
80+
{
81+
metric_obj = keras.metrics.mean_absolute_error;
82+
}
83+
else
84+
throw new NotImplementedException("");
8085

81-
throw new NotImplementedException("");
86+
return new MeanMetricWrapper(metric_obj, metric);
8287
}
8388

8489
public IEnumerable<Metric> metrics

‎src/TensorFlowNET.Keras/Metrics/MetricsApi.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,11 @@ public Tensor sparse_categorical_accuracy(Tensor y_true, Tensor y_pred)
4040

4141
return math_ops.cast(math_ops.equal(y_true, y_pred), TF_DataType.TF_FLOAT);
4242
}
43+
44+
public Tensor mean_absolute_error(Tensor y_true, Tensor y_pred)
45+
{
46+
y_true = math_ops.cast(y_true, y_pred.dtype);
47+
return keras.backend.mean(math_ops.abs(y_pred - y_true), axis: -1);
48+
}
4349
}
4450
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL