Skip to content

Commit 6479706

Browse files
committed
added FeedDict
1 parent 08266af commit 6479706

3 files changed

Lines changed: 35 additions & 22 deletions

File tree

src/TensorFlowNET.Core/Sessions/BaseSession.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class BaseSession
1919

2020
public BaseSession(string target = "", Graph graph = null)
2121
{
22-
if(graph is null)
22+
if (graph is null)
2323
{
2424
_graph = ops.get_default_graph();
2525
}
@@ -41,9 +41,9 @@ public virtual NDArray run(object fetches, params FeedItem[] feed_dict)
4141
return _run(fetches, feed_dict);
4242
}
4343

44-
public virtual NDArray run(ITensorOrOperation[] fetches, Hashtable feed_dict = null)
44+
public virtual NDArray run(object fetches, Hashtable feed_dict = null)
4545
{
46-
var feed_items = feed_dict == null ? new FeedItem[0] :
46+
var feed_items = feed_dict == null ? new FeedItem[0] :
4747
feed_dict.Keys.OfType<object>().Select(key => new FeedItem(key, feed_dict[key])).ToArray();
4848
return _run(fetches, feed_items);
4949
}
@@ -98,16 +98,16 @@ private NDArray _run(object fetches, FeedItem[] feed_dict = null)
9898
feed_dict_tensor[subfeed_t] = (NDArray)val;
9999
break;
100100
case bool val:
101-
feed_dict_tensor[subfeed_t] = (NDArray) val;
101+
feed_dict_tensor[subfeed_t] = (NDArray)val;
102102
break;
103103
case bool[] val:
104104
feed_dict_tensor[subfeed_t] = (NDArray)val;
105105
break;
106106
default:
107107
Console.WriteLine($"can't handle data type of subfeed_val");
108108
throw new NotImplementedException("_run subfeed");
109-
}
110-
109+
}
110+
111111
feed_map[subfeed_t.name] = (subfeed_t, subfeed_val);
112112
}
113113
}
@@ -146,9 +146,9 @@ private NDArray _run(object fetches, FeedItem[] feed_dict = null)
146146
/// </returns>
147147
private NDArray[] _do_run(List<Operation> target_list, List<Tensor> fetch_list, Dictionary<object, object> feed_dict)
148148
{
149-
var feeds = feed_dict.Select(x =>
149+
var feeds = feed_dict.Select(x =>
150150
{
151-
if(x.Key is Tensor tensor)
151+
if (x.Key is Tensor tensor)
152152
{
153153
switch (x.Value)
154154
{
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Tensorflow.Sessions
7+
{
8+
public class FeedDict : Hashtable
9+
{
10+
}
11+
}

test/TensorFlowNET.Examples/TextProcess/TextClassificationTrain.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using NumSharp;
99
using Tensorflow;
1010
using Tensorflow.Keras.Engine;
11+
using Tensorflow.Sessions;
1112
using TensorFlowNET.Examples.Text.cnn_models;
1213
using TensorFlowNET.Examples.TextClassification;
1314
using TensorFlowNET.Examples.Utility;
@@ -91,7 +92,7 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
9192
foreach (var (x_batch, y_batch, total) in train_batches)
9293
{
9394
i++;
94-
var train_feed_dict = new Hashtable
95+
var train_feed_dict = new FeedDict
9596
{
9697
[model_x] = x_batch,
9798
[model_y] = y_batch,
@@ -113,25 +114,26 @@ protected virtual bool RunWithImportedGraph(Session sess, Graph graph)
113114

114115
if (step % 100 == 0)
115116
{
116-
continue;
117117
// # Test accuracy with validation data for each epoch.
118118
var valid_batches = batch_iter(valid_x, valid_y, BATCH_SIZE, 1);
119-
var (sum_accuracy, cnt) = (0, 0);
119+
var (sum_accuracy, cnt) = (0.0f, 0);
120120
foreach (var (valid_x_batch, valid_y_batch, total_validation_batches) in valid_batches)
121121
{
122-
// valid_feed_dict = {
123-
// model.x: valid_x_batch,
124-
// model.y: valid_y_batch,
125-
// model.is_training: False
126-
// }
127-
128-
// accuracy = sess.run(model.accuracy, feed_dict = valid_feed_dict)
129-
// sum_accuracy += accuracy
130-
// cnt += 1
122+
var valid_feed_dict = new FeedDict
123+
{
124+
[model_x] = valid_x_batch,
125+
[model_y] = valid_y_batch,
126+
[is_training] = false
127+
};
128+
var result1 = sess.run(accuracy, valid_feed_dict);
129+
float accuracy_value = result1;
130+
sum_accuracy += accuracy_value;
131+
cnt += 1;
131132
}
132-
// valid_accuracy = sum_accuracy / cnt
133133

134-
// print("\nValidation Accuracy = {1}\n".format(step // num_batches_per_epoch, sum_accuracy / cnt))
134+
var valid_accuracy = sum_accuracy / cnt;
135+
136+
print($"\nValidation Accuracy = {valid_accuracy}\n");
135137

136138
// # Save model
137139
// if valid_accuracy > max_accuracy:

0 commit comments

Comments
 (0)