Skip to content

Commit b03a52d

Browse files
committed
add Tensor.Operator +
1 parent 53c3a4e commit b03a52d

13 files changed

Lines changed: 80 additions & 26 deletions

File tree

‎docs/source/FrontCover.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# The Definitive Guide to Tensorflow.NET
2-
# Tensorflow.NET 权威指南
1+
# The Definitive Guide to TensorFlow.NET
2+
# TensorFlow.NET 权威指南
33

44

55

‎docs/source/Preface.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515

1616

17-
Why do I start the Tensorflow.NET project?
17+
Why do I start the TensorFlow.NET project?
1818

19-
我为什么会写Tensorflow.NET?
19+
我为什么会写TensorFlow.NET?
2020

2121
再过几天就是2018年圣诞节,看着孩子一天天长大并懂事,感慨时间过得太快。IT技术更新换代比以往任何时候都更快,各种前后端技术纷纷涌现。大数据,人工智能和区块链,容器技术和微服务,分布式计算和无服务器技术,让人眼花缭乱。Amazon AI服务接口宣称不需要具有任何机器学习经验的工程师就能使用,让像我这样刚静下心来学习了两年并打算将来转行做AI架构的想法泼了一桶凉水。
2222

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ public static partial class tf
99
{
1010
public static Tensor constant(NDArray nd, string name = "Const", bool verify_shape = false)
1111
{
12-
var t = constant_op.Create(nd, name, verify_shape);
13-
/*var graph = tf.get_default_graph();
14-
var tensor = new Tensor(nd);
15-
var op = graph.NewOperation("Const", name, tensor);*/
16-
17-
return t;
12+
return constant_op.Create(nd, name, verify_shape);
1813
}
1914
}
2015
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public unsafe Operation _apply_op_helper(string op_type_name, string name = "",
5959

6060
if (!String.IsNullOrEmpty(input_arg.TypeAttr))
6161
{
62-
attrs[input_arg.TypeAttr] = DataType.DtFloat;
62+
attrs[input_arg.TypeAttr] = (keywords[input_name] as Tensor).dtype;
6363
}
6464

6565
if (input_arg.IsRef)
@@ -92,7 +92,7 @@ public unsafe Operation _apply_op_helper(string op_type_name, string name = "",
9292
switch (attr_def.Type)
9393
{
9494
case "type":
95-
attr_value.Type = _MakeType(value, attr_def);
95+
attr_value.Type = _MakeType((TF_DataType)value, attr_def);
9696
break;
9797
case "shape":
9898
attr_value.Shape = new TensorShapeProto();
@@ -127,9 +127,9 @@ public unsafe Operation _apply_op_helper(string op_type_name, string name = "",
127127
return op;
128128
}
129129

130-
public DataType _MakeType(Object v, AttrDef attr_def)
130+
public DataType _MakeType(TF_DataType v, AttrDef attr_def)
131131
{
132-
return DataType.DtFloat;
132+
return v.as_datatype_enum();
133133
}
134134
}
135135
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public static Tensor placeholder(TF_DataType dtype, TensorShape shape = null)
3737

3838
private static OpDefLibrary _InitOpDefLibrary()
3939
{
40-
// c_api.TF_GraphGetOpDef(g.Handle, op_type_name, buffer.Handle, status.Handle);
4140
var bytes = File.ReadAllBytes("Operations/op_list_proto_array.bin");
4241
var op_list = OpList.Parser.ParseFrom(bytes);
4342
var op_def_lib = new OpDefLibrary();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ public static Tensor add(Tensor a, Tensor b)
1717

1818
var _op = _op_def_lib._apply_op_helper("Add", name: "add", keywords: keywords);
1919

20-
var tensor = new Tensor(_op, 0, TF_DataType.TF_FLOAT);
21-
22-
return tensor;
20+
return new Tensor(_op, 0, _op.OutputType(0));
2321
}
2422

2523
private static OpDefLibrary _InitOpDefLibrary()

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ public static unsafe IntPtr _create_c_op(Graph graph, NodeDef node_def, List<Ten
2828
var op_desc = graph.NewOperation(node_def.Op, node_def.Name);
2929

3030
// Add inputs
31-
if(inputs != null && inputs.Count > 0)
31+
if(inputs != null)
3232
{
33-
/*foreach (var op_input in inputs)
33+
foreach (var op_input in inputs)
3434
{
35-
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
36-
}*/
37-
38-
c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
35+
bool isList = false;
36+
if (!isList)
37+
{
38+
c_api.TF_AddInput(op_desc, op_input._as_tf_output());
39+
}
40+
else
41+
{
42+
c_api.TF_AddInputList(op_desc, inputs.Select(x => x._as_tf_output()).ToArray(), inputs.Count);
43+
}
44+
}
3945
}
4046

4147
var status = new Status();

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ private unsafe object[] _call_tf_sessionrun(KeyValuePair<TF_Output, Tensor>[] fe
131131
case TF_DataType.TF_FLOAT:
132132
result[i] = *(float*)c_api.TF_TensorData(output_values[i]);
133133
break;
134+
case TF_DataType.TF_INT32:
135+
result[i] = *(int*)c_api.TF_TensorData(output_values[i]);
136+
break;
134137
default:
135138
throw new NotImplementedException("can't get output");
136139
break;

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Docs: https://tensorflownet.readthedocs.io</Description>
1919
<AssemblyVersion>0.0.2.0</AssemblyVersion>
2020
<PackageReleaseNotes></PackageReleaseNotes>
21+
<LangVersion>7.2</LangVersion>
2122
</PropertyGroup>
2223

2324
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public partial class Tensor
8+
{
9+
public static Tensor operator +(Tensor t1, Tensor t2)
10+
{
11+
return gen_math_ops.add(t1, t2);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)