From cbf2d81d6bd8b7c5d4a51dcd892de80e414829e0 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Fri, 3 Mar 2023 20:21:12 -0600 Subject: [PATCH] ICallback --- .../Tensorflow.Console.csproj | 2 +- .../Keras/Engine/ICallback.cs | 15 +++ src/TensorFlowNET.Core/Keras/Engine/IModel.cs | 67 ++++++++++- .../Keras/Engine/IOptimizer.cs | 13 ++ src/TensorFlowNET.Core/Keras/IKerasApi.cs | 9 ++ src/TensorFlowNET.Core/Keras/Layers/ILayer.cs | 6 +- .../Tensorflow.Binding.csproj | 6 +- .../Callbacks/CallbackList.cs | 112 +++++++++--------- src/TensorFlowNET.Keras/Callbacks/History.cs | 103 ++++++++-------- .../Callbacks/ICallback.cs | 19 --- .../Callbacks/ProgbarLogger.cs | 9 +- src/TensorFlowNET.Keras/Engine/Functional.cs | 2 +- .../Engine/Model.Compile.cs | 7 +- src/TensorFlowNET.Keras/Engine/Model.Fit.cs | 2 +- src/TensorFlowNET.Keras/Engine/Model.Train.cs | 6 +- src/TensorFlowNET.Keras/Engine/Model.cs | 12 +- src/TensorFlowNET.Keras/KerasInterface.cs | 2 +- .../Optimizers/IOptimizer.cs | 6 - .../Optimizers/OptimizerV2.cs | 11 +- .../Saving/SavedModel/Save.cs | 2 +- .../Tensorflow.Keras.csproj | 8 +- .../TensorBenchmark.cs | 4 +- .../Tensorflow.Benchmark.csproj | 4 +- .../Unmanaged/StructCastBenchmark.cs | 2 +- .../TensorFlowNET.Graph.UnitTest.csproj | 4 +- test/TensorFlowNET.Keras.UnitTest/Gradient.cs | 12 +- .../Layers/ModelSaveTest.cs | 2 +- .../MultiThreadsTest.cs | 6 +- .../SaveModel/SequentialModelSave.cs | 20 ++-- .../Tensorflow.Keras.UnitTest.csproj | 4 +- .../Tensorflow.Native.UnitTest.csproj | 6 +- .../Tensorflow.Binding.UnitTest.csproj | 2 +- 32 files changed, 269 insertions(+), 216 deletions(-) create mode 100644 src/TensorFlowNET.Core/Keras/Engine/ICallback.cs create mode 100644 src/TensorFlowNET.Core/Keras/Engine/IOptimizer.cs delete mode 100644 src/TensorFlowNET.Keras/Callbacks/ICallback.cs delete mode 100644 src/TensorFlowNET.Keras/Optimizers/IOptimizer.cs diff --git a/src/TensorFlowNET.Console/Tensorflow.Console.csproj b/src/TensorFlowNET.Console/Tensorflow.Console.csproj index e66c7033..db28f905 100644 --- a/src/TensorFlowNET.Console/Tensorflow.Console.csproj +++ b/src/TensorFlowNET.Console/Tensorflow.Console.csproj @@ -20,7 +20,7 @@ - + diff --git a/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs b/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs new file mode 100644 index 00000000..1fc5fc84 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Engine/ICallback.cs @@ -0,0 +1,15 @@ +namespace Tensorflow.Keras.Engine; + +public interface ICallback +{ + Dictionary> history { get; set; } + void on_train_begin(); + void on_epoch_begin(int epoch); + void on_train_batch_begin(long step); + void on_train_batch_end(long end_step, Dictionary logs); + void on_epoch_end(int epoch, Dictionary epoch_logs); + void on_predict_begin(); + void on_predict_batch_begin(long step); + void on_predict_batch_end(long end_step, Dictionary logs); + void on_predict_end(); +} diff --git a/src/TensorFlowNET.Core/Keras/Engine/IModel.cs b/src/TensorFlowNET.Core/Keras/Engine/IModel.cs index a26d2974..8bcfcbbb 100644 --- a/src/TensorFlowNET.Core/Keras/Engine/IModel.cs +++ b/src/TensorFlowNET.Core/Keras/Engine/IModel.cs @@ -1,6 +1,65 @@ -namespace Tensorflow.Keras.Engine +using Tensorflow.Functions; +using Tensorflow.Keras.Losses; +using Tensorflow.Keras.Saving; +using Tensorflow.NumPy; + +namespace Tensorflow.Keras.Engine; + +public interface IModel : ILayer { - public interface IModel - { - } + void compile(IOptimizer optimizer = null, + ILossFunc loss = null, + string[] metrics = null); + + void compile(string optimizer, string loss, string[] metrics); + + ICallback fit(NDArray x, NDArray y, + int batch_size = -1, + int epochs = 1, + int verbose = 1, + float validation_split = 0f, + bool shuffle = true, + int initial_epoch = 0, + int max_queue_size = 10, + int workers = 1, + bool use_multiprocessing = false); + + void save(string filepath, + bool overwrite = true, + bool include_optimizer = true, + string save_format = "tf", + SaveOptions? options = null, + ConcreteFunction? signatures = null, + bool save_traces = true); + + void save_weights(string filepath, + bool overwrite = true, + string save_format = null, + object options = null); + + void load_weights(string filepath, + bool by_name = false, + bool skip_mismatch = false, + object options = null); + + void evaluate(NDArray x, NDArray y, + int batch_size = -1, + int verbose = 1, + int steps = -1, + int max_queue_size = 10, + int workers = 1, + bool use_multiprocessing = false, + bool return_dict = false); + + Tensors predict(Tensor x, + int batch_size = -1, + int verbose = 0, + int steps = -1, + int max_queue_size = 10, + int workers = 1, + bool use_multiprocessing = false); + + void summary(int line_length = -1, float[] positions = null); + + IKerasConfig get_config(); } diff --git a/src/TensorFlowNET.Core/Keras/Engine/IOptimizer.cs b/src/TensorFlowNET.Core/Keras/Engine/IOptimizer.cs new file mode 100644 index 00000000..68d6d059 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/Engine/IOptimizer.cs @@ -0,0 +1,13 @@ +namespace Tensorflow.Keras.Engine; + +public interface IOptimizer +{ + Tensor[] aggregate_gradients(IEnumerable<(Tensor, IVariableV1)> grads_and_vars); + Tensor[] clip_gradients(Tensor[] grads); + void apply_gradients((Tensor, ResourceVariable) grads_and_vars, + string name = null, + bool experimental_aggregate_gradients = true); + void apply_gradients(IEnumerable<(Tensor, ResourceVariable)> grads_and_vars, + string name = null, + bool experimental_aggregate_gradients = true); +} diff --git a/src/TensorFlowNET.Core/Keras/IKerasApi.cs b/src/TensorFlowNET.Core/Keras/IKerasApi.cs index cffd3f79..7fb3635c 100644 --- a/src/TensorFlowNET.Core/Keras/IKerasApi.cs +++ b/src/TensorFlowNET.Core/Keras/IKerasApi.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Engine; using Tensorflow.Keras.Layers; using Tensorflow.Keras.Losses; using Tensorflow.Keras.Metrics; @@ -13,5 +14,13 @@ namespace Tensorflow.Keras public ILossesApi losses { get; } public IMetricsApi metrics { get; } public IInitializersApi initializers { get; } + + /// + /// `Model` groups layers into an object with training and inference features. + /// + /// + /// + /// + public IModel Model(Tensors inputs, Tensors outputs, string name = null); } } diff --git a/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs b/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs index 20a98e3d..d2dfe8c5 100644 --- a/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs +++ b/src/TensorFlowNET.Core/Keras/Layers/ILayer.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using Tensorflow.Keras.ArgsDefinition; -using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Engine; using Tensorflow.Keras.Saving; using Tensorflow.Training; @@ -15,7 +13,7 @@ namespace Tensorflow.Keras List Layers { get; } List InboundNodes { get; } List OutboundNodes { get; } - Tensors Apply(Tensors inputs, Tensor state = null, bool is_training = false); + Tensors Apply(Tensors inputs, Tensor state = null, bool training = false); List TrainableVariables { get; } List TrainableWeights { get; } List NonTrainableWeights { get; } diff --git a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj index 8925228d..214b2777 100644 --- a/src/TensorFlowNET.Core/Tensorflow.Binding.csproj +++ b/src/TensorFlowNET.Core/Tensorflow.Binding.csproj @@ -5,7 +5,7 @@ Tensorflow.Binding Tensorflow 2.10.0 - 0.100.4 + 1.0.0 10.0 enable Haiping Chen, Meinrad Recheis, Eli Belash @@ -20,7 +20,7 @@ Google's TensorFlow full binding in .NET Standard. Building, training and infering deep learning models. https://tensorflownet.readthedocs.io - 0.100.4.0 + 1.0.0.0 tf.net 0.100.x and above are based on tensorflow native 2.10.0 @@ -38,7 +38,7 @@ https://tensorflownet.readthedocs.io tf.net 0.7x.x aligns with TensorFlow v2.7.x native library. tf.net 0.10x.x aligns with TensorFlow v2.10.x native library. - 0.100.4.0 + 1.0.0.0 LICENSE true true diff --git a/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs b/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs index 54e3780a..c64fd712 100644 --- a/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs +++ b/src/TensorFlowNET.Keras/Callbacks/CallbackList.cs @@ -1,63 +1,63 @@ using System; using System.Collections.Generic; using System.Text; +using Tensorflow.Keras.Engine; -namespace Tensorflow.Keras.Callbacks +namespace Tensorflow.Keras.Callbacks; + +public class CallbackList { - public class CallbackList - { - List callbacks = new List(); - public History History => callbacks[0] as History; - - public CallbackList(CallbackParams parameters) - { - callbacks.Add(new History(parameters)); - callbacks.Add(new ProgbarLogger(parameters)); - } - - public void on_train_begin() - { - callbacks.ForEach(x => x.on_train_begin()); - } - - public void on_epoch_begin(int epoch) - { - callbacks.ForEach(x => x.on_epoch_begin(epoch)); - } - - public void on_train_batch_begin(long step) - { - callbacks.ForEach(x => x.on_train_batch_begin(step)); - } - - public void on_train_batch_end(long end_step, Dictionary logs) - { - callbacks.ForEach(x => x.on_train_batch_end(end_step, logs)); - } - - public void on_epoch_end(int epoch, Dictionary epoch_logs) - { - callbacks.ForEach(x => x.on_epoch_end(epoch, epoch_logs)); - } - - public void on_predict_begin() - { - callbacks.ForEach(x => x.on_predict_begin()); - } - - public void on_predict_batch_begin(long step) - { - callbacks.ForEach(x => x.on_predict_batch_begin(step)); - } - - public void on_predict_batch_end(long end_step, Dictionary logs) - { - callbacks.ForEach(x => x.on_predict_batch_end(end_step, logs)); - } - - public void on_predict_end() - { - callbacks.ForEach(x => x.on_predict_end()); - } + List callbacks = new List(); + public History History => callbacks[0] as History; + + public CallbackList(CallbackParams parameters) + { + callbacks.Add(new History(parameters)); + callbacks.Add(new ProgbarLogger(parameters)); + } + + public void on_train_begin() + { + callbacks.ForEach(x => x.on_train_begin()); + } + + public void on_epoch_begin(int epoch) + { + callbacks.ForEach(x => x.on_epoch_begin(epoch)); + } + + public void on_train_batch_begin(long step) + { + callbacks.ForEach(x => x.on_train_batch_begin(step)); + } + + public void on_train_batch_end(long end_step, Dictionary logs) + { + callbacks.ForEach(x => x.on_train_batch_end(end_step, logs)); + } + + public void on_epoch_end(int epoch, Dictionary epoch_logs) + { + callbacks.ForEach(x => x.on_epoch_end(epoch, epoch_logs)); + } + + public void on_predict_begin() + { + callbacks.ForEach(x => x.on_predict_begin()); + } + + public void on_predict_batch_begin(long step) + { + callbacks.ForEach(x => x.on_predict_batch_begin(step)); + } + + public void on_predict_batch_end(long end_step, Dictionary logs) + { + callbacks.ForEach(x => x.on_predict_batch_end(end_step, logs)); + } + + public void on_predict_end() + { + callbacks.ForEach(x => x.on_predict_end()); } } diff --git a/src/TensorFlowNET.Keras/Callbacks/History.cs b/src/TensorFlowNET.Keras/Callbacks/History.cs index 89e1834b..978cfcd5 100644 --- a/src/TensorFlowNET.Keras/Callbacks/History.cs +++ b/src/TensorFlowNET.Keras/Callbacks/History.cs @@ -1,73 +1,70 @@ -using System; -using System.Collections.Generic; -using System.Text; +using Tensorflow.Keras.Engine; -namespace Tensorflow.Keras.Callbacks +namespace Tensorflow.Keras.Callbacks; + +public class History : ICallback { - public class History : ICallback + List epochs; + CallbackParams _parameters; + public Dictionary> history { get; set; } + + public History(CallbackParams parameters) { - List epochs; - CallbackParams _parameters; - public Dictionary> history { get; set; } + _parameters = parameters; + } - public History(CallbackParams parameters) - { - _parameters = parameters; - } + public void on_train_begin() + { + epochs = new List(); + history = new Dictionary>(); + } - public void on_train_begin() - { - epochs = new List(); - history = new Dictionary>(); - } + public void on_epoch_begin(int epoch) + { - public void on_epoch_begin(int epoch) - { + } - } + public void on_train_batch_begin(long step) + { + + } - public void on_train_batch_begin(long step) - { - - } + public void on_train_batch_end(long end_step, Dictionary logs) + { + } - public void on_train_batch_end(long end_step, Dictionary logs) - { - } + public void on_epoch_end(int epoch, Dictionary epoch_logs) + { + epochs.Add(epoch); - public void on_epoch_end(int epoch, Dictionary epoch_logs) + foreach (var log in epoch_logs) { - epochs.Add(epoch); - - foreach (var log in epoch_logs) + if (!history.ContainsKey(log.Key)) { - if (!history.ContainsKey(log.Key)) - { - history[log.Key] = new List(); - } - history[log.Key].Add((float)log.Value); + history[log.Key] = new List(); } + history[log.Key].Add((float)log.Value); } + } - public void on_predict_begin() - { - epochs = new List(); - history = new Dictionary>(); - } + public void on_predict_begin() + { + epochs = new List(); + history = new Dictionary>(); + } - public void on_predict_batch_begin(long step) - { - - } + public void on_predict_batch_begin(long step) + { + + } - public void on_predict_batch_end(long end_step, Dictionary logs) - { - - } + public void on_predict_batch_end(long end_step, Dictionary logs) + { + + } - public void on_predict_end() - { - - } + public void on_predict_end() + { + } } diff --git a/src/TensorFlowNET.Keras/Callbacks/ICallback.cs b/src/TensorFlowNET.Keras/Callbacks/ICallback.cs deleted file mode 100644 index 7d71ccac..00000000 --- a/src/TensorFlowNET.Keras/Callbacks/ICallback.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow.Keras.Callbacks -{ - public interface ICallback - { - void on_train_begin(); - void on_epoch_begin(int epoch); - void on_train_batch_begin(long step); - void on_train_batch_end(long end_step, Dictionary logs); - void on_epoch_end(int epoch, Dictionary epoch_logs); - void on_predict_begin(); - void on_predict_batch_begin(long step); - void on_predict_batch_end(long end_step, Dictionary logs); - void on_predict_end(); - } -} diff --git a/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs b/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs index bb18b2cb..a30c6051 100644 --- a/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs +++ b/src/TensorFlowNET.Keras/Callbacks/ProgbarLogger.cs @@ -1,8 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; +using System.Diagnostics; +using Tensorflow.Keras.Engine; namespace Tensorflow.Keras.Callbacks { @@ -13,6 +10,8 @@ namespace Tensorflow.Keras.Callbacks CallbackParams _parameters; Stopwatch _sw; + public Dictionary> history { get; set; } + public ProgbarLogger(CallbackParams parameters) { _parameters = parameters; diff --git a/src/TensorFlowNET.Keras/Engine/Functional.cs b/src/TensorFlowNET.Keras/Engine/Functional.cs index 33320101..e768bd0b 100644 --- a/src/TensorFlowNET.Keras/Engine/Functional.cs +++ b/src/TensorFlowNET.Keras/Engine/Functional.cs @@ -348,7 +348,7 @@ namespace Tensorflow.Keras.Engine var layer_inputs = node.MapArguments(tensor_dict); tf.Logger.Debug($"Depth {depth}: {node.Layer}: {node.Layer.Name}"); - var outputs = node.Layer.Apply(layer_inputs, is_training: training ?? false); + var outputs = node.Layer.Apply(layer_inputs, training: training ?? false); foreach (var output in outputs.Where(x => x != null)) tf.Logger.Information($"Depth {depth}: {node.Layer}: {node.Layer.Name} {output.shape}"); // Update tensor_dict for next or later input diff --git a/src/TensorFlowNET.Keras/Engine/Model.Compile.cs b/src/TensorFlowNET.Keras/Engine/Model.Compile.cs index 3d99129b..21f0c5b4 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Compile.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Compile.cs @@ -1,5 +1,4 @@ -using System; -using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Losses; using Tensorflow.Keras.Metrics; using Tensorflow.Keras.Optimizers; @@ -11,7 +10,7 @@ namespace Tensorflow.Keras.Engine LossesContainer compiled_loss; MetricsContainer compiled_metrics; - public void compile(OptimizerV2 optimizer = null, + public void compile(IOptimizer optimizer = null, ILossFunc loss = null, string[] metrics = null) { @@ -32,7 +31,7 @@ namespace Tensorflow.Keras.Engine _is_compiled = true; } - public void compile(OptimizerV2 optimizer = null, + public void compile(IOptimizer optimizer = null, ILossFunc loss = null, IMetricFunc[] metrics = null) { diff --git a/src/TensorFlowNET.Keras/Engine/Model.Fit.cs b/src/TensorFlowNET.Keras/Engine/Model.Fit.cs index 96685380..1ebd56d3 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Fit.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Fit.cs @@ -22,7 +22,7 @@ namespace Tensorflow.Keras.Engine /// /// /// - public History fit(NDArray x, NDArray y, + public ICallback fit(NDArray x, NDArray y, int batch_size = -1, int epochs = 1, int verbose = 1, diff --git a/src/TensorFlowNET.Keras/Engine/Model.Train.cs b/src/TensorFlowNET.Keras/Engine/Model.Train.cs index 0151d543..8d85d70d 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.Train.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.Train.cs @@ -51,11 +51,11 @@ namespace Tensorflow.Keras.Engine return dict; } - void _minimize(GradientTape tape, OptimizerV2 optimizer, Tensor loss, List trainable_variables) + void _minimize(GradientTape tape, IOptimizer optimizer, Tensor loss, List trainable_variables) { var gradients = tape.gradient(loss, trainable_variables); - gradients = optimizer._aggregate_gradients(zip(gradients, trainable_variables)); - gradients = optimizer._clip_gradients(gradients); + gradients = optimizer.aggregate_gradients(zip(gradients, trainable_variables)); + gradients = optimizer.clip_gradients(gradients); optimizer.apply_gradients(zip(gradients, trainable_variables.Select(x => x as ResourceVariable)), experimental_aggregate_gradients: false); diff --git a/src/TensorFlowNET.Keras/Engine/Model.cs b/src/TensorFlowNET.Keras/Engine/Model.cs index bbc6e829..5b3cdbff 100644 --- a/src/TensorFlowNET.Keras/Engine/Model.cs +++ b/src/TensorFlowNET.Keras/Engine/Model.cs @@ -1,13 +1,7 @@ -using System.Collections.Generic; -using System.Linq; -using Tensorflow.Keras.ArgsDefinition; -using Tensorflow.Keras.Engine.DataAdapters; +using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Losses; -using Tensorflow.Keras.Optimizers; using Tensorflow.Keras.Saving.SavedModel; using Tensorflow.Train; -using static Tensorflow.Binding; -using static Tensorflow.KerasApi; namespace Tensorflow.Keras.Engine { @@ -25,7 +19,7 @@ namespace Tensorflow.Keras.Engine #pragma warning restore CS0414 // The field 'Model._is_compiled' is assigned but its value is never used #pragma warning restore CS0108 // Member hides inherited member; missing new keyword ILossFunc loss; - OptimizerV2 optimizer; + IOptimizer optimizer; IVariableV1 _steps_per_execution; protected bool _is_graph_network; protected Tensors inputs; @@ -39,7 +33,7 @@ namespace Tensorflow.Keras.Engine public bool IsGraphNetwork => _is_graph_network; - public OptimizerV2 Optimizer + public IOptimizer Optimizer { get => optimizer; set => optimizer = value; diff --git a/src/TensorFlowNET.Keras/KerasInterface.cs b/src/TensorFlowNET.Keras/KerasInterface.cs index 2bde713c..6816e690 100644 --- a/src/TensorFlowNET.Keras/KerasInterface.cs +++ b/src/TensorFlowNET.Keras/KerasInterface.cs @@ -46,7 +46,7 @@ namespace Tensorflow.Keras /// /// /// - public Functional Model(Tensors inputs, Tensors outputs, string name = null) + public IModel Model(Tensors inputs, Tensors outputs, string name = null) => new Functional(inputs, outputs, name: name); /// diff --git a/src/TensorFlowNET.Keras/Optimizers/IOptimizer.cs b/src/TensorFlowNET.Keras/Optimizers/IOptimizer.cs deleted file mode 100644 index b6099baf..00000000 --- a/src/TensorFlowNET.Keras/Optimizers/IOptimizer.cs +++ /dev/null @@ -1,6 +0,0 @@ -namespace Tensorflow.Keras.Optimizers -{ - public interface IOptimizer - { - } -} diff --git a/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs b/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs index a52c5ada..dcd7535f 100644 --- a/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs +++ b/src/TensorFlowNET.Keras/Optimizers/OptimizerV2.cs @@ -1,10 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; using Tensorflow.Keras.Utils; using Tensorflow.Train; -using static Tensorflow.Binding; namespace Tensorflow.Keras.Optimizers { @@ -114,12 +111,12 @@ namespace Tensorflow.Keras.Optimizers }); } - public Tensor[] _aggregate_gradients(IEnumerable<(Tensor, IVariableV1)> grads_and_vars) + public Tensor[] aggregate_gradients(IEnumerable<(Tensor, IVariableV1)> grads_and_vars) { return grads_and_vars.Select(x => x.Item1).ToArray(); } - public Tensor[] _clip_gradients(Tensor[] grads) + public Tensor[] clip_gradients(Tensor[] grads) { return grads; } diff --git a/src/TensorFlowNET.Keras/Saving/SavedModel/Save.cs b/src/TensorFlowNET.Keras/Saving/SavedModel/Save.cs index 220eae4b..2d2de28b 100644 --- a/src/TensorFlowNET.Keras/Saving/SavedModel/Save.cs +++ b/src/TensorFlowNET.Keras/Saving/SavedModel/Save.cs @@ -33,7 +33,7 @@ public partial class KerasSavedModelUtils } } - OptimizerV2? orig_optimizer = null; + IOptimizer? orig_optimizer = null; if (!include_optimizer) { orig_optimizer = model.Optimizer; diff --git a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj index 104e6433..1bbb3442 100644 --- a/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj +++ b/src/TensorFlowNET.Keras/Tensorflow.Keras.csproj @@ -7,7 +7,7 @@ enable Tensorflow.Keras AnyCPU;x64 - 0.10.4 + 1.0.0 Haiping Chen Keras for .NET Apache 2.0, Haiping Chen 2023 @@ -37,8 +37,8 @@ Keras is an API designed for human beings, not machines. Keras follows best prac Git true Open.snk - 0.10.4.0 - 0.10.4.0 + 1.0.0.0 + 1.0.0.0 LICENSE Debug;Release;GPU @@ -73,7 +73,7 @@ Keras is an API designed for human beings, not machines. Keras follows best prac - + diff --git a/src/TensorFlowNet.Benchmarks/TensorBenchmark.cs b/src/TensorFlowNet.Benchmarks/TensorBenchmark.cs index c1aadd46..fa99755e 100644 --- a/src/TensorFlowNet.Benchmarks/TensorBenchmark.cs +++ b/src/TensorFlowNet.Benchmarks/TensorBenchmark.cs @@ -1,10 +1,8 @@ using BenchmarkDotNet.Attributes; -using Tensorflow; -using Tensorflow.Eager; namespace TensorFlowBenchmark { - [SimpleJob(launchCount: 1, warmupCount: 1, targetCount: 10)] + [SimpleJob(launchCount: 1, warmupCount: 1)] [MinColumn, MaxColumn, MeanColumn, MedianColumn] public class TensorBenchmark { diff --git a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj index ee0c113f..53261f80 100644 --- a/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj +++ b/src/TensorFlowNet.Benchmarks/Tensorflow.Benchmark.csproj @@ -36,8 +36,8 @@ - - + + diff --git a/src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs b/src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs index d4b0fee9..6e2b7160 100644 --- a/src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs +++ b/src/TensorFlowNet.Benchmarks/Unmanaged/StructCastBenchmark.cs @@ -16,7 +16,7 @@ namespace TensorFlowBenchmark.Unmanaged } } - [SimpleJob(launchCount: 1, warmupCount: 2, targetCount: 10)] + [SimpleJob(launchCount: 1, warmupCount: 2)] [MinColumn, MaxColumn, MeanColumn, MedianColumn] public unsafe class StructCastBenchmark { diff --git a/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj b/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj index e05d48bb..7f6f3c67 100644 --- a/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj +++ b/test/TensorFlowNET.Graph.UnitTest/TensorFlowNET.Graph.UnitTest.csproj @@ -27,11 +27,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/TensorFlowNET.Keras.UnitTest/Gradient.cs b/test/TensorFlowNET.Keras.UnitTest/Gradient.cs index 159800e1..fad8e118 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Gradient.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Gradient.cs @@ -11,17 +11,17 @@ namespace TensorFlowNET.Keras.UnitTest; [TestClass] public class GradientTest { - public Model get_actor(int num_states) + public IModel get_actor(int num_states) { - var inputs = keras.layers.Input(shape: num_states); - var outputs = keras.layers.Dense(1, activation: keras.activations.Tanh).Apply(inputs); + var inputs = tf.keras.layers.Input(shape: num_states); + var outputs = tf.keras.layers.Dense(1, activation: keras.activations.Tanh).Apply(inputs); - Model model = keras.Model(inputs, outputs); + var model = tf.keras.Model(inputs, outputs); return model; } - public Model get_critic(int num_states, int num_actions) + public IModel get_critic(int num_states, int num_actions) { // State as input var state_input = keras.layers.Input(shape: num_states); @@ -33,7 +33,7 @@ public class GradientTest var outputs = keras.layers.Dense(1).Apply(concat); - Model model = keras.Model(new Tensors(state_input, action_input), outputs); + var model = tf.keras.Model(new Tensors(state_input, action_input), outputs); model.summary(); return model; diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/ModelSaveTest.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/ModelSaveTest.cs index 67e8ff79..12bf02a5 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/ModelSaveTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/ModelSaveTest.cs @@ -22,7 +22,7 @@ namespace TensorFlowNET.Keras.UnitTest Assert.AreEqual(model.Layers.Count, new_model.Layers.Count); } - Functional GetFunctionalModel() + IModel GetFunctionalModel() { // Create a simple model. var inputs = keras.Input(shape: 32); diff --git a/test/TensorFlowNET.Keras.UnitTest/MultiThreadsTest.cs b/test/TensorFlowNET.Keras.UnitTest/MultiThreadsTest.cs index 2d487087..30454f88 100644 --- a/test/TensorFlowNET.Keras.UnitTest/MultiThreadsTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/MultiThreadsTest.cs @@ -51,7 +51,7 @@ namespace TensorFlowNET.Keras.UnitTest //Sanity check without multithreading for (int i = 0; i < 2; i++) { - Functional clone = BuildModel(); + var clone = BuildModel(); clone.load_weights(savefile); //Predict something @@ -71,7 +71,7 @@ namespace TensorFlowNET.Keras.UnitTest }); } - Functional BuildModel() + IModel BuildModel() { tf.Context.reset_context(); var inputs = keras.Input(shape: 2); @@ -81,7 +81,7 @@ namespace TensorFlowNET.Keras.UnitTest var outputs = DenseLayer.Apply(inputs); // build keras model - Functional model = keras.Model(inputs, outputs, name: Guid.NewGuid().ToString()); + var model = tf.keras.Model(inputs, outputs, name: Guid.NewGuid().ToString()); // show model summary model.summary(); diff --git a/test/TensorFlowNET.Keras.UnitTest/SaveModel/SequentialModelSave.cs b/test/TensorFlowNET.Keras.UnitTest/SaveModel/SequentialModelSave.cs index fe9b8b71..5b7c2b62 100644 --- a/test/TensorFlowNET.Keras.UnitTest/SaveModel/SequentialModelSave.cs +++ b/test/TensorFlowNET.Keras.UnitTest/SaveModel/SequentialModelSave.cs @@ -3,9 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using Tensorflow; using Tensorflow.Keras; -using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; -using Tensorflow.Keras.Layers; using Tensorflow.Keras.Losses; using Tensorflow.Keras.Optimizers; using Tensorflow.NumPy; @@ -20,14 +18,16 @@ public class SequentialModelSave [TestMethod] public void SimpleModelFromAutoCompile() { - var inputs = new KerasInterface().Input((28, 28, 1)); - var x = new Flatten(new FlattenArgs()).Apply(inputs); - x = new Dense(new DenseArgs() { Units = 100, Activation = tf.nn.relu }).Apply(x); - x = new LayersApi().Dense(units: 10).Apply(x); - var outputs = new LayersApi().Softmax(axis: 1).Apply(x); - var model = new KerasInterface().Model(inputs, outputs); - - model.compile(new Adam(0.001f), new LossesApi().SparseCategoricalCrossentropy(), new string[] { "accuracy" }); + var inputs = tf.keras.layers.Input((28, 28, 1)); + var x = tf.keras.layers.Flatten().Apply(inputs); + x = tf.keras.layers.Dense(100, activation: tf.nn.relu).Apply(x); + x = tf.keras.layers.Dense(units: 10).Apply(x); + var outputs = tf.keras.layers.Softmax(axis: 1).Apply(x); + var model = tf.keras.Model(inputs, outputs); + + model.compile(new Adam(0.001f), + tf.keras.losses.SparseCategoricalCrossentropy(), + new string[] { "accuracy" }); var data_loader = new MnistModelLoader(); var num_epochs = 1; diff --git a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj index bcd52c22..7d519bf6 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj +++ b/test/TensorFlowNET.Keras.UnitTest/Tensorflow.Keras.UnitTest.csproj @@ -16,11 +16,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj index d7af0376..23d41d1d 100644 --- a/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj +++ b/test/TensorFlowNET.Native.UnitTest/Tensorflow.Native.UnitTest.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -47,11 +47,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj index 56c212d0..faf3188d 100644 --- a/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj +++ b/test/TensorFlowNET.UnitTest/Tensorflow.Binding.UnitTest.csproj @@ -51,7 +51,7 @@ - +