Skip to content

Commit c56e2c8

Browse files
committed
finished #140
1 parent 1a9e610 commit c56e2c8

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ public Operation(IntPtr handle)
3838
return;
3939

4040
_handle = handle;
41+
42+
_outputs = new Tensor[NumOutputs];
43+
for (int i = 0; i < NumOutputs; i++)
44+
_outputs[i] = new Tensor(this, i, OutputType(i));
4145
}
4246

4347
public Operation(Graph g, string opType, string oper_name)
@@ -99,7 +103,6 @@ public Operation(NodeDef node_def, Graph g, List<Tensor> inputs = null, TF_DataT
99103

100104
// Initialize self._outputs.
101105
output_types = new TF_DataType[NumOutputs];
102-
103106
for (int i = 0; i < NumOutputs; i++)
104107
output_types[i] = OutputType(i);
105108

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public static implicit operator Tensor(int scalar)
1616
return constant_op.Constant(scalar);
1717
}
1818

19+
public static implicit operator int(Tensor tensor)
20+
{
21+
return tensor.Data<int>()[0];
22+
}
23+
1924
public static implicit operator IntPtr(Tensor tensor)
2025
{
2126
return tensor._handle;

src/TensorFlowNET.Core/Variables/RefVariable.Implicit.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ public static implicit operator Tensor(RefVariable var)
2323

2424
public static implicit operator RefVariable(Tensor var)
2525
{
26-
switch (var.dtype)
27-
{
28-
case TF_DataType.TF_INT32:
29-
return tf.Variable(var.Data<int>()[0]);
30-
}
31-
3226
return null;
3327
}
3428
}

test/TensorFlowNET.UnitTest/VariableTest.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public void ScalarVar()
4343
[TestMethod]
4444
public void Add()
4545
{
46-
var x = tf.Variable(10, name: "x");
46+
int result = 0;
47+
Tensor x = tf.Variable(10, name: "x");
4748

4849
var model = tf.global_variables_initializer();
4950
using (var session = tf.Session())
@@ -52,10 +53,12 @@ public void Add()
5253
for(int i = 0; i < 5; i++)
5354
{
5455
x = x + 1;
55-
var result = session.run(x);
56+
result = session.run(x);
5657
print(result);
5758
}
5859
}
60+
61+
Assert.AreEqual(15, result);
5962
}
6063
}
6164
}

0 commit comments

Comments
 (0)