Skip to content

Commit 625368a

Browse files
committed
fix result of session.run
1 parent 476f8cf commit 625368a

17 files changed

Lines changed: 106 additions & 44 deletions

src/TensorFlowNET.Core/Sessions/_ElementFetchMapper.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,23 @@ public override NDArray build_results(List<object> values)
4343
case NDArray value:
4444
result = value;
4545
break;
46-
case float fVal:
47-
result = fVal;
46+
case short value:
47+
result = value;
48+
break;
49+
case int value:
50+
result = value;
51+
break;
52+
case long value:
53+
result = value;
54+
break;
55+
case float value:
56+
result = value;
57+
break;
58+
case double value:
59+
result = value;
60+
break;
61+
case string value:
62+
result = value;
4863
break;
4964
default:
5065
break;

src/TensorFlowNET.Core/Sessions/_FetchHandler.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public NDArray build_results(BaseSession session, NDArray[] tensor_values)
5858
{
5959
var value = tensor_values[j];
6060
j += 1;
61-
if (value.ndim == 2)
62-
{
63-
full_values.Add(value[0]);
64-
}
65-
else
61+
if (value.ndim == 0)
6662
{
6763
switch (value.dtype.Name)
6864
{
@@ -75,8 +71,15 @@ public NDArray build_results(BaseSession session, NDArray[] tensor_values)
7571
case "Double":
7672
full_values.Add(value.Data<double>(0));
7773
break;
74+
case "String":
75+
full_values.Add(value.Data<string>(0));
76+
break;
7877
}
7978
}
79+
else
80+
{
81+
full_values.Add(value[np.arange(1)]);
82+
}
8083
}
8184
i += 1;
8285
}

test/TensorFlowNET.Examples/BasicEagerApi.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ namespace TensorFlowNET.Examples
1111
/// </summary>
1212
public class BasicEagerApi : IExample
1313
{
14+
public bool Enabled => false;
15+
1416
private Tensor a, b, c, d;
1517

16-
public void Run()
18+
public bool Run()
1719
{
1820
// Set Eager API
1921
Console.WriteLine("Setting Eager mode...");
@@ -34,6 +36,8 @@ public void Run()
3436
Console.WriteLine($"a * b = {d}");
3537

3638
// Full compatibility with Numpy
39+
40+
return true;
3741
}
3842

3943
public void PrepareData()

test/TensorFlowNET.Examples/BasicOperations.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ namespace TensorFlowNET.Examples
1010
/// Basic Operations example using TensorFlow library.
1111
/// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/basic_operations.py
1212
/// </summary>
13-
public class BasicOperations : IExample
13+
public class BasicOperations : Python, IExample
1414
{
15+
public bool Enabled => true;
1516
private Session sess;
1617

17-
public void Run()
18+
public bool Run()
1819
{
1920
// Basic constant operations
2021
// The value returned by the constructor represents the output
@@ -86,15 +87,12 @@ public void Run()
8687
// graph: the two constants and matmul.
8788
//
8889
// The output of the op is returned in 'result' as a numpy `ndarray` object.
89-
using (sess = tf.Session())
90+
return with(tf.Session(), sess =>
9091
{
9192
var result = sess.run(product);
9293
Console.WriteLine(result.ToString()); // ==> [[ 12.]]
93-
if (result.Data<int>()[0] != 12)
94-
{
95-
throw new ValueError("BasicOperations");
96-
}
97-
}
94+
return result.Data<int>()[0] == 12;
95+
});
9896
}
9997

10098
public void PrepareData()

test/TensorFlowNET.Examples/HelloWorld.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ namespace TensorFlowNET.Examples
99
/// Simple hello world using TensorFlow
1010
/// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/1_Introduction/helloworld.py
1111
/// </summary>
12-
public class HelloWorld : IExample
12+
public class HelloWorld : Python, IExample
1313
{
14-
public void Run()
14+
public bool Enabled => true;
15+
public bool Run()
1516
{
1617
/* Create a Constant op
1718
The op is added as a node to the default graph.
@@ -22,16 +23,13 @@ of the Constant op. */
2223
var hello = tf.constant(str);
2324

2425
// Start tf session
25-
using (var sess = tf.Session())
26+
return with(tf.Session(), sess =>
2627
{
2728
// Run the op
2829
var result = sess.run(hello);
2930
Console.WriteLine(result.ToString());
30-
if(!result.ToString().Equals(str))
31-
{
32-
throw new ValueError("HelloWorld example acts in unexpected way.");
33-
}
34-
}
31+
return result.ToString().Equals(str);
32+
});
3533
}
3634

3735
public void PrepareData()

test/TensorFlowNET.Examples/IExample.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace TensorFlowNET.Examples
1010
/// </summary>
1111
public interface IExample
1212
{
13-
void Run();
13+
bool Enabled { get; }
14+
bool Run();
1415
void PrepareData();
1516
}
1617
}

test/TensorFlowNET.Examples/ImageRecognition.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ namespace TensorFlowNET.Examples
1212
{
1313
public class ImageRecognition : Python, IExample
1414
{
15+
public bool Enabled => true;
16+
1517
string dir = "ImageRecognition";
1618
string pbFile = "tensorflow_inception_graph.pb";
1719
string labelFile = "imagenet_comp_graph_label_strings.txt";
1820
string picFile = "grace_hopper.jpg";
1921

20-
public void Run()
22+
public bool Run()
2123
{
2224
PrepareData();
2325

@@ -54,7 +56,10 @@ public void Run()
5456
});
5557

5658
Console.WriteLine($"{picFile}: {labels[idx]} {propability}");
59+
return labels[idx].Equals("military uniform");
5760
}
61+
62+
return false;
5863
}
5964

6065
private NDArray ReadTensorFromImageFile(string file_name,

test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace TensorFlowNET.Examples
1919
/// </summary>
2020
public class InceptionArchGoogLeNet : Python, IExample
2121
{
22+
public bool Enabled => false;
2223
string dir = "label_image_data";
2324
string pbFile = "inception_v3_2016_08_28_frozen.pb";
2425
string labelFile = "imagenet_slim_labels.txt";
@@ -30,7 +31,7 @@ public class InceptionArchGoogLeNet : Python, IExample
3031
string input_name = "import/input";
3132
string output_name = "import/InceptionV3/Predictions/Reshape_1";
3233

33-
public void Run()
34+
public bool Run()
3435
{
3536
PrepareData();
3637

@@ -60,6 +61,8 @@ public void Run()
6061

6162
foreach (float idx in top_k)
6263
Console.WriteLine($"{picFile}: {idx} {labels[(int)idx]}, {results[(int)idx]}");
64+
65+
return true;
6366
}
6467

6568
private NDArray ReadTensorFromImageFile(string file_name,

test/TensorFlowNET.Examples/LinearRegression.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace TensorFlowNET.Examples
1212
/// </summary>
1313
public class LinearRegression : Python, IExample
1414
{
15+
public bool Enabled => true;
16+
1517
NumPyRandom rng = np.random;
1618

1719
// Parameters
@@ -22,7 +24,7 @@ public class LinearRegression : Python, IExample
2224
NDArray train_X, train_Y;
2325
int n_samples;
2426

25-
public void Run()
27+
public bool Run()
2628
{
2729
// Training Data
2830
PrepareData();
@@ -52,7 +54,7 @@ public void Run()
5254
var init = tf.global_variables_initializer();
5355

5456
// Start training
55-
with(tf.Session(), sess =>
57+
return with(tf.Session(), sess =>
5658
{
5759
// Run the initializer
5860
sess.run(init);
@@ -91,7 +93,10 @@ public void Run()
9193
new FeedItem(X, test_X),
9294
new FeedItem(Y, test_Y));
9395
Console.WriteLine($"Testing cost={testing_cost}");
94-
Console.WriteLine($"Absolute mean square loss difference: {Math.Abs((float)training_cost - (float)testing_cost)}");
96+
var diff = Math.Abs((float)training_cost - (float)testing_cost);
97+
Console.WriteLine($"Absolute mean square loss difference: {diff}");
98+
99+
return diff < 0.01;
95100
});
96101
}
97102

test/TensorFlowNET.Examples/LogisticRegression.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ namespace TensorFlowNET.Examples
1717
/// </summary>
1818
public class LogisticRegression : Python, IExample
1919
{
20+
public bool Enabled => true;
2021
private float learning_rate = 0.01f;
2122
private int training_epochs = 10;
2223
private int batch_size = 100;
2324
private int display_step = 1;
2425

2526
Datasets mnist;
2627

27-
public void Run()
28+
public bool Run()
2829
{
2930
PrepareData();
3031

@@ -48,7 +49,7 @@ public void Run()
4849
// Initialize the variables (i.e. assign their default value)
4950
var init = tf.global_variables_initializer();
5051

51-
with(tf.Session(), sess =>
52+
return with(tf.Session(), sess =>
5253
{
5354

5455
// Run the initializer
@@ -88,7 +89,7 @@ public void Run()
8889
float acc = accuracy.eval(new FeedItem(x, mnist.test.images), new FeedItem(y, mnist.test.labels));
8990
print($"Accuracy: {acc.ToString("F4")}");
9091

91-
Predict();
92+
return acc > 0.9;
9293
});
9394
}
9495

0 commit comments

Comments
 (0)