|
| 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 | +} |
0 commit comments