FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

add oper constant · attackgithub/TensorFlow.NET@cb96250 · GitHub

Repository navigation

Commit cb96250

Browse files
committed
add oper constant
1 parent 5f595eb commit cb96250

6 files changed

Lines changed: 58 additions & 21 deletions

File tree

‎README.md‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,37 @@ TensorFlow.NET is a member project of SciSharp stack.
77
![tensors_flowing](docs/assets/tensors_flowing.gif)
88

99
### How to use
10+
Download the pre-compiled dll [here](tensorflowlib) and place it in the bin folder.
11+
12+
```cs
13+
// import tensorflow.net
14+
using using Tensorflow;
15+
```
16+
1017
```cs
11-
using TensorFlowNET.Core;
18+
// Create a Constant op
19+
var a = tf.constant(4.0f);
20+
var b = tf.constant(5.0f);
21+
var c = tf.add(a, b);
1222

13-
namespace TensorFlowNET.Examples
23+
using (var sess = tf.Session())
1424
{
15-
public class HelloWorld : IExample
16-
{
17-
public void Run()
18-
{
19-
var hello = tf.constant("Hello, TensorFlow!");
20-
}
21-
}
25+
var o = sess.run(c);
26+
}
27+
```
28+
29+
```cs
30+
// Create a placeholder op
31+
var a = tf.placeholder(tf.float32);
32+
var b = tf.placeholder(tf.float32);
33+
var c = tf.add(a, b);
34+
35+
using(var sess = tf.Session())
36+
{
37+
var feed_dict = new Dictionary<Tensor, object>();
38+
feed_dict.Add(a, 3.0f);
39+
feed_dict.Add(b, 2.0f);
40+
41+
var o = sess.run(c, feed_dict);
2242
}
2343
```

‎src/TensorFlowNET.Core/BaseSession.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,16 @@ private unsafe byte[] _run(Tensor fetches, Dictionary<Tensor, object> feed_dict
6666
var status = new Status();
6767

6868
c_api.TF_SessionRun(_session,
69-
run_options: null,
69+
run_options: IntPtr.Zero,
7070
inputs: new TF_Output[] { },
7171
input_values: new IntPtr[] { },
72-
ninputs: 1,
72+
ninputs: 0,
7373
outputs: new TF_Output[] { },
7474
output_values: new IntPtr[] { },
7575
noutputs: 1,
7676
target_opers: new IntPtr[] { },
7777
ntargets: 1,
78-
run_metadata: null,
78+
run_metadata: IntPtr.Zero,
7979
status: status.Handle);
8080

8181
return null;

‎src/TensorFlowNET.Core/c_api.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ public static class c_api
6767
public static extern unsafe void TF_SetAttrTensor(TF_OperationDescription desc, string attr_name, TF_Tensor value, TF_Status status);
6868

6969
[DllImport(TensorFlowLibName)]
70-
public static extern unsafe void TF_SessionRun(TF_Session session, TF_Buffer* run_options,
70+
public static extern unsafe void TF_SessionRun(TF_Session session, IntPtr run_options,
7171
TF_Output[] inputs, TF_Tensor[] input_values, int ninputs,
7272
TF_Output[] outputs, TF_Tensor[] output_values, int noutputs,
7373
TF_Operation[] target_opers, int ntargets,
74-
TF_Buffer* run_metadata,
74+
IntPtr run_metadata,
7575
TF_Status status);
7676

7777
[DllImport(TensorFlowLibName)]

‎src/TensorFlowNET.Core/ops.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten
2121
var op_desc = c_api.TF_NewOperation(graph.Handle, node_def.Op, node_def.Name);
2222

2323
// Add inputs
24-
foreach(var op_input in inputs)
24+
if(inputs != null)
2525
{
26-
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
26+
foreach (var op_input in inputs)
27+
{
28+
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
29+
}
2730
}
2831

2932
var status = new Status();

‎src/TensorFlowNET.Core/tensor_util.cs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ public static TensorProto make_tensor_proto(object values, Type dtype = null)
1616

1717
switch (values)
1818
{
19+
case float val:
20+
nparray = np.array(new float[] { val }, np.float32);
21+
tensor_proto = new tensor_pb2.TensorProto
22+
{
23+
Dtype = DataType.DtFloat,
24+
TensorShape = tensor_shape.as_shape().as_proto()
25+
};
26+
tensor_proto.FloatVal.Add(val);
27+
break;
1928
case double val:
2029
nparray = np.array(new double[] { val }, np.float64);
2130
tensor_proto = new tensor_pb2.TensorProto
@@ -25,7 +34,6 @@ public static TensorProto make_tensor_proto(object values, Type dtype = null)
2534
};
2635
tensor_proto.DoubleVal.Add(val);
2736
break;
28-
2937
case string val:
3038
nparray = np.array(new string[] { val }, np.chars);
3139
tensor_proto = new tensor_pb2.TensorProto

‎test/TensorFlowNET.UnitTest/OperationsTest.cs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ public class OperationsTest
1212
[TestMethod]
1313
public void constant()
1414
{
15-
tf.constant(4.0);
15+
var a = tf.constant(4.0f);
16+
var b = tf.constant(5.0f);
17+
var c = tf.add(a, b);
18+
using (var sess = tf.Session())
19+
{
20+
var o = sess.run(c);
21+
}
1622
}
1723

1824
[TestMethod]
@@ -31,10 +37,10 @@ public void add()
3137
using(var sess = tf.Session())
3238
{
3339
var feed_dict = new Dictionary<Tensor, object>();
34-
feed_dict.Add(a, 3);
35-
feed_dict.Add(b, 2);
40+
feed_dict.Add(a, 3.0f);
41+
feed_dict.Add(b, 2.0f);
3642

37-
sess.run(c, feed_dict);
43+
var o = sess.run(c, feed_dict);
3844
}
3945
}
4046
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL