Skip to content

Commit 48282b8

Browse files
committed
fixed SciSharp#175
1 parent d61ccf0 commit 48282b8

11 files changed

Lines changed: 83 additions & 77 deletions

File tree

data/linear_regression.zip

16.9 KB
Binary file not shown.

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ public static (Tensor, Tensor) _MulGrad(Operation op, Tensor grad)
4747
x = math_ops.conj(x);
4848
y = math_ops.conj(y);
4949

50-
var r1 = math_ops.reduce_sum(gen_math_ops.mul(grad, y), rx);
51-
var r2 = math_ops.reduce_sum(gen_math_ops.mul(x, grad), ry);
52-
53-
return (gen_array_ops.reshape(r1, sx), gen_array_ops.reshape(r2, sy));
50+
var mul1 = gen_math_ops.mul(grad, y);
51+
var mul2 = gen_math_ops.mul(x, grad);
52+
var reduce_sum1 = math_ops.reduce_sum(mul1, rx);
53+
var reduce_sum2 = math_ops.reduce_sum(mul2, ry);
54+
var reshape1 = gen_array_ops.reshape(reduce_sum1, sx);
55+
var reshape2 = gen_array_ops.reshape(reduce_sum2, sy);
56+
57+
return (reshape1, reshape2);
5458
}
5559

5660
public static (Tensor, Tensor) _SubGrad(Operation op, Tensor grad)
@@ -129,9 +133,12 @@ public static (Tensor, Tensor) _PowGrad(Operation op, Tensor grad)
129133
var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy);
130134
x = math_ops.conj(x);
131135
y = math_ops.conj(y);
132-
y = math_ops.conj(z);
133-
var gx = gen_array_ops.reshape(math_ops.reduce_sum(grad * y * gen_math_ops.pow(x, y - 1.0), rx), sx);
134-
Tensor log_x = null;
136+
z = math_ops.conj(z);
137+
var pow = gen_math_ops.pow(x, y - 1.0f);
138+
var mul = grad * y * pow;
139+
var reduce_sum = math_ops.reduce_sum(mul, rx);
140+
var gx = gen_array_ops.reshape(reduce_sum, sx);
141+
135142
// Avoid false singularity at x = 0
136143
Tensor mask = null;
137144
if (x.dtype.is_complex())
@@ -142,8 +149,10 @@ public static (Tensor, Tensor) _PowGrad(Operation op, Tensor grad)
142149
var safe_x = array_ops.where(mask, x, ones);
143150
var x1 = gen_array_ops.log(safe_x);
144151
var y1 = array_ops.zeros_like(x);
145-
log_x = array_ops.where(mask, x1, y1);
146-
var gy = gen_array_ops.reshape(math_ops.reduce_sum(grad * z * log_x, ry), sy);
152+
var log_x = array_ops.where(mask, x1, y1);
153+
var mul1 = grad * z * log_x;
154+
var reduce_sum1 = math_ops.reduce_sum(mul1, ry);
155+
var gy = gen_array_ops.reshape(reduce_sum1, sy);
147156

148157
return (gx, gy);
149158
}

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[]
196196

197197
_create_op_helper(op, true);
198198

199-
Console.Write($"create_op: {op_type} '{node_def.Name}'");
199+
/*Console.Write($"create_op: {op_type} '{node_def.Name}'");
200200
Console.Write($", inputs: {(inputs.Length == 0 ? "empty" : String.Join(", ", inputs.Select(x => x.name)))}");
201201
Console.Write($", control_inputs: {(control_inputs.Length == 0 ? "empty" : String.Join(", ", control_inputs.Select(x => x.name)))}");
202202
Console.Write($", outputs: {(op.outputs.Length == 0 ? "empty" : String.Join(", ", op.outputs.Select(x => x.name)))}");
203-
Console.WriteLine();
203+
Console.WriteLine();*/
204204

205205
return op;
206206
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Newtonsoft.Json;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Runtime.InteropServices;
@@ -15,7 +14,7 @@ public partial class Operation
1514

1615
private Tensor[] _outputs;
1716
public Tensor[] outputs => _outputs;
18-
[JsonIgnore]
17+
//[JsonIgnore]
1918
public Tensor output => _outputs.FirstOrDefault();
2019

2120
public int NumControlOutputs => c_api.TF_OperationNumControlOutputs(_handle);

src/TensorFlowNET.Core/Operations/Operation.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Google.Protobuf.Collections;
2-
using Newtonsoft.Json;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -13,15 +12,15 @@ public partial class Operation : ITensorOrOperation
1312
private readonly IntPtr _handle; // _c_op in python
1413

1514
private Graph _graph;
16-
[JsonIgnore]
15+
//[JsonIgnore]
1716
public Graph graph => _graph;
18-
[JsonIgnore]
17+
//[JsonIgnore]
1918
public int _id => _id_value;
20-
[JsonIgnore]
19+
//[JsonIgnore]
2120
public int _id_value;
2221

2322
public string type => OpType;
24-
[JsonIgnore]
23+
//[JsonIgnore]
2524
public Operation op => this;
2625
public TF_DataType dtype => TF_DataType.DtInvalid;
2726
private Status status = new Status();

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,4 @@ Upgraded to TensorFlow 1.13 RC2.
5252
<Content CopyToOutputDirectory="PreserveNewest" Include="./runtimes/win-x64/native/tensorflow.dll" Link="tensorflow.dll" Pack="true" PackagePath="runtimes/win-x64/native/tensorflow.dll" />
5353
</ItemGroup>
5454

55-
<ItemGroup>
56-
<Reference Include="Newtonsoft.Json">
57-
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
58-
</Reference>
59-
</ItemGroup>
60-
6155
</Project>

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Newtonsoft.Json;
2-
using NumSharp.Core;
1+
using NumSharp.Core;
32
using System;
43
using System.Collections.Generic;
54
using System.Linq;
@@ -18,13 +17,13 @@ public partial class Tensor : IDisposable, ITensorOrOperation
1817
private readonly IntPtr _handle;
1918

2019
private int _id;
21-
[JsonIgnore]
20+
//[JsonIgnore]
2221
public int Id => _id;
23-
[JsonIgnore]
22+
//[JsonIgnore]
2423
public Graph graph => op?.graph;
25-
[JsonIgnore]
24+
//[JsonIgnore]
2625
public Operation op { get; }
27-
[JsonIgnore]
26+
//[JsonIgnore]
2827
public Tensor[] outputs => op.outputs;
2928

3029
/// <summary>
@@ -104,7 +103,7 @@ public int rank
104103

105104
public int NDims => rank;
106105

107-
[JsonIgnore]
106+
//[JsonIgnore]
108107
public Operation[] Consumers => consumers();
109108

110109
public string Device => op.Device;

src/TensorFlowNET.Core/ops.py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public static Func<Operation, Tensor, Tensor[]> get_gradient_function(Operation
351351

352352
return (oper, out_grads) =>
353353
{
354-
Console.WriteLine($"get_gradient_function: {oper.type} '{oper.name}'");
354+
// Console.WriteLine($"get_gradient_function: {oper.type} '{oper.name}'");
355355

356356
switch (oper.type)
357357
{

test/TensorFlowNET.Examples/LinearRegression.cs

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Newtonsoft.Json;
2-
using NumSharp.Core;
1+
using NumSharp.Core;
32
using System;
43
using System.Collections.Generic;
54
using System.Text;
@@ -13,64 +12,44 @@ namespace TensorFlowNET.Examples
1312
/// </summary>
1413
public class LinearRegression : Python, IExample
1514
{
16-
private NumPyRandom rng = np.random;
15+
NumPyRandom rng = np.random;
16+
17+
// Parameters
18+
float learning_rate = 0.01f;
19+
int training_epochs = 1000;
20+
int display_step = 50;
1721

1822
public void Run()
1923
{
20-
var graph = tf.Graph().as_default();
21-
22-
// Parameters
23-
float learning_rate = 0.01f;
24-
int training_epochs = 1000;
25-
int display_step = 10;
26-
2724
// Training Data
2825
var train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
2926
7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
3027
var train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
3128
2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
3229
var n_samples = train_X.shape[0];
3330

31+
var graph = tf.Graph().as_default();
32+
3433
// tf Graph Input
3534
var X = tf.placeholder(tf.float32);
3635
var Y = tf.placeholder(tf.float32);
3736

3837
// Set model weights
39-
//var rnd1 = rng.randn<float>();
40-
//var rnd2 = rng.randn<float>();
38+
// We can set a fixed init value in order to debug
39+
// var rnd1 = rng.randn<float>();
40+
// var rnd2 = rng.randn<float>();
4141
var W = tf.Variable(-0.06f, name: "weight");
4242
var b = tf.Variable(-0.73f, name: "bias");
4343

44-
var mul = tf.multiply(X, W);
45-
var pred = tf.add(mul, b);
44+
// Construct a linear model
45+
var pred = tf.add(tf.multiply(X, W), b);
4646

4747
// Mean squared error
48-
var sub = pred - Y;
49-
var pow = tf.pow(sub, 2.0f);
50-
51-
var reduce = tf.reduce_sum(pow);
52-
var cost = reduce / (2.0f * n_samples);
48+
var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
5349

5450
// radient descent
5551
// Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
56-
var grad = tf.train.GradientDescentOptimizer(learning_rate);
57-
var optimizer = grad.minimize(cost);
58-
59-
//tf.train.export_meta_graph(filename: "linear_regression.meta.bin");
60-
// import meta
61-
// var new_saver = tf.train.import_meta_graph("linear_regression.meta.bin");
62-
var text = JsonConvert.SerializeObject(graph, new JsonSerializerSettings
63-
{
64-
Formatting = Formatting.Indented
65-
});
66-
67-
/*var cost = graph.OperationByName("truediv").output;
68-
var pred = graph.OperationByName("Add").output;
69-
var optimizer = graph.OperationByName("GradientDescent");
70-
var X = graph.OperationByName("Placeholder").output;
71-
var Y = graph.OperationByName("Placeholder_1").output;
72-
var W = graph.OperationByName("weight").output;
73-
var b = graph.OperationByName("bias").output;*/
52+
var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
7453

7554
// Initialize the variables (i.e. assign their default value)
7655
var init = tf.global_variables_initializer();
@@ -89,22 +68,33 @@ public void Run()
8968
sess.run(optimizer,
9069
new FeedItem(X, x),
9170
new FeedItem(Y, y));
92-
var rW = sess.run(W);
9371
}
9472

9573
// Display logs per epoch step
96-
/*if ((epoch + 1) % display_step == 0)
74+
if ((epoch + 1) % display_step == 0)
9775
{
9876
var c = sess.run(cost,
9977
new FeedItem(X, train_X),
10078
new FeedItem(Y, train_Y));
101-
var rW = sess.run(W);
102-
Console.WriteLine($"Epoch: {epoch + 1} cost={c} " +
103-
$"W={rW} b={sess.run(b)}");
104-
}*/
79+
Console.WriteLine($"Epoch: {epoch + 1} cost={c} " + $"W={sess.run(W)} b={sess.run(b)}");
80+
}
10581
}
10682

10783
Console.WriteLine("Optimization Finished!");
84+
var training_cost = sess.run(cost,
85+
new FeedItem(X, train_X),
86+
new FeedItem(Y, train_Y));
87+
Console.WriteLine($"Training cost={training_cost} W={sess.run(W)} b={sess.run(b)}");
88+
89+
// Testing example
90+
var test_X = np.array(6.83f, 4.668f, 8.9f, 7.91f, 5.7f, 8.7f, 3.1f, 2.1f);
91+
var test_Y = np.array(1.84f, 2.273f, 3.2f, 2.831f, 2.92f, 3.24f, 1.35f, 1.03f);
92+
Console.WriteLine("Testing... (Mean square loss Comparison)");
93+
var testing_cost = sess.run(tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * test_X.shape[0]),
94+
new FeedItem(X, test_X),
95+
new FeedItem(Y, test_Y));
96+
Console.WriteLine($"Testing cost={testing_cost}");
97+
Console.WriteLine($"Absolute mean square loss difference: {Math.Abs((float)training_cost - (float)testing_cost)}");
10898
});
10999
}
110100
}

test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
109
<PackageReference Include="NumSharp" Version="0.7.3" />
1110
<PackageReference Include="TensorFlow.NET" Version="0.3.0" />
1211
</ItemGroup>

0 commit comments

Comments
 (0)