| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9f2adcf commit 006eeaa
22 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -424,24 +424,30 @@ public static bool issubset<T>(this IEnumerable<T> subset, IEnumerable<T> src) | |||
| 424 | 424 | return true; | |
| 425 | 425 | } | |
| 426 | 426 | ||
| 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 | + | ||
| 427 | 433 | public static bool empty<T>(this Queue<T> queue) | |
| 428 | 434 | => queue.Count == 0; | |
| 429 | 435 | ||
| 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) | ||
| 431 | 437 | { | |
| 432 | 438 | if (dic.ContainsKey(key)) | |
| 433 | 439 | return dic[key]; | |
| 434 | 440 | ||
| 435 | - dic[key] = value; | ||
| 436 | - return value; | ||
| 441 | + dic[key] = defaultValue; | ||
| 442 | + return defaultValue; | ||
| 437 | 443 | } | |
| 438 | 444 | ||
| 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) | ||
| 440 | 446 | { | |
| 441 | 447 | if (dic.ContainsKey(key)) | |
| 442 | 448 | return dic[key]; | |
| 443 | 449 | ||
| 444 | - return value; | ||
| 450 | + return defaultValue; | ||
| 445 | 451 | } | |
| 446 | 452 | } | |
| 447 | 453 | } | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4,13 +4,11 @@ | |||
| 4 | 4 | ||
| 5 | 5 | namespace Tensorflow.Keras.ArgsDefinition | |
| 6 | 6 | { | |
| 7 | - public class RMSpropArgs | ||
| 7 | + public class RMSpropArgs : OptimizerV2Args | ||
| 8 | 8 | { | |
| 9 | - public float LearningRate { get; set; } = 0.001f; | ||
| 10 | 9 | public float RHO { get; set; } = 0.9f; | |
| 11 | 10 | public float Momentum { get; set; } = 0.0f; | |
| 12 | 11 | public float Epsilon { get; set; } = 1e-7f; | |
| 13 | 12 | public bool Centered { get; set; } = false; | |
| 14 | - public string Name { get; set; } = "RMSprop"; | ||
| 15 | 13 | } | |
| 16 | 14 | } | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,9 +96,37 @@ void _init_graph_network(Tensors inputs, Tensors outputs) | |||
| 96 | 96 | NodesByDepth = nodes_by_depth; | |
| 97 | 97 | _layers = layers; | |
| 98 | 98 | ||
| 99 | + // Build self.input_names and self.output_names. | ||
| 100 | + _set_output_names(); | ||
| 101 | + | ||
| 99 | 102 | ComputeTensorUsageCount(); | |
| 100 | 103 | } | |
| 101 | 104 | ||
| 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 | + | ||
| 102 | 130 | void ComputeTensorUsageCount() | |
| 103 | 131 | { | |
| 104 | 132 | var available_tensors = inputs.Select(x => x.GetHashCode()).ToList(); | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,7 +12,7 @@ public partial class Layer | |||
| 12 | 12 | { | |
| 13 | 13 | protected List<Layer> _layers = new List<Layer>(); | |
| 14 | 14 | public List<Layer> Layers => _layers; | |
| 15 | - | ||
| 15 | + | ||
| 16 | 16 | protected Layer Dense(int units, | |
| 17 | 17 | Activation activation = null, | |
| 18 | 18 | TensorShape input_shape = null) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,11 +6,19 @@ namespace Tensorflow.Keras.Engine | |||
| 6 | 6 | { | |
| 7 | 7 | public partial class Layer | |
| 8 | 8 | { | |
| 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() | ||
| 11 | 17 | { | |
| 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; | ||
| 14 | 22 | } | |
| 15 | 23 | ||
| 16 | 24 | void _set_trainable_state(Dictionary<Layer, object> trainable_state) | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments