forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReduce.cs
More file actions
74 lines (64 loc) · 2.72 KB
/
Reduce.cs
File metadata and controls
74 lines (64 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using Tensorflow.Keras.Losses;
using Tensorflow.Keras.Utils;
using static Tensorflow.Binding;
namespace Tensorflow.Keras.Metrics
{
/// <summary>
/// Encapsulates metrics that perform a reduce operation on the values.
/// </summary>
public class Reduce : Metric
{
public Reduce(string reduction, string name, TF_DataType dtype = TF_DataType.DtInvalid)
: base(name: name, dtype: dtype)
{
_reduction = reduction;
_dtype = dtype;
total = add_weight("total", initializer: tf.zeros_initializer);
if (reduction == Reduction.WEIGHTED_MEAN ||
reduction == Reduction.SUM_OVER_BATCH_SIZE)
{
count = add_weight("count", initializer: tf.zeros_initializer);
}
}
public Tensor update_state(Tensor values, Tensor sample_weight = null)
{
if (sample_weight != null)
{
(values, _, sample_weight) = losses_utils.squeeze_or_expand_dimensions(
values, sample_weight: sample_weight);
sample_weight = math_ops.cast(sample_weight, dtype: values.dtype);
values = math_ops.multiply(values, sample_weight);
}
Tensor update_total_op = null;
var value_sum = math_ops.reduce_sum(values);
tf_with(ops.control_dependencies(new[] { value_sum }), ctl =>
{
update_total_op = total.assign_add(value_sum);
});
// Exit early if the reduction doesn't have a denominator.
if (_reduction == Reduction.SUM)
return update_total_op;
// Update `count` for reductions that require a denominator.
Tensor num_values = null;
if (_reduction == Reduction.SUM_OVER_BATCH_SIZE)
num_values = math_ops.cast(array_ops.size(values), _dtype);
else if (_reduction == ReductionV2.WEIGHTED_MEAN)
{
if (sample_weight == null)
num_values = math_ops.cast(array_ops.size(values), _dtype);
else
num_values = math_ops.reduce_sum(sample_weight);
}
return tf_with(ops.control_dependencies(new[] { update_total_op }), ctl
=> count.assign_add(num_values));
}
public override Tensor result()
{
if (_reduction == Reduction.SUM)
return array_ops.identity(total.AsTensor());
else if (_reduction == Reduction.WEIGHTED_MEAN || _reduction == Reduction.SUM_OVER_BATCH_SIZE)
return math_ops.div_no_nan(total.AsTensor(), count.AsTensor());
return base.result();
}
}
}