| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9d08a31 commit d59db72
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,7 +21,7 @@ public TensorLikeDataAdapter(TensorLikeDataAdapterArgs args) | |||
| 21 | 21 | this.args = args; | |
| 22 | 22 | _process_tensorlike(); | |
| 23 | 23 | num_samples = args.X.shape[0]; | |
| 24 | - var batch_size = args.BatchSize; | ||
| 24 | + var batch_size = args.BatchSize == -1 ? 32 : args.BatchSize; | ||
| 25 | 25 | _batch_size = batch_size; | |
| 26 | 26 | _size = Convert.ToInt32(Math.Ceiling(num_samples / (batch_size + 0f))); | |
| 27 | 27 | num_full_batches = num_samples / batch_size; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,84 @@ | |||
| 1 | + using NumSharp; | ||
| 2 | + using System; | ||
| 3 | + using System.Collections.Generic; | ||
| 4 | + using System.Linq; | ||
| 5 | + using Tensorflow.Keras.ArgsDefinition; | ||
| 6 | + using Tensorflow.Keras.Engine.DataAdapters; | ||
| 7 | + using static Tensorflow.Binding; | ||
| 8 | + | ||
| 9 | + namespace Tensorflow.Keras.Engine | ||
| 10 | + { | ||
| 11 | + public partial class Model | ||
| 12 | + { | ||
| 13 | + /// <summary> | ||
| 14 | + /// Returns the loss value & metrics values for the model in test mode. | ||
| 15 | + /// </summary> | ||
| 16 | + /// <param name="x"></param> | ||
| 17 | + /// <param name="y"></param> | ||
| 18 | + /// <param name="batch_size"></param> | ||
| 19 | + /// <param name="verbose"></param> | ||
| 20 | + /// <param name="steps"></param> | ||
| 21 | + /// <param name="max_queue_size"></param> | ||
| 22 | + /// <param name="workers"></param> | ||
| 23 | + /// <param name="use_multiprocessing"></param> | ||
| 24 | + /// <param name="return_dict"></param> | ||
| 25 | + public void evaluate(NDArray x, NDArray y, | ||
| 26 | + int batch_size = -1, | ||
| 27 | + int verbose = 1, | ||
| 28 | + int steps = -1, | ||
| 29 | + int max_queue_size = 10, | ||
| 30 | + int workers = 1, | ||
| 31 | + bool use_multiprocessing = false, | ||
| 32 | + bool return_dict = false) | ||
| 33 | + { | ||
| 34 | + data_handler = new DataHandler(new DataHandlerArgs | ||
| 35 | + { | ||
| 36 | + X = x, | ||
| 37 | + Y = y, | ||
| 38 | + BatchSize = batch_size, | ||
| 39 | + StepsPerEpoch = steps, | ||
| 40 | + InitialEpoch = 0, | ||
| 41 | + Epochs = 1, | ||
| 42 | + MaxQueueSize = max_queue_size, | ||
| 43 | + Workers = workers, | ||
| 44 | + UseMultiprocessing = use_multiprocessing, | ||
| 45 | + Model = this, | ||
| 46 | + StepsPerExecution = _steps_per_execution | ||
| 47 | + }); | ||
| 48 | + | ||
| 49 | + Console.WriteLine($"Testing..."); | ||
| 50 | + foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) | ||
| 51 | + { | ||
| 52 | + // reset_metrics(); | ||
| 53 | + // callbacks.on_epoch_begin(epoch) | ||
| 54 | + // data_handler.catch_stop_iteration(); | ||
| 55 | + IEnumerable<(string, Tensor)> results = null; | ||
| 56 | + foreach (var step in data_handler.steps()) | ||
| 57 | + { | ||
| 58 | + // callbacks.on_train_batch_begin(step) | ||
| 59 | + results = test_function(iterator); | ||
| 60 | + } | ||
| 61 | + Console.WriteLine($"iterator: {epoch + 1}, " + string.Join(", ", results.Select(x => $"{x.Item1}: {(float)x.Item2}"))); | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + IEnumerable<(string, Tensor)> test_function(OwnedIterator iterator) | ||
| 66 | + { | ||
| 67 | + var data = iterator.next(); | ||
| 68 | + var outputs = test_step(data[0], data[1]); | ||
| 69 | + tf_with(ops.control_dependencies(new object[0]), ctl => _test_counter.assign_add(1)); | ||
| 70 | + return outputs; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + List<(string, Tensor)> test_step(Tensor x, Tensor y) | ||
| 74 | + { | ||
| 75 | + (x, y) = data_handler.DataAdapter.Expand1d(x, y); | ||
| 76 | + var y_pred = Apply(x, is_training: false); | ||
| 77 | + var loss = compiled_loss.Call(y, y_pred); | ||
| 78 | + | ||
| 79 | + compiled_metrics.update_state(y, y_pred); | ||
| 80 | + | ||
| 81 | + return metrics.Select(x => (x.Name, x.result())).ToList(); | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,6 +54,7 @@ public void fit(NDArray x, NDArray y, | |||
| 54 | 54 | stop_training = false; | |
| 55 | 55 | _train_counter.assign(0); | |
| 56 | 56 | bool first_step = true; | |
| 57 | + Console.WriteLine($"Training..."); | ||
| 57 | 58 | foreach (var (epoch, iterator) in data_handler.enumerate_epochs()) | |
| 58 | 59 | { | |
| 59 | 60 | // reset_metrics(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + using System.Collections.Generic; | ||
| 2 | + using Tensorflow.Keras.Metrics; | ||
| 3 | + | ||
| 4 | + namespace Tensorflow.Keras.Engine | ||
| 5 | + { | ||
| 6 | + public partial class Model | ||
| 7 | + { | ||
| 8 | + public void save(string path) | ||
| 9 | + { | ||
| 10 | + | ||
| 11 | + } | ||
| 12 | + } | ||
| 13 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,8 @@ PM> Install-Package TensorFlow.NET | |||
| 5 | 5 | PM> Install-Package SciSharp.TensorFlow.Redist | |
| 6 | 6 | ``` | |
| 7 | 7 | ||
| 8 | + Add `<RuntimeIdentifier>win-x64</RuntimeIdentifier>` to a `PropertyGroup` in your `.csproj` when targeting `.NET 472`. | ||
| 9 | + | ||
| 8 | 10 | ### Run in Linux | |
| 9 | 11 | ||
| 10 | 12 | Download Linux pre-built library and unzip `libtensorflow.so` and `libtensorflow_framework.so` into current running directory. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments