forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlFlowApiTest.cs
More file actions
66 lines (56 loc) · 2.1 KB
/
Copy pathControlFlowApiTest.cs
File metadata and controls
66 lines (56 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.NumPy;
using System;
using Tensorflow;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest.ManagedAPI
{
[TestClass]
public class ControlFlowApiTest
{
[TestMethod]
public void WhileLoopOneInputEagerMode()
{
tf.enable_eager_execution();
var i = tf.constant(2);
Func<Tensor, Tensor> c = (x) => tf.less(x, 10);
Func<Tensor, Tensor> b = (x) => tf.add(x, 1);
var r = tf.while_loop(c, b, i);
Assert.AreEqual(10, (int)r);
}
[TestMethod]
public void WhileLoopTwoInputsEagerMode()
{
tf.enable_eager_execution();
var i = tf.constant(2);
var j = tf.constant(3);
Func<Tensors, Tensor> c = (x) => tf.less(x[0] + x[1], 10);
Func<Tensors, Tensors> b = (x) => new[] { tf.add(x[0], 1), tf.add(x[1], 1) };
var r = tf.while_loop(c, b, new[] { i, j });
Assert.AreEqual(5, (int)r[0]);
Assert.AreEqual(6, (int)r[1]);
}
[TestMethod, Ignore]
public void WhileLoopGraphMode()
{
tf.compat.v1.disable_eager_execution();
var i = tf.constant(2);
Func<Tensor, Tensor> c = (x) => tf.less(x, 10);
Func<Tensor, Tensor> b = (x) => tf.add(x, 1);
var r = tf.while_loop(c, b, i);
Assert.AreEqual(10, (int)r);
}
[TestMethod, Ignore]
public void ScanFunctionGraphMode()
{
tf.compat.v1.disable_eager_execution();
Func<Tensor, Tensor, Tensor> fn = (prev, current) => tf.add(prev, current);
var input = tf.placeholder(TF_DataType.TF_FLOAT, new Shape(6));
var scan = tf.scan(fn, input);
var sess = tf.Session();
sess.run(tf.global_variables_initializer());
var result = sess.run(scan, new FeedItem(input, np.array(1, 2, 3, 4, 5, 6)));
Assert.AreEqual(new float[] { 1, 3, 6, 10, 15, 21 }, result.ToArray<float>());
}
}
}