Browse Source

Keras model evaluate.

tags/v0.30
Oceania2018 4 years ago
parent
commit
d59db72396
5 changed files with 101 additions and 1 deletions
  1. +1
    -1
      src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs
  2. +84
    -0
      src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs
  3. +1
    -0
      src/TensorFlowNET.Keras/Engine/Model.Fit.cs
  4. +13
    -0
      src/TensorFlowNET.Keras/Engine/Model.Save.cs
  5. +2
    -0
      tensorflowlib/README.md

+ 1
- 1
src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs View File

@@ -21,7 +21,7 @@ namespace Tensorflow.Keras.Engine.DataAdapters
this.args = args;
_process_tensorlike();
num_samples = args.X.shape[0];
var batch_size = args.BatchSize;
var batch_size = args.BatchSize == -1 ? 32 : args.BatchSize;
_batch_size = batch_size;
_size = Convert.ToInt32(Math.Ceiling(num_samples / (batch_size + 0f)));
num_full_batches = num_samples / batch_size;


+ 84
- 0
src/TensorFlowNET.Keras/Engine/Model.Evaluate.cs View File

@@ -0,0 +1,84 @@
using NumSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine.DataAdapters;
using static Tensorflow.Binding;

namespace Tensorflow.Keras.Engine
{
public partial class Model
{
/// <summary>
/// Returns the loss value & metrics values for the model in test mode.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="batch_size"></param>
/// <param name="verbose"></param>
/// <param name="steps"></param>
/// <param name="max_queue_size"></param>
/// <param name="workers"></param>
/// <param name="use_multiprocessing"></param>
/// <param name="return_dict"></param>
public 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)
{
data_handler = new DataHandler(new DataHandlerArgs
{
X = x,
Y = y,
BatchSize = batch_size,
StepsPerEpoch = steps,
InitialEpoch = 0,
Epochs = 1,
MaxQueueSize = max_queue_size,
Workers = workers,
UseMultiprocessing = use_multiprocessing,
Model = this,
StepsPerExecution = _steps_per_execution
});

Console.WriteLine($"Testing...");
foreach (var (epoch, iterator) in data_handler.enumerate_epochs())
{
// reset_metrics();
// callbacks.on_epoch_begin(epoch)
// data_handler.catch_stop_iteration();
IEnumerable<(string, Tensor)> results = null;
foreach (var step in data_handler.steps())
{
// callbacks.on_train_batch_begin(step)
results = test_function(iterator);
}
Console.WriteLine($"iterator: {epoch + 1}, " + string.Join(", ", results.Select(x => $"{x.Item1}: {(float)x.Item2}")));
}
}

IEnumerable<(string, Tensor)> test_function(OwnedIterator iterator)
{
var data = iterator.next();
var outputs = test_step(data[0], data[1]);
tf_with(ops.control_dependencies(new object[0]), ctl => _test_counter.assign_add(1));
return outputs;
}

List<(string, Tensor)> test_step(Tensor x, Tensor y)
{
(x, y) = data_handler.DataAdapter.Expand1d(x, y);
var y_pred = Apply(x, is_training: false);
var loss = compiled_loss.Call(y, y_pred);

compiled_metrics.update_state(y, y_pred);

return metrics.Select(x => (x.Name, x.result())).ToList();
}
}
}

+ 1
- 0
src/TensorFlowNET.Keras/Engine/Model.Fit.cs View File

@@ -54,6 +54,7 @@ namespace Tensorflow.Keras.Engine
stop_training = false;
_train_counter.assign(0);
bool first_step = true;
Console.WriteLine($"Training...");
foreach (var (epoch, iterator) in data_handler.enumerate_epochs())
{
// reset_metrics();


+ 13
- 0
src/TensorFlowNET.Keras/Engine/Model.Save.cs View File

@@ -0,0 +1,13 @@
using System.Collections.Generic;
using Tensorflow.Keras.Metrics;

namespace Tensorflow.Keras.Engine
{
public partial class Model
{
public void save(string path)
{
}
}
}

+ 2
- 0
tensorflowlib/README.md View File

@@ -5,6 +5,8 @@ PM> Install-Package TensorFlow.NET
PM> Install-Package SciSharp.TensorFlow.Redist
```

Add `<RuntimeIdentifier>win-x64</RuntimeIdentifier>` to a `PropertyGroup` in your `.csproj` when targeting `.NET 472`.

### Run in Linux

Download Linux pre-built library and unzip `libtensorflow.so` and `libtensorflow_framework.so` into current running directory.


Loading…
Cancel
Save