Skip to content

Commit 4a80846

Browse files
committed
fixed SciSharp#164
1 parent f189838 commit 4a80846

7 files changed

Lines changed: 53 additions & 17 deletions

File tree

‎src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
143143

144144
}
145145
});
146-
147-
// temp fix name scope
148-
op.Graph._name_stack = "gradients";
149146
}
150147
}
151148
else

‎src/TensorFlowNET.Core/Gradients/math_grad.py.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ public static (Tensor, Tensor) _RealDivGrad(Operation op, Tensor grad)
4848
var reduce_sum1 = math_ops.reduce_sum(realdiv1, rx);
4949
var realdiv2 = gen_math_ops.real_div(-x, y);
5050
var realdiv3 = gen_math_ops.real_div(realdiv2, y);
51-
var reduce_sum2 = math_ops.reduce_sum(grad * realdiv3, ry);
51+
var mul = grad * realdiv3;
52+
var reduce_sum2 = math_ops.reduce_sum(mul, ry);
5253

5354
return (gen_array_ops.reshape(reduce_sum1, sx), gen_array_ops.reshape(reduce_sum2, sy));
5455
}

‎src/TensorFlowNET.Core/Graphs/Graph.cs‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public partial class Graph : IDisposable
2323
private List<String> _unfetchable_ops = new List<string>();
2424

2525
public string _name_stack = "";
26-
public string old_stack = "";
2726
public string _graph_key;
2827
public Status Status { get; }
2928

@@ -180,8 +179,6 @@ public string get_name_scope()
180179

181180
public string name_scope(string name)
182181
{
183-
old_stack = _name_stack;
184-
185182
string new_stack = "";
186183

187184
if (name.EndsWith("/"))

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,6 @@ private static Tensor shape_internal(Tensor input, string name = "", bool optimi
9494
return constant_op.constant(nd, name);
9595
}
9696
}
97-
else
98-
{
99-
// result = gen_array_ops.shape();
100-
}
10197

10298
return gen_array_ops.shape(input);
10399
});

‎src/TensorFlowNET.Core/ops.name_scope.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class name_scope : IPython
1414
public object _values;
1515
public Context _ctx;
1616
public string _name_scope;
17+
public string old_stack = "";
1718
private object _g_manager;
1819

1920
public name_scope(string name, string default_name = "", object values = null)
@@ -38,15 +39,14 @@ public void __enter__()
3839
if (g == null)
3940
g = get_default_graph();
4041

42+
old_stack = g._name_stack;
4143
_name_scope = g.name_scope(_name);
4244
}
4345

4446
public void Dispose()
4547
{
4648
var g = get_default_graph();
47-
g._name_stack = g.old_stack;
48-
// clear g._name_stack
49-
g.old_stack = "";
49+
g._name_stack = old_stack;
5050
}
5151

5252
public void __exit__()

‎src/TensorFlowNET.Core/ops.py.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,11 @@ public static Session get_default_session()
294294
switch (oper.type)
295295
{
296296
case "Add":
297-
return math_grad._AddGrad(op, out_grads);
297+
return math_grad._AddGrad(oper, out_grads);
298298
case "Sum":
299-
return math_grad._SumGrad(op, out_grads);
299+
return math_grad._SumGrad(oper, out_grads);
300300
case "RealDiv":
301-
return math_grad._RealDivGrad(op, out_grads);
301+
return math_grad._RealDivGrad(oper, out_grads);
302302
default:
303303
throw new NotImplementedException($"get_gradient_function {oper.type}");
304304
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Tensorflow;
6+
7+
namespace TensorFlowNET.UnitTest
8+
{
9+
[TestClass]
10+
public class NameScopeTest : Python
11+
{
12+
Graph g = ops.get_default_graph();
13+
string name = "";
14+
15+
[TestMethod]
16+
public void NestedNameScope()
17+
{
18+
with<ops.name_scope>(new ops.name_scope("scope1"), scope1 =>
19+
{
20+
name = scope1;
21+
Assert.AreEqual("scope1", g._name_stack);
22+
Assert.AreEqual("scope1/", name);
23+
24+
var const1 = tf.constant(1.0);
25+
Assert.AreEqual("scope1/Const:0", const1.name);
26+
27+
with<ops.name_scope>(new ops.name_scope("scope2"), scope2 =>
28+
{
29+
name = scope2;
30+
Assert.AreEqual("scope1/scope2", g._name_stack);
31+
Assert.AreEqual("scope1/scope2/", name);
32+
33+
var const2 = tf.constant(2.0);
34+
Assert.AreEqual("scope1/scope2/Const:0", const2.name);
35+
});
36+
37+
Assert.AreEqual("scope1", g._name_stack);
38+
var const3 = tf.constant(2.0);
39+
Assert.AreEqual("scope1/Const_1:0", const3.name);
40+
});
41+
42+
Assert.AreEqual("", g._name_stack);
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)