Skip to content

Commit 3b93c7b

Browse files
committed
linear regression test 1
1 parent 7d0f38a commit 3b93c7b

18 files changed

Lines changed: 125 additions & 111 deletions

src/TensorFlowNET.Core/APIs/tf.math.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Tensor subtract<T>(Tensor x, T[] y, string name = "") where T : st
1818
public static Tensor divide<T>(Tensor x, T[] y, string name = "") where T : struct
1919
=> x / ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y");
2020

21-
public static Tensor pow(Tensor x, double y) => gen_math_ops.pow(x, y);
21+
public static Tensor pow<T1, T2>(T1 x, T2 y) => gen_math_ops.pow(x, y);
2222

2323
/// <summary>
2424
/// Computes the sum of elements across dimensions of a tensor.

src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ private static Tensor[] _DefaultGradYs(Tensor[] grad_ys, Tensor[] ys, bool coloc
357357
if (y.dtype.is_complex())
358358
throw new TypeAccessException($"Gradients of complex tensors must set grad_ys (y.dtype = {y.dtype})");
359359
var shape = array_ops.shape(y);
360-
var constant = constant_op.constant(1.0, name: $"grad_ys_{i}");
360+
var constant = constant_op.constant(1.0f, name: $"grad_ys_{i}");
361361
var fill = gen_array_ops.fill(shape, constant);
362362
new_grad_ys.Add(fill);
363363
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public ITensorOrOperation _get_operation_by_tf_operation(IntPtr tf_oper)
4141
public Operation _create_op_from_tf_operation(IntPtr c_op, bool compute_device = true)
4242
{
4343
var ret = new Operation(c_op);
44+
_add_op(ret);
4445

4546
var name_key = ret.name.ToLower();
4647
if (!_names_in_use.ContainsKey(name_key))

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ private void _create_op_helper(Operation op, bool compute_device = true)
212212

213213
public void _add_op(Operation op)
214214
{
215+
op._id_value = _next_id();
215216
_nodes_by_id[op._id] = op;
216217
_nodes_by_name[op.name] = op;
217218
_version = Math.Max(_version, op._id);

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public partial class Operation : ITensorOrOperation
1313

1414
public Graph graph { get; }
1515
public int _id => _id_value;
16-
private int _id_value;
16+
public int _id_value;
1717

1818
public string type => OpType;
1919
public Operation op => this;
@@ -46,8 +46,6 @@ public Operation(IntPtr handle)
4646
_outputs = new Tensor[NumOutputs];
4747
for (int i = 0; i < NumOutputs; i++)
4848
_outputs[i] = new Tensor(this, i, OutputType(i));
49-
50-
graph._add_op(this);
5149
}
5250

5351
public Operation(Graph g, string opType, string oper_name)
@@ -100,8 +98,6 @@ public Operation(NodeDef node_def, Graph g, Tensor[] inputs = null, TF_DataType[
10098
}
10199

102100
// This will be set by self.inputs.
103-
104-
_id_value = graph._next_id();
105101
if(op_def == null)
106102
op_def = g.GetOpDef(node_def.Op);
107103

src/TensorFlowNET.Core/Python.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public static float time()
8888

8989
public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
9090
{
91-
int index = 0;
92-
yield return(t1.Data<T>(index), t2.Data<T>(index));
91+
for (int i = 0; i < t1.size; i++)
92+
yield return (t1.Data<T>(i), t2.Data<T>(i));
9393
}
9494

9595
public static IEnumerable<(T1, T2)> zip<T1, T2>(IList<T1> t1, IList<T2> t2)

src/TensorFlowNET.Core/Sessions/BaseSession.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private NDArray _run(object fetches, FeedItem[] feed_dict = null)
5959
{
6060
var subfeed_t = _graph.as_graph_element(subfeed, allow_tensor: true, allow_operation: false);
6161
var subfeed_dtype = subfeed_t.dtype.as_numpy_datatype();
62+
6263
switch (subfeed_val)
6364
{
6465
case IntPtr pointer:
@@ -86,6 +87,7 @@ private NDArray _run(object fetches, FeedItem[] feed_dict = null)
8687
Console.WriteLine($"can't handle data type of subfeed_val");
8788
throw new NotImplementedException("_run subfeed");
8889
}
90+
8991
feed_map[subfeed_t.name] = (subfeed_t, subfeed_val);
9092
}
9193
}

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Upgraded to TensorFlow 1.13 RC2.
4545

4646
<ItemGroup>
4747
<PackageReference Include="Google.Protobuf" Version="3.6.1" />
48-
<PackageReference Include="NumSharp" Version="0.7.2" />
48+
<PackageReference Include="NumSharp" Version="0.7.3" />
4949
</ItemGroup>
5050

5151
<ItemGroup>

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

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,45 @@ private IntPtr Allocate(NDArray nd)
5252
var dataType = ToTFDataType(nd.dtype);
5353
// shape
5454
var dims = nd.shape.Select(x => (long)x).ToArray();
55-
55+
var nd1 = nd.ravel();
5656
switch (nd.dtype.Name)
5757
{
5858
case "Int16":
59-
Marshal.Copy(nd.ravel().Data<short>(), 0, dotHandle, nd.size);
59+
Marshal.Copy(nd1.Data<short>(), 0, dotHandle, nd.size);
6060
break;
6161
case "Int32":
62-
Marshal.Copy(nd.ravel().Data<int>(), 0, dotHandle, nd.size);
62+
Marshal.Copy(nd1.Data<int>(), 0, dotHandle, nd.size);
6363
break;
6464
case "Single":
65-
Marshal.Copy(nd.ravel().Data<float>(), 0, dotHandle, nd.size);
65+
Marshal.Copy(nd1.Data<float>(), 0, dotHandle, nd.size);
66+
/*if (nd.size > 1)
67+
{
68+
var bb = nd.Data<byte>();
69+
var bytes = Marshal.AllocHGlobal(bb.Length);
70+
Marshal.Copy(bb, 0, bytes, bb.Length);
71+
ulong bytes_len = c_api.TF_StringEncodedSize((ulong)bb.Length);
72+
var dataTypeByte = ToTFDataType(nd.dtype);
73+
// shape
74+
var dims2 = nd.shape.Select(x => (long)x).ToArray();
75+
76+
var tfHandle2 = c_api.TF_AllocateTensor(dataTypeByte,
77+
dims2,
78+
nd.ndim,
79+
bytes_len + sizeof(Int64));
80+
81+
dotHandle = c_api.TF_TensorData(tfHandle2);
82+
Marshal.WriteInt64(dotHandle, 0);
83+
c_api.TF_StringEncode(bytes, (ulong)bb.Length, dotHandle + sizeof(Int64), bytes_len, status);
84+
return tfHandle2;
85+
}
86+
else
87+
{
88+
Marshal.Copy(nd1.Data<float>(), 0, dotHandle, nd.size);
89+
}*/
90+
6691
break;
6792
case "Double":
68-
Marshal.Copy(nd.ravel().Data<double>(), 0, dotHandle, nd.size);
93+
Marshal.Copy(nd1.Data<double>(), 0, dotHandle, nd.size);
6994
break;
7095
//case "Byte":
7196
/*var bb = nd.Data<byte>();
@@ -119,7 +144,7 @@ private IntPtr Allocate(NDArray nd)
119144
dims,
120145
dims.Length,
121146
dotHandle,
122-
size,
147+
(UIntPtr)size,
123148
deallocator,
124149
ref deallocator_called);
125150

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

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,83 +6,70 @@ namespace Tensorflow
66
{
77
public partial class Tensor
88
{
9-
public static Tensor operator +(Tensor x, Tensor y)
10-
{
11-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "add", new Tensor[] { x, y }), scope =>
12-
{
13-
return gen_math_ops.add(x, y, scope);
14-
});
15-
}
16-
17-
public static Tensor operator +(Tensor x, int y)
18-
{
19-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "add", new object[] { x, y }), scope =>
20-
{
21-
var y1 = ops.convert_to_tensor(y, x.dtype.as_base_dtype(), name: "y");
22-
return gen_math_ops.add(x, y1, scope);
23-
});
24-
}
9+
public static Tensor operator +(Tensor x, Tensor y) => BinaryOpWrapper("add", x, y);
10+
public static Tensor operator +(Tensor x, int y) => BinaryOpWrapper("add", x, y);
2511

2612
public static Tensor operator -(Tensor t1) => gen_math_ops.neg(t1);
27-
public static Tensor operator -(Tensor t1, Tensor t2) => gen_math_ops.sub(t1, t2);
28-
public static Tensor operator -(Tensor t1, int t2) => gen_math_ops.sub(t1, t2);
29-
public static Tensor operator -(Tensor t1, double t2) => gen_math_ops.sub(t1, t2);
3013

31-
public static Tensor operator *(double x, Tensor y)
32-
{
33-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "mul", new { x, y }),
34-
scope =>
35-
{
36-
var x1 = ops.convert_to_tensor(x, y.dtype.as_base_dtype(), name: "x");
37-
return gen_math_ops.mul(x1, y, name: scope);
38-
});
39-
}
14+
public static Tensor operator -(Tensor x, Tensor y) => BinaryOpWrapper("sub", x, y);
15+
public static Tensor operator -(Tensor x, int y) => BinaryOpWrapper("sub", x, y);
16+
public static Tensor operator -(Tensor x, double y) => BinaryOpWrapper("sub", x, y);
4017

41-
public static Tensor operator *(Tensor x, Tensor y)
42-
{
43-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "mul", new Tensor[] { x, y }), scope =>
44-
{
45-
return gen_math_ops.mul(x, y, name: scope);
46-
});
47-
}
18+
public static Tensor operator *(float x, Tensor y) => BinaryOpWrapper("mul", x, y);
19+
public static Tensor operator *(double x, Tensor y) => BinaryOpWrapper("mul", x, y);
20+
public static Tensor operator *(Tensor x, Tensor y) => BinaryOpWrapper("mul", x, y);
21+
public static Tensor operator *(Tensor x, int y) => BinaryOpWrapper("mul", x, y);
4822

49-
public static Tensor operator *(Tensor x, int y)
50-
{
51-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "mul", new object[] { x, y }), scope =>
52-
{
53-
var y1 = ops.convert_to_tensor(y, x.dtype.as_base_dtype(), name: "y");
54-
return gen_math_ops.mul(x, y1, name: scope);
55-
});
56-
}
23+
public static Tensor operator /(Tensor x, Tensor y) => BinaryOpWrapper("truediv", x, y);
24+
public static Tensor operator /(Tensor x, float y) => BinaryOpWrapper("truediv", x, y);
25+
public static Tensor operator /(Tensor x, double y) => BinaryOpWrapper("truediv", x, y);
5726

58-
public static Tensor operator /(Tensor x, Tensor y)
59-
{
60-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("truediv/", "truediv", new Tensor[] { x, y }), scope =>
61-
{
62-
return gen_math_ops.real_div(x, y, scope);
63-
});
64-
}
27+
public static Tensor operator %(Tensor x, Tensor y) => BinaryOpWrapper("mod", x, y);
6528

66-
public static Tensor operator /(Tensor x, double y)
67-
{
68-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("truediv/", "truediv", new object[] { x, y }), scope =>
69-
{
70-
var y1 = ops.convert_to_tensor(y, dtype: x.dtype.as_base_dtype(), name: "y");
71-
return gen_math_ops.real_div(x, y1, scope);
72-
});
73-
}
29+
public static Tensor operator >(Tensor x, int y) => gen_array_ops.greater(x, y);
30+
public static Tensor operator >(Tensor x, double y) => gen_array_ops.greater(x, y);
31+
public static Tensor operator <(Tensor x, int y) => gen_array_ops.less(x, y);
32+
public static Tensor operator <(Tensor x, double y) => gen_array_ops.less(x, y);
7433

75-
public static Tensor operator %(Tensor x, Tensor y)
34+
private static Tensor BinaryOpWrapper<Tx, Ty>(string name, Tx x, Ty y)
7635
{
77-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope("", "mod", new object[] { x, y }), scope =>
36+
TF_DataType dtype = TF_DataType.DtInvalid;
37+
if (x is Tensor tl)
38+
dtype = tl.dtype.as_base_dtype();
39+
if( y is Tensor tr)
40+
dtype = tr.dtype.as_base_dtype();
41+
42+
var namescope = new ops.name_scope("", name, new { x, y });
43+
return Python.with<ops.name_scope, Tensor>(namescope, scope =>
7844
{
79-
return gen_math_ops.floor_mod(x, y, scope);
45+
Tensor result = null;
46+
var x1 = ops.convert_to_tensor(x, dtype: dtype, name: "x");
47+
var y1 = ops.convert_to_tensor(y, dtype: dtype, name: "y");
48+
49+
switch (name)
50+
{
51+
case "add":
52+
result = gen_math_ops.add(x1, y1, name: scope);
53+
break;
54+
case "truediv":
55+
result = gen_math_ops.real_div(x1, y1, name: scope);
56+
break;
57+
case "mul":
58+
result = gen_math_ops.mul(x1, y1, name: scope);
59+
break;
60+
case "sub":
61+
result = gen_math_ops.sub(x1, y1, name: scope);
62+
break;
63+
case "mod":
64+
result = gen_math_ops.floor_mod(x1, y1, name: scope);
65+
break;
66+
default:
67+
throw new NotImplementedException($"BinaryOpWrapper: {name} - {typeof(Tx).Name}, {typeof(Ty)}");
68+
}
69+
70+
return result;
8071
});
72+
8173
}
82-
83-
public static Tensor operator >(Tensor x, int y) => gen_array_ops.greater(x, y);
84-
public static Tensor operator >(Tensor x, double y) => gen_array_ops.greater(x, y);
85-
public static Tensor operator <(Tensor x, int y) => gen_array_ops.less(x, y);
86-
public static Tensor operator <(Tensor x, double y) => gen_array_ops.less(x, y);
8774
}
8875
}

0 commit comments

Comments
 (0)