Skip to content

Commit 6a1ed38

Browse files
committed
1 parent 7e77cb5 commit 6a1ed38

8 files changed

Lines changed: 33 additions & 31 deletions

File tree

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class CondContext : ControlFlowContext, IProtoBuf<CondContextDef, CondCon
3737
/// <param name="import_scope"></param>
3838
public CondContext(Tensor pred = null,
3939
Tensor pivot = null,
40-
int? branch = null,
40+
int branch = 0,
4141
string name = "cond_text",
4242
CondContextDef context_def = null,
4343
string import_scope = null)
@@ -55,7 +55,7 @@ public CondContext(Tensor pred = null,
5555
base.__init__();
5656
_pred = pred;
5757
_pivot = pivot;
58-
58+
_branch = branch; // 0 or 1 representing this branch
5959
// Values considered to have been already seen in this context. pred is not
6060
// included in this context.
6161
_values.Add(pred.name);
@@ -105,11 +105,6 @@ public override Tensor AddValue(Tensor val)
105105
_external_values[result.name] = result;
106106
}
107107

108-
// for debug purpose
109-
if(ops.get_default_graph()._nodes_by_name.Count > 60)
110-
{
111-
}
112-
113108
with(ops.control_dependencies(null), ctrl =>
114109
{
115110
var (r0, r1) = control_flow_ops._SwitchRefOrTensor(result, _pred);

src/TensorFlowNET.Core/Operations/Operation.Control.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void _control_flow_post_processing()
2929

3030
public void _add_control_input(Operation op)
3131
{
32-
// c_api.TF_AddControlInput(_operDesc, op);
32+
//c_api.TF_AddControlInput(_operDesc, op);
3333
c_api.AddControlInput(graph, _handle, op);
3434
}
3535

src/TensorFlowNET.Core/Operations/Operation.Input.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public InputList inputs
2727

2828
for (int i = 0; i < NumInputs; i++)
2929
{
30-
var tf_outputs = Input(i);
31-
var op = new Operation(tf_outputs.oper);
32-
retval[i] = op.outputs[tf_outputs.index];
30+
var tf_output = Input(i);
31+
var op = new Operation(tf_output.oper);
32+
retval[i] = op.outputs[tf_output.index];
3333
}
3434

3535
_inputs = new InputList(retval);

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public partial class Operation : ITensorOrOperation
5151
public string Device => c_api.StringPiece(c_api.TF_OperationDevice(_handle));
5252

5353
private NodeDef _node_def;
54+
//[JsonIgnore]
5455
public NodeDef node_def
5556
{
5657
get
@@ -290,6 +291,7 @@ public void _update_input(int index, Tensor tensor)
290291
// the updated inputs are reloaded from the c_api
291292
c_api.UpdateEdge(_graph, output, input, status);
292293
//var updated_inputs = inputs;
294+
status.Check();
293295
}
294296

295297
private void _assert_same_graph(Tensor tensor)

src/TensorFlowNET.Core/Operations/gen_control_flow_ops.py.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ public static Operation no_op(string name = null)
3636
public static (Tensor, Tensor) @switch(Tensor data, Tensor pred, string name = null)
3737
{
3838
var _op = _op_def_lib._apply_op_helper("Switch", name, new { data, pred });
39-
var _result = (_op.outputs[0], _op.outputs[1]);
4039
var _inputs_flat = _op.inputs;
4140
var _attrs = ("T", _op.get_attr("T"));
4241
// TODO: missing original code
4342
//_execute.record_gradient("Switch", _inputs_flat, _attrs, _result, name);
44-
return _result;
43+
return (_op.outputs[0], _op.outputs[1]);
4544
}
4645

4746
public static (Tensor, Tensor) merge(Tensor[] inputs, string name = null)

src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ private IntPtr Allocate(NDArray nd)
128128

129129
public Tensor(Operation op, int value_index, TF_DataType dtype)
130130
{
131-
this.op = op;
132-
this.value_index = value_index;
133-
this._dtype = dtype;
131+
_op = op;
132+
_value_index = value_index;
133+
_dtype = dtype;
134134
_id = ops.uid();
135135
}
136136
}

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ public partial class Tensor : Python, IDisposable, ITensorOrOperation
2222
public int Id => _id;
2323
//[JsonIgnore]
2424
public Graph graph => op?.graph;
25+
private Operation _op;
2526
//[JsonIgnore]
26-
public Operation op { get; }
27+
public Operation op => _op;
2728
//[JsonIgnore]
2829
public Tensor[] outputs => op.outputs;
2930

3031
/// <summary>
3132
/// The string name of this tensor.
3233
/// </summary>
33-
public string name => $"{(op == null ? "Operation was not named" : $"{op.name}:{value_index}")}";
34+
public string name => $"{(op == null ? "Operation was not named" : $"{op.name}:{_value_index}")}";
3435

35-
public int value_index { get; }
36+
private int _value_index;
37+
public int value_index => _value_index;
3638

3739
private Status status = new Status();
3840

test/TensorFlowNET.UnitTest/control_flow_ops_test/CondTestCases.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public void testCondTrue_ConstOnly()
2222
var y = tf.constant(5, name: "y");
2323

2424
var z = control_flow_ops.cond(tf.less(x, y),
25-
() => tf.constant(22, name: "t2"),
26-
() => tf.constant(55, name: "f5"));
25+
() => tf.constant(22, name: "t22"),
26+
() => tf.constant(55, name: "f55"));
2727

2828
int result = z.eval(sess);
2929
assertEquals(result, 22);
@@ -41,8 +41,8 @@ public void testCondFalse_ConstOnly()
4141
var y = tf.constant(1, name: "y");
4242

4343
var z = control_flow_ops.cond(tf.less(x, y),
44-
() => tf.constant(22, name: "t2"),
45-
() => tf.constant(11, name: "f1"));
44+
() => tf.constant(22, name: "t22"),
45+
() => tf.constant(11, name: "f11"));
4646

4747
int result = z.eval(sess);
4848
assertEquals(result, 11);
@@ -56,17 +56,18 @@ public void testCondTrue()
5656

5757
with(tf.Session(graph), sess =>
5858
{
59-
var x = tf.constant(2);
60-
var y = tf.constant(5);
61-
var z = control_flow_ops.cond(tf.less(x, y), () => tf.multiply(x, tf.constant(17)),
62-
() => tf.add(y, tf.constant(23)));
63-
//tf.train.export_meta_graph(@"D:\dev\tensorboard\logdir\sharp.meta", as_text: false);
59+
var x = tf.constant(2, name: "x");
60+
var y = tf.constant(5, name: "y");
61+
62+
var z = control_flow_ops.cond(tf.less(x, y),
63+
() => tf.multiply(x, 17),
64+
() => tf.add(y, 23));
65+
6466
int result = z.eval(sess);
6567
assertEquals(result, 34);
6668
});
6769
}
6870

69-
//[Ignore("This Test Fails due to missing edges in the graph!")]
7071
[TestMethod]
7172
public void testCondFalse()
7273
{
@@ -76,8 +77,11 @@ public void testCondFalse()
7677
{
7778
var x = tf.constant(2);
7879
var y = tf.constant(1);
79-
var z = control_flow_ops.cond(tf.less(x, y), () => tf.multiply(x, tf.constant(17)),
80-
() => tf.add(y, tf.constant(23)));
80+
81+
var z = control_flow_ops.cond(tf.less(x, y),
82+
() => tf.multiply(x, 17),
83+
() => tf.add(y, 23));
84+
8185
int result = z.eval(sess);
8286
assertEquals(result, 24);
8387
});

0 commit comments

Comments
 (0)