Skip to content

Commit 006eeaa

Browse files
committed
Add Metrics architecture.
1 parent 9f2adcf commit 006eeaa

22 files changed

Lines changed: 339 additions & 25 deletions

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,24 +424,30 @@ public static bool issubset<T>(this IEnumerable<T> subset, IEnumerable<T> src)
424424
return true;
425425
}
426426

427+
public static void extendleft<T>(this Queue<T> queue, IEnumerable<T> elements)
428+
{
429+
foreach (var element in elements.Reverse())
430+
queue.Enqueue(element);
431+
}
432+
427433
public static bool empty<T>(this Queue<T> queue)
428434
=> queue.Count == 0;
429435

430-
public static TValue SetDefault<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value)
436+
public static TValue SetDefault<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue defaultValue)
431437
{
432438
if (dic.ContainsKey(key))
433439
return dic[key];
434440

435-
dic[key] = value;
436-
return value;
441+
dic[key] = defaultValue;
442+
return defaultValue;
437443
}
438444

439-
public static TValue Get<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue value)
445+
public static TValue Get<TKey, TValue>(this Dictionary<TKey, TValue> dic, TKey key, TValue defaultValue)
440446
{
441447
if (dic.ContainsKey(key))
442448
return dic[key];
443449

444-
return value;
450+
return defaultValue;
445451
}
446452
}
447453
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.ArgsDefinition
6+
{
7+
public class OptimizerV2Args
8+
{
9+
public string Name { get; set; }
10+
public float LearningRate { get; set; } = 0.001f;
11+
public float InitialDecay { get; set; }
12+
public float ClipNorm { get; set; }
13+
public float ClipValue { get; set; }
14+
}
15+
}

src/TensorFlowNET.Core/Keras/ArgsDefinition/RMSpropArgs.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Tensorflow.Keras.ArgsDefinition
66
{
7-
public class RMSpropArgs
7+
public class RMSpropArgs : OptimizerV2Args
88
{
9-
public float LearningRate { get; set; } = 0.001f;
109
public float RHO { get; set; } = 0.9f;
1110
public float Momentum { get; set; } = 0.0f;
1211
public float Epsilon { get; set; } = 1e-7f;
1312
public bool Centered { get; set; } = false;
14-
public string Name { get; set; } = "RMSprop";
1513
}
1614
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.Engine
6+
{
7+
public class Container
8+
{
9+
protected string[] _output_names;
10+
protected bool _built;
11+
12+
public Container(string[] output_names)
13+
{
14+
_output_names = output_names;
15+
}
16+
}
17+
}

src/TensorFlowNET.Core/Keras/Engine/Functional.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,37 @@ void _init_graph_network(Tensors inputs, Tensors outputs)
9696
NodesByDepth = nodes_by_depth;
9797
_layers = layers;
9898

99+
// Build self.input_names and self.output_names.
100+
_set_output_names();
101+
99102
ComputeTensorUsageCount();
100103
}
101104

105+
/// <summary>
106+
/// Assigns unique names to the Network's outputs.
107+
/// </summary>
108+
void _set_output_names()
109+
{
110+
var uniquified = new List<string>();
111+
var output_names = new List<string>();
112+
var prefix_count = new Dictionary<string, int>();
113+
114+
foreach (var layer in _output_layers)
115+
{
116+
var proposal = layer.Name;
117+
while (output_names.Contains(proposal))
118+
{
119+
var existing_count = prefix_count.Get(layer.Name, 1);
120+
proposal = $"{layer.Name}_{existing_count}";
121+
prefix_count[layer.Name] = existing_count + 1;
122+
}
123+
output_names.add(proposal);
124+
uniquified.append(proposal);
125+
}
126+
127+
this.output_names = uniquified.ToArray();
128+
}
129+
102130
void ComputeTensorUsageCount()
103131
{
104132
var available_tensors = inputs.Select(x => x.GetHashCode()).ToList();
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.Engine
6+
{
7+
public partial class Layer
8+
{
9+
public IEnumerable<Layer> _flatten_layers(bool recursive = true, bool include_self = true)
10+
{
11+
if (include_self)
12+
yield return this;
13+
14+
var seen_object_ids = new List<int>();
15+
var deque = new Queue<Layer>(_layers);
16+
while (!deque.empty())
17+
{
18+
var layer_or_container = deque.Dequeue();
19+
var layer_or_container_id = layer_or_container.GetHashCode();
20+
if (seen_object_ids.Contains(layer_or_container_id))
21+
continue;
22+
seen_object_ids.Add(layer_or_container_id);
23+
yield return layer_or_container;
24+
if (recursive)
25+
deque.extendleft(layer_or_container._layers);
26+
}
27+
}
28+
}
29+
}

src/TensorFlowNET.Core/Keras/Engine/Layer.Layers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public partial class Layer
1212
{
1313
protected List<Layer> _layers = new List<Layer>();
1414
public List<Layer> Layers => _layers;
15-
15+
1616
protected Layer Dense(int units,
1717
Activation activation = null,
1818
TensorShape input_shape = null)

src/TensorFlowNET.Core/Keras/Engine/Layer.State.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ namespace Tensorflow.Keras.Engine
66
{
77
public partial class Layer
88
{
9-
Dictionary<Layer, object> trainable_state;
10-
Dictionary<Layer, object> _get_trainable_state()
9+
protected Dictionary<Layer, bool> trainable_state;
10+
protected Dictionary<Layer, bool> _compiled_trainable_state;
11+
12+
/// <summary>
13+
/// Get the `trainable` state of each sublayer.
14+
/// </summary>
15+
/// <returns></returns>
16+
protected Dictionary<Layer, bool> _get_trainable_state()
1117
{
12-
trainable_state = new Dictionary<Layer, object>();
13-
throw new NotImplementedException("");
18+
trainable_state = new Dictionary<Layer, bool>();
19+
foreach (var layer in _flatten_layers())
20+
trainable_state[layer] = layer.Trainable;
21+
return trainable_state;
1422
}
1523

1624
void _set_trainable_state(Dictionary<Layer, object> trainable_state)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Keras.ArgsDefinition;
5+
using Tensorflow.Keras.Losses;
6+
using Tensorflow.Keras.Metrics;
7+
8+
namespace Tensorflow.Keras.Engine
9+
{
10+
public class LossesContainer : Container
11+
{
12+
ILossFunc _user_losses;
13+
ILossFunc _losses;
14+
Mean _loss_metric;
15+
16+
public LossesContainer(ILossFunc losses, string[] output_names = null)
17+
: base(output_names)
18+
{
19+
_user_losses = losses;
20+
_losses = losses;
21+
_loss_metric = new Mean(name: "loss");
22+
_built = false;
23+
}
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow.Keras.Engine
6+
{
7+
public class MetricsContainer : Container
8+
{
9+
string[] _user_metrics;
10+
string[] _metrics;
11+
12+
public MetricsContainer(string[] metrics, string[] output_names = null)
13+
: base(output_names)
14+
{
15+
_user_metrics = metrics;
16+
_metrics = metrics;
17+
_built = false;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)