Skip to content

Commit bd1e853

Browse files
committed
fix unit test evaluate.
1 parent fc5131d commit bd1e853

5 files changed

Lines changed: 74 additions & 69 deletions

File tree

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

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,67 @@ private Tensor _ProcessOutputTensor(Tensor val)
238238
return real_val;
239239
}
240240

241-
public override void AddInnerOp(Operation resultOp)
242-
{
243-
throw new NotImplementedException();
241+
protected override void _AddOpInternal(Operation op)
242+
{
243+
if (op.inputs.Length == 0)
244+
{
245+
//If we're in a while loop, remove any control inputs from outside the
246+
// loop.
247+
_RemoveExternalControlEdges(op);
248+
if (!op.control_inputs.Any(input_op => OpInContext(input_op)))
249+
op._add_control_input(_pivot.op);
250+
}
251+
else
252+
{
253+
// Make each input to 'op' available in this CondContext. If an input is
254+
// already part of this context there's nothing to do, but if it's
255+
// external, AddValue() will handle adding the appropriate Switch node and
256+
// other bookkeeping.
257+
for (int index = 0; index < op.inputs.Length; index++)
258+
{
259+
var x = op.inputs[index];
260+
Tensor real_x = null;
261+
if (op.type == "Merge" && x.op.type == "NextIteration")
262+
{
263+
//# Edge case: if we're importing a while loop inside this CondContext,
264+
//# AddValue() will not correctly handle the NextIteration inputs to
265+
//# Merge node. The problem is that the NextIteration should also be
266+
//# part of this context, but if we're importing it won't have been
267+
//# processed and added to the context yet, so AddValue() will try to
268+
//# add a Switch which results in an invalid graph. Instead, we use the
269+
//# NextIteration input as-is here, and it will eventually be added to
270+
//# the context via AddOp().
271+
real_x = x;
272+
}
273+
else
274+
{
275+
real_x = AddValue(x);
276+
}
277+
if (real_x != x)
278+
op._update_input(index, real_x);
279+
}
280+
// Remove any external control dependency on this op.
281+
_RemoveExternalControlEdges(op);
282+
// TODO: implement below code dependencies
283+
//if (op.graph._is_function(op.type) || op.type == "SymbolicGradient")
284+
// op._add_control_input(_pivot.op);
285+
}
286+
287+
// Mark op's outputs as seen by this context and any outer contexts.
288+
var output_names = op.outputs.Select(x => x.name).ToArray();
289+
IControlFlowContext ctxt = this;
290+
while (ctxt != null)
291+
{
292+
foreach (var name in output_names)
293+
ctxt.values.Add(name);
294+
ctxt = ctxt.outer_context;
295+
}
296+
297+
if (_outer_context != null || !control_flow_ops.IsLoopExit(op))
298+
op.graph.prevent_fetching(op);
299+
300+
if (_outer_context != null)
301+
_outer_context.AddInnerOp(op);
244302
}
245303

246304
public CondContextDef to_proto(string export_scope)

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

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

122-
public virtual void AddInnerOp(Operation resultOp)
122+
/// <summary>
123+
/// Notifies a scope about an operator added to an inner scope.
124+
/// </summary>
125+
/// <param name="op"></param>
126+
public virtual void AddInnerOp(Operation op)
123127
{
124-
// to be overridden
128+
if (_outer_context != null)
129+
_outer_context.AddInnerOp(op);
125130
}
126131

127132
protected HashSet<string> _values = new HashSet<string>();
@@ -131,68 +136,10 @@ public virtual void AddInnerOp(Operation resultOp)
131136
/// </summary>
132137
protected virtual void _AddOpInternal(Operation op)
133138
{
134-
if (op.inputs.Length == 0)
135-
{
136-
//If we're in a while loop, remove any control inputs from outside the
137-
// loop.
138-
_RemoveExternalControlEdges(op);
139-
if (!op.control_inputs.Any(input_op => OpInContext(input_op)))
140-
op._add_control_input(_pivot.op);
141-
}
142-
else
143-
{
144-
// Make each input to 'op' available in this CondContext. If an input is
145-
// already part of this context there's nothing to do, but if it's
146-
// external, AddValue() will handle adding the appropriate Switch node and
147-
// other bookkeeping.
148-
for (int index = 0; index < op.inputs.Length; index++)
149-
{
150-
var x = op.inputs[index];
151-
Tensor real_x = null;
152-
if (op.type == "Merge" && x.op.type == "NextIteration")
153-
{
154-
//# Edge case: if we're importing a while loop inside this CondContext,
155-
//# AddValue() will not correctly handle the NextIteration inputs to
156-
//# Merge node. The problem is that the NextIteration should also be
157-
//# part of this context, but if we're importing it won't have been
158-
//# processed and added to the context yet, so AddValue() will try to
159-
//# add a Switch which results in an invalid graph. Instead, we use the
160-
//# NextIteration input as-is here, and it will eventually be added to
161-
//# the context via AddOp().
162-
real_x = x;
163-
}
164-
else
165-
{
166-
real_x = AddValue(x);
167-
}
168-
if (real_x != x)
169-
op._update_input(index, real_x);
170-
}
171-
// Remove any external control dependency on this op.
172-
_RemoveExternalControlEdges(op);
173-
// TODO: implement below code dependencies
174-
//if (op.graph._is_function(op.type) || op.type == "SymbolicGradient")
175-
// op._add_control_input(_pivot.op);
176-
}
177139

178-
// Mark op's outputs as seen by this context and any outer contexts.
179-
var output_names = op.outputs.Select(x => x.name).ToArray();
180-
IControlFlowContext ctxt = this;
181-
while (ctxt != null)
182-
{
183-
foreach(var name in output_names)
184-
ctxt.values.Add(name);
185-
ctxt = ctxt.outer_context;
186-
}
187-
188-
if (_outer_context != null || !control_flow_ops.IsLoopExit(op))
189-
op.graph.prevent_fetching(op);
190-
191-
if (_outer_context != null)
192-
_outer_context.AddInnerOp(op);
193140
}
194141

195-
private bool OpInContext(Operation op)
142+
protected bool OpInContext(Operation op)
196143
{
197144
return IsContainingContext(op._get_control_flow_context(), this);
198145
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public static Tensor l2_normalize(Tensor x,
2323
return with(ops.name_scope(name, "l2_normalize", new { x }), scope =>
2424
{
2525
x = ops.convert_to_tensor(x, name: "x");
26-
var square_sum = math_ops.reduce_sum(math_ops.square(x), axis, keepdims: true);
26+
var sq = math_ops.square(x);
27+
var square_sum = math_ops.reduce_sum(sq, axis, keepdims: true);
2728
var x_inv_norm = math_ops.rsqrt(math_ops.maximum(square_sum, epsilon));
2829
return math_ops.multiply(x, x_inv_norm, name: name);
2930
});

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ public static NDArray _eval_using_default_session(Tensor tensor, FeedItem[] feed
360360
/// <returns>The default `Session` being used in the current thread.</returns>
361361
public static Session get_default_session()
362362
{
363+
if (tf.defaultSession == null)
364+
tf.defaultSession = tf.Session();
363365
return tf.defaultSession;
364366
}
365367

‎test/TensorFlowNET.UnitTest/PythonTest.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@ public T evaluate<T>(Tensor tensor)
143143
// return self._eval_helper(tensors)
144144
// else:
145145
{
146-
var sess = ops.get_default_session();
147-
if (sess == null)
148-
sess = self.session();
149-
with<Session>(sess, s =>
146+
with(ops.get_default_session(), s =>
150147
{
151148
var ndarray=tensor.eval();
152149
if (typeof(T) == typeof(double))

0 commit comments

Comments
 (0)