Skip to content

Commit b66e258

Browse files
committed
ported while and switch/case testcases
1 parent 1e63200 commit b66e258

9 files changed

Lines changed: 130 additions & 28 deletions

File tree

‎src/TensorFlowNET.Core/Operations/ControlFlows/ControlFlowContext.cs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ public virtual Tensor AddValue(Tensor val)
119119
return null;
120120
}
121121

122-
/// <summary>
123-
/// Notifies a scope about an operator added to an inner scope.
124-
/// </summary>
122+
/// <summary>
123+
/// Notifies a scope about an operator added to an inner scope.
124+
/// </summary>
125125
/// <param name="op"></param>
126126
public virtual void AddInnerOp(Operation op)
127127
{
128-
if (_outer_context != null)
128+
if (_outer_context != null)
129129
_outer_context.AddInnerOp(op);
130130
}
131131

@@ -164,6 +164,12 @@ protected virtual void _RemoveExternalControlEdges(Operation op)
164164
var internal_control_inputs = op.control_inputs;
165165
}
166166

167+
public object to_proto()
168+
{
169+
throw new NotImplementedException();
170+
}
171+
172+
167173
public void Dispose()
168174
{
169175
}

‎src/TensorFlowNET.Core/Operations/ControlFlows/IControlFlowContext.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public interface IControlFlowContext
1111
HashSet<string> values { get; }
1212
Tensor AddValue(Tensor val);
1313
void AddInnerOp(Operation resultOp);
14+
object to_proto();
1415
}
1516
}

‎src/TensorFlowNET.Core/Operations/ControlFlows/WhileContext.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@ namespace Tensorflow.Operations
66
{
77
public class WhileContext : ControlFlowContext
88
{
9+
public static WhileContext from_proto(object proto)
10+
{
11+
throw new NotImplementedException();
12+
}
13+
14+
public object to_proto()
15+
{
16+
throw new NotImplementedException();
17+
}
918
}
1019
}

‎src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,13 @@ public static Tensor ZerosLikeOutsideLoop(Operation op, int index)
490490
}
491491

492492
throw new NotImplementedException("ZerosLikeOutsideLoop");
493-
}
494-
495-
493+
}
494+
495+
// TODO
496+
public static void while_loop(Func<Tensor, Tensor> func, Func<Tensor, Tensor> func1, Tensor[] tensors, int? i)
497+
{
498+
throw new NotImplementedException();
499+
}
500+
496501
}
497502
}

‎src/TensorFlowNET.Core/Variables/variable_scope.py.cs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,11 @@ public void Dispose()
240240
if (_current_name_scope != null)
241241
_current_name_scope.Dispose();
242242
}
243+
244+
// TODO for Switch/Case
245+
public static RefVariable get_variable(string embeddingMatrix, double[,] initializer, bool use_resource)
246+
{
247+
throw new NotImplementedException();
248+
}
243249
}
244250
}

‎src/TensorFlowNET.Core/Variables/variables.py.cs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,10 @@ public static Operation variables_initializer(RefVariable[] var_list, string nam
6767
else
6868
return gen_control_flow_ops.no_op(name: name);
6969
}
70+
71+
public static Tensor global_variables_initializer()
72+
{
73+
throw new NotImplementedException();
74+
}
7075
}
7176
}

‎test/TensorFlowNET.UnitTest/PythonTest.cs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ public void assertTrue(bool cond)
9595
Assert.IsTrue(cond);
9696
}
9797

98+
99+
public void assertProtoEquals(object toProto, object o)
100+
{
101+
throw new NotImplementedException();
102+
}
103+
98104
#endregion
99105

100-
#region tensor evaluation
106+
#region tensor evaluation and test session
101107

102108
protected object _eval_helper(Tensor[] tensors)
103109
{
@@ -166,6 +172,11 @@ public T evaluate<T>(Tensor tensor)
166172
}
167173

168174

175+
protected Session cached_session()
176+
{
177+
throw new NotImplementedException();
178+
}
179+
169180
//Returns a TensorFlow Session for use in executing tests.
170181
public Session session(Graph graph = null, object config = null, bool use_gpu = false, bool force_gpu = false)
171182
{

‎test/TensorFlowNET.UnitTest/control_flow_ops_test/SwitchTestCase.cs‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
23
using Tensorflow;
34

45
namespace TensorFlowNET.UnitTest.control_flow_ops_test
@@ -14,24 +15,33 @@ public class SwitchTestCase : PythonTest
1415
[TestMethod]
1516
public void testResourceReadInLoop()
1617
{
17-
//def testResourceReadInLoop(self):
18-
// embedding_matrix = variable_scope.get_variable(
19-
// "embedding_matrix", initializer=[[2.0], [3.0]], use_resource=True)
20-
//
21-
// def cond(it, _):
22-
// return it < 5
23-
//
24-
// def body(it, cost):
25-
// embedding = embedding_ops.embedding_lookup(embedding_matrix, [0])
26-
// cost += math_ops.reduce_sum(embedding)
27-
// return it + 1, cost
28-
//
29-
// _, cost = control_flow_ops.while_loop(
30-
// cond, body, [constant_op.constant(0),
31-
// constant_op.constant(0.0)])
32-
// with self.cached_session():
33-
// self.evaluate(variables.global_variables_initializer())
34-
// self.assertAllEqual(10.0, self.evaluate(cost))
18+
19+
var embedding_matrix = variable_scope.get_variable(
20+
"embedding_matrix", initializer: new double[,] { { 2.0 }, { 3.0 } }, use_resource: true);
21+
22+
Tensor cond(Tensor it, Tensor _)
23+
{
24+
return it < 5;
25+
}
26+
27+
// TODO: below code doesn't compile
28+
//(Tensor, Tensor) body(Tensor it, Tensor cost)
29+
//{
30+
// var embedding = embedding_ops.embedding_lookup(embedding_matrix, new int[]{0});
31+
// cost += math_ops.reduce_sum(embedding);
32+
// return (it + 1, cost);
33+
//}
34+
//var (_, cost1) = control_flow_ops.while_loop(
35+
// cond, body, new[]
36+
// {
37+
// constant_op.constant(0),
38+
// constant_op.constant(0.0)
39+
// });
40+
//with<Session>(this.cached_session(), sess =>
41+
//{
42+
// self.evaluate(variables.global_variables_initializer());
43+
// self.assertAllEqual(10.0, self.evaluate(cost1));
44+
//});
3545
}
3646

3747

@@ -49,7 +59,7 @@ public void testIndexedSlicesGradientInCondInWhileLoopResource()
4959
doTestIndexedSlicesGradientInCondInWhileLoop(use_resource: true);
5060
}
5161

52-
private void doTestIndexedSlicesGradientInCondInWhileLoop(bool use_resource= false)
62+
private void doTestIndexedSlicesGradientInCondInWhileLoop(bool use_resource = false)
5363
{
5464
//def doTestIndexedSlicesGradientInCondInWhileLoop(self, use_resource=False):
5565
// embedding_matrix = variable_scope.get_variable(
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
using Tensorflow;
7+
using Tensorflow.Operations;
8+
9+
namespace TensorFlowNET.UnitTest.control_flow_ops_test
10+
{
11+
[TestClass]
12+
public class WhileContextTestCase : PythonTest
13+
{
14+
private void _testWhileContextHelper(int? maximum_iterations = null)
15+
{
16+
// TODO: implement missing code dependencies
17+
with<Session>(this.cached_session(), sess =>
18+
{
19+
var i = constant_op.constant(0, name: "i");
20+
var c = new Func<Tensor, Tensor>(x => gen_math_ops.less(x, 10, name: "c"));
21+
var b = new Func<Tensor, Tensor>(x => gen_math_ops.add(x, 1, name: "c"));
22+
control_flow_ops.while_loop(
23+
c, b, new[] { i }, maximum_iterations = maximum_iterations);
24+
foreach (Operation op in sess.graph.get_operations())
25+
{
26+
var control_flow_context = op._get_control_flow_context();
27+
if (control_flow_context != null)
28+
self.assertProtoEquals(control_flow_context.to_proto(),
29+
WhileContext.from_proto(
30+
control_flow_context.to_proto()).to_proto());
31+
}
32+
});
33+
}
34+
35+
[Ignore("TODO")]
36+
[TestMethod]
37+
public void testWhileContext()
38+
{
39+
_testWhileContextHelper();
40+
}
41+
42+
[Ignore("TODO")]
43+
[TestMethod]
44+
public void testWhileContextWithMaximumIterations()
45+
{
46+
_testWhileContextHelper(maximum_iterations: 10);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)