Skip to content

Commit ccce438

Browse files
committed
finished HelloWorld example.
1 parent 458b396 commit ccce438

11 files changed

Lines changed: 73 additions & 37 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.constant.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public static partial class tf
99
{
1010
public static Tensor constant(NDArray nd, string name = "Const", bool verify_shape = false)
1111
{
12-
//constant_op.Create(nd, name, verify_shape);
13-
var graph = tf.get_default_graph();
12+
var t = constant_op.Create(nd, name, verify_shape);
13+
/*var graph = tf.get_default_graph();
1414
var tensor = new Tensor(nd);
15-
var op = graph.NewOperation("Const", name, tensor);
15+
var op = graph.NewOperation("Const", name, tensor);*/
1616

17-
return null;
17+
return t;
1818
}
1919
}
2020
}

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ namespace Tensorflow
88
{
99
public partial class Graph
1010
{
11-
public Operation NewOperation(string opType, string opName, Tensor tensor)
11+
public OpDef GetOpDef(string type)
1212
{
13-
var desc = c_api.TF_NewOperation(_handle, opType, opName);
14-
15-
if (tensor.dtype == TF_DataType.TF_STRING)
16-
{
17-
var value = "Hello World!";
18-
var bytes = Encoding.UTF8.GetBytes(value);
19-
var buf = Marshal.AllocHGlobal(bytes.Length + 1);
20-
Marshal.Copy(bytes, 0, buf, bytes.Length);
21-
c_api.TF_SetAttrString(desc, "value", buf, (uint)value.Length);
22-
}
23-
else
13+
using (var buffer = new Buffer())
14+
using (var status = new Status())
2415
{
25-
c_api.TF_SetAttrTensor(desc, "value", tensor, Status);
16+
c_api.TF_GraphGetOpDef(_handle, type, buffer, status);
17+
return OpDef.Parser.ParseFrom(buffer.Data);
2618
}
19+
}
20+
21+
public OperationDescription NewOperation(string opType, string opName)
22+
{
23+
OperationDescription desc = c_api.TF_NewOperation(_handle, opType, opName);
24+
return desc;
25+
26+
/*c_api.TF_SetAttrTensor(desc, "value", tensor, Status);
2727
2828
Status.Check();
2929
@@ -32,7 +32,7 @@ public Operation NewOperation(string opType, string opName, Tensor tensor)
3232
var op = c_api.TF_FinishOperation(desc, Status);
3333
Status.Check();
3434
35-
return op;
35+
return op;*/
3636
}
3737
}
3838
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,15 @@ public unsafe Operation create_op(string op_type, List<Tensor> inputs, TF_DataTy
106106
original_op: null,
107107
op_def: op_def);
108108

109+
_create_op_helper(op, true);
109110
return op;
110111
}
111112

113+
private void _create_op_helper(Operation op, bool compute_device = true)
114+
{
115+
116+
}
117+
112118
public void _add_op(Operation op)
113119
{
114120
_nodes_by_id[op._id] = op;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,17 @@ public Operation(NodeDef node_def, Graph g, List<Tensor> inputs = null, TF_DataT
109109
Graph = g;
110110

111111
_id_value = Graph._next_id();
112+
if(op_def == null)
113+
op_def = g.GetOpDef(node_def.Op);
112114

113115
_handle = ops._create_c_op(g, node_def, inputs);
114-
116+
115117
_outputs = new Tensor[NumOutputs];
118+
output_types = new TF_DataType[NumOutputs];
119+
116120
for (int i = 0; i < NumOutputs; i++)
117121
{
122+
output_types[i] = OutputType(i);
118123
_outputs[i] = new Tensor(this, i, output_types[i]);
119124
}
120125

‎src/TensorFlowNET.Core/Operations/c_api.ops.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public static partial class c_api
197197
public static extern int TF_OperationOutputListLength(IntPtr oper, string arg_name, IntPtr status);
198198

199199
[DllImport(TensorFlowLibName)]
200-
public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, UIntPtr proto_len, IntPtr status);
200+
public static extern void TF_SetAttrValueProto(IntPtr desc, string attr_name, IntPtr proto, uint proto_len, IntPtr status);
201201

202202
/// <summary>
203203
/// Set `num_dims` to -1 to represent "unknown rank".

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public static Tensor convert_to_tensor(object value, string name = "")
2525

2626
public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Tensor> inputs)
2727
{
28-
var op_desc = c_api.TF_NewOperation(graph, node_def.Op, node_def.Name);
28+
var op_desc = graph.NewOperation(node_def.Op, node_def.Name);
2929

3030
// Add inputs
3131
if(inputs != null && inputs.Count > 0)
3232
{
33-
foreach (var op_input in inputs)
33+
/*foreach (var op_input in inputs)
3434
{
3535
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
36-
}
36+
}*/
3737

38-
//c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
38+
c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
3939
}
4040

4141
var status = new Status();
@@ -48,9 +48,10 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten
4848
var bytes = attr.Value.ToByteArray();
4949
var proto = Marshal.AllocHGlobal(bytes.Length);
5050
Marshal.Copy(bytes, 0, proto, bytes.Length);
51-
c_api.TF_SetAttrValueProto(op_desc, attr.Key, proto, proto_len: (UIntPtr)bytes.Length, status: status);
51+
52+
c_api.TF_SetAttrValueProto(op_desc, attr.Key, proto, proto_len: (uint)bytes.Length, status: status);
5253

53-
if(status.Code != TF_Code.TF_OK) throw new Exception(status.Message);
54+
status.Check(true);
5455
}
5556

5657
var c_op = c_api.TF_FinishOperation(op_desc, status);
@@ -60,6 +61,11 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten
6061
return c_op;
6162
}
6263

64+
public static OpDef _get_op_def(Graph graph, string type)
65+
{
66+
return graph.GetOpDef(type);
67+
}
68+
6369
public static NodeDef _NodeDef(string op_type, string name, string device = "", Dictionary<string, AttrValue> attrs = null)
6470
{
6571
var node_def = new node_def_pb2.NodeDef();

‎src/TensorFlowNET.Core/Sessions/BaseSession.cs‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,27 @@ private unsafe object[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] fe
115115
run_metadata: IntPtr.Zero,
116116
status: status);
117117

118-
var result = output_values.Select(x => c_api.TF_TensorData(x))
119-
.Select(x => (object)*(float*)x)
120-
.ToArray();
118+
object[] result = new object[fetch_list.Length];
121119

122-
var op = new Operation(fetch_list[0].oper);
123-
//var metadata = c_api.TF_OperationGetAttrMetadata(fetch_list[0].oper, "dtype", status);
120+
for (int i = 0; i < fetch_list.Length; i++)
121+
{
122+
var tensor = new Tensor(output_values[i]);
123+
124+
switch (tensor.dtype)
125+
{
126+
case TF_DataType.TF_STRING:
127+
// wired, don't know why we have to start from offset 9.
128+
var bytes = tensor.Data();
129+
result[i] = UTF8Encoding.Default.GetString(bytes, 9, bytes.Length - 9);
130+
break;
131+
case TF_DataType.TF_FLOAT:
132+
result[i] = *(float*)c_api.TF_TensorData(output_values[i]);
133+
break;
134+
default:
135+
throw new NotImplementedException("can't get output");
136+
break;
137+
}
138+
}
124139

125140
return result;
126141
}

‎src/TensorFlowNET.Core/Status/Status.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Tensorflow
88
/// TF_Status holds error information. It either has an OK code, or
99
/// else an error code with an associated error message.
1010
/// </summary>
11-
public class Status
11+
public class Status : IDisposable
1212
{
1313
private readonly IntPtr _handle;
1414

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ public byte[] Data()
146146
{
147147
var data = new byte[bytesize];
148148
Marshal.Copy(buffer, data, 0, (int)bytesize);
149-
150149
return data;
151150
}
152151

‎src/TensorFlowNET.Core/Tensors/constant_op.cs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ public class constant_op
2222
public static Tensor Create(NDArray nd, string name = "Const", bool verify_shape = false)
2323
{
2424
Graph g = ops.get_default_graph();
25-
var tensor_value = new AttrValue();
2625
var tensor_pb = tensor_util.make_tensor_proto(nd, verify_shape);
27-
tensor_value.Tensor = tensor_pb;
26+
var tensor_value = new AttrValue
27+
{
28+
Type = tensor_pb.Dtype,
29+
Tensor = tensor_pb
30+
};
31+
2832
var dtype_value = new AttrValue
2933
{
3034
Type = tensor_value.Tensor.Dtype,
3135
};
3236

3337
var attrs = new Dictionary<string, AttrValue>();
34-
attrs["dtype"] = dtype_value;
3538
attrs["value"] = tensor_value;
39+
attrs["dtype"] = dtype_value;
3640

3741
var op = g.create_op("Const",
3842
null,

0 commit comments

Comments
 (0)