FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Keras model evaluate. · Liang813/TensorFlow.NET@d59db72 · GitHub

Repository navigation

Commit d59db72

Browse files
committed
Keras model evaluate.
1 parent 9d08a31 commit d59db72

5 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎src/TensorFlowNET.Keras/Engine/DataAdapters/TensorLikeDataAdapter.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public TensorLikeDataAdapter(TensorLikeDataAdapterArgs args)
2121
this.args = args;
2222
_process_tensorlike();
2323
num_samples = args.X.shape[0];
24-
var batch_size = args.BatchSize;
24+
var batch_size = args.BatchSize == -1 ? 32 : args.BatchSize;
2525
_batch_size = batch_size;
2626
_size = Convert.ToInt32(Math.Ceiling(num_samples / (batch_size + 0f)));
2727
num_full_batches = num_samples / batch_size;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

‎src/TensorFlowNET.Keras/Engine/Model.Fit.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void fit(NDArray x, NDArray y,
5454
stop_training = false;
5555
_train_counter.assign(0);
5656
bool first_step = true;
57+
Console.WriteLine($"Training...");
5758
foreach (var (epoch, iterator) in data_handler.enumerate_epochs())
5859
{
5960
// reset_metrics();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

‎tensorflowlib/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PM> Install-Package TensorFlow.NET
55
PM> Install-Package SciSharp.TensorFlow.Redist
66
```
77

8+
Add `<RuntimeIdentifier>win-x64</RuntimeIdentifier>` to a `PropertyGroup` in your `.csproj` when targeting `.NET 472`.
9+
810
### Run in Linux
911

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

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL