Skip to content

Commit 5d91b21

Browse files
committed
Update tiled net.
1 parent 72f9c45 commit 5d91b21

9 files changed

+132
-105
lines changed

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/TiledNet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void AdjustLearningRate(double learningRate)
8181
/// <returns>The task.</returns>
8282
public async Task Initialize()
8383
{
84-
var initialAdamIteration = 462;
84+
var initialAdamIteration = 1726;
8585
var model = new TiledNetwork.TiledNetwork(this.numLayers, this.numNodes, this.numFeatures, this.learningRate, this.clipValue, "tilednet");
8686
model.Parameters.AdamIteration = initialAdamIteration;
8787
this.TiledNetwork = model;
@@ -122,7 +122,7 @@ public void SaveWeights()
122122
/// </summary>
123123
public void ApplyWeights()
124124
{
125-
var guid = "tiled_0a7edd07-8034-4d91-b7fd-68fb84a22a48_462";
125+
var guid = "tiled_717e1887-0217-447c-bc39-33c64d2c5994_1726";
126126
var dir = $"E:\\vnnstore\\{guid}";
127127
for (int i = 0; i < this.modelLayers.Count; ++i)
128128
{

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/TiledNetTrainer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public async Task Train()
1818

1919
var pngFiles = Directory.GetFiles(@"E:\images\inputs\svg", "*.png");
2020

21-
Random random = new Random(8);
21+
Random random = new Random(9);
2222
var files = pngFiles.OrderBy(x => random.Next()).ToArray();
2323
uint i = 0;
2424
await files.WithRepeatAsync(async (pngFile, token) =>
@@ -111,7 +111,7 @@ await files.WithRepeatAsync(async (pngFile, token) =>
111111
Console.WriteLine($"Loss: {lossAndGradient[1, 0].Item1[0, 0]}, {lossAndGradient[1, 1].Item1[0, 0]}, {lossAndGradient[1, 2].Item1[0, 0]}");
112112
Console.WriteLine($"Loss: {lossAndGradient[2, 0].Item1[0, 0]}, {lossAndGradient[2, 1].Item1[0, 0]}, {lossAndGradient[2, 2].Item1[0, 0]}");
113113
//Console.WriteLine($"O1 X: {o1[0, 0]}, O1 Y: {o1[0, 1]}, Loss: {loss[0, 0]}, {loss0[0, 0]}, {loss1[0, 0]}");
114-
//await net.Backward(lossAndGradient);
114+
await net.Backward(lossAndGradient);
115115
//net.ApplyGradients();
116116
//}
117117

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/TiledNetwork/TiledNetwork.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,14 @@ public async Task<Matrix> AutomaticBackwardPropagate((Matrix, Matrix)[,] lossAnd
361361
{
362362
for (int j = 0; j < lossAndGradient.GetLength(1); j++)
363363
{
364-
grad[i, j] = lossAndGradient[i, j].Item2;
364+
if ((i == 0 && j == 0) || (i == 0 && j == 1))
365+
{
366+
grad[i, j] = lossAndGradient[i, j].Item2;
367+
}
368+
else
369+
{
370+
grad[i, j] = new Matrix(1, 2);
371+
}
365372
}
366373
}
367374

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/VectorNetwork/RMAD/ElementwiseVectorCartesianTiledSummationOperation.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,14 @@ public override BackwardResult Backward(Matrix dOutput)
207207
}
208208
});
209209

210+
var di1 = CommonMatrixUtils.PieceTogether(this.dInput1);
211+
var di2 = CommonMatrixUtils.PieceTogether(this.dInput2);
212+
var dw = CommonMatrixUtils.PieceTogetherExactly(this.dWeights);
213+
210214
return new BackwardResultBuilder()
211-
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput1))
212-
.AddInputGradient(CommonMatrixUtils.PieceTogether(this.dInput2))
213-
.AddInputGradient(CommonMatrixUtils.PieceTogetherExactly(this.dWeights))
215+
.AddInputGradient(di1)
216+
.AddInputGradient(di2)
217+
.AddInputGradient(dw)
214218
.Build();
215219
}
216220

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/VectorNetwork/RMAD/ElementwiseVectorConstituentMonoMultiplyOperation.cs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,16 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
7676
double[] dSumY_dResultMagnitude = new double[input2.Rows / 2];
7777
double[] dResultMagnitude_dWeight = new double[input2.Rows / 2];
7878

79-
double dInputMag_dOutputMag = 0.0d;
80-
double dInputMag_dOutputAngle = 0.0d;
81-
double dInputAngle_dOutputMag = 0.0d;
82-
double dInputAngle_dOutputAngle = 0.0d;
83-
double dInput2Mag_dOutputMag = 0.0d;
84-
double dInput2Mag_dOutputAngle = 0.0d;
85-
double dInput2Angle_dOutputMag = 0.0d;
86-
double dInput2Angle_dOutputAngle = 0.0d;
87-
double dWeight_dOutputMag = 0.0d;
88-
double dWeight_dOutputAngle = 0.0d;
79+
double[] dInputMag_dOutputMag = new double[input2.Rows / 2];
80+
double[] dInputMag_dOutputAngle = new double[input2.Rows / 2];
81+
double[] dInputAngle_dOutputMag = new double[input2.Rows / 2];
82+
double[] dInputAngle_dOutputAngle = new double[input2.Rows / 2];
83+
double[] dInput2Mag_dOutputMag = new double[input2.Rows / 2];
84+
double[] dInput2Mag_dOutputAngle = new double[input2.Rows / 2];
85+
double[] dInput2Angle_dOutputMag = new double[input2.Rows / 2];
86+
double[] dInput2Angle_dOutputAngle = new double[input2.Rows / 2];
87+
double[] dWeight_dOutputMag = new double[input2.Rows / 2];
88+
double[] dWeight_dOutputAngle = new double[input2.Rows / 2];
8989

9090
for (int k = 0; k < input2.Rows / 2; k++)
9191
{
@@ -183,63 +183,63 @@ public Matrix Forward(Matrix input1, Matrix input2, Matrix weights)
183183

184184
for (int k = 0; k < input2.Rows / 2; k++)
185185
{
186-
dInputMag_dOutputMag +=
186+
dInputMag_dOutputMag[k] =
187187
dCombinedMagnitude_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dMagnitude[k] +
188188
dCombinedMagnitude_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dMagnitude[k] +
189189
dCombinedMagnitude_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dMagnitude[k] +
190190
dCombinedMagnitude_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dMagnitude[k];
191191

192-
dInput2Mag_dOutputMag +=
192+
dInput2Mag_dOutputMag[k] =
193193
dCombinedMagnitude_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX1[k] * dX2_dWMagnitude[k] +
194194
dCombinedMagnitude_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY1[k] * dY2_dWMagnitude[k] +
195195
dCombinedMagnitude_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY1[k] * dY2_dWMagnitude[k] +
196196
dCombinedMagnitude_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX1[k] * dX2_dWMagnitude[k];
197197

198-
dInputMag_dOutputAngle +=
198+
dInputMag_dOutputAngle[k] =
199199
dCombinedAngle_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dMagnitude[k] +
200200
dCombinedAngle_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dMagnitude[k] +
201201
dCombinedAngle_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dMagnitude[k] +
202202
dCombinedAngle_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dMagnitude[k];
203203

204-
dInput2Mag_dOutputAngle +=
204+
dInput2Mag_dOutputAngle[k] =
205205
dCombinedAngle_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWMagnitude[k] +
206206
dCombinedAngle_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWMagnitude[k] +
207207
dCombinedAngle_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWMagnitude[k] +
208208
dCombinedAngle_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWMagnitude[k];
209209

210-
dInputAngle_dOutputMag +=
210+
dInputAngle_dOutputMag[k] =
211211
dCombinedMagnitude_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dAngle[k] +
212212
dCombinedMagnitude_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dAngle[k] +
213213
dCombinedMagnitude_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dAngle[k] +
214214
dCombinedMagnitude_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dAngle[k];
215215

216-
dInput2Angle_dOutputMag +=
216+
dInput2Angle_dOutputMag[k] =
217217
dCombinedMagnitude_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWAngle[k] +
218218
dCombinedMagnitude_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWAngle[k] +
219219
dCombinedMagnitude_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWAngle[k] +
220220
dCombinedMagnitude_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWAngle[k];
221221

222-
dInputAngle_dOutputAngle +=
222+
dInputAngle_dOutputAngle[k] =
223223
dCombinedAngle_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dAngle[k] +
224224
dCombinedAngle_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dAngle[k] +
225225
dCombinedAngle_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY1[k] * dY1_dAngle[k] +
226226
dCombinedAngle_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX1[k] * dX1_dAngle[k];
227227

228-
dInput2Angle_dOutputAngle +=
228+
dInput2Angle_dOutputAngle[k] =
229229
dCombinedAngle_dSumX * dSumX_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWAngle[k] +
230230
dCombinedAngle_dSumY * dSumY_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWAngle[k] +
231231
dCombinedAngle_dSumX * dSumX_dDeltaY[k] * dDeltaY_dY2[k] * dY2_dWAngle[k] +
232232
dCombinedAngle_dSumY * dSumY_dDeltaX[k] * dDeltaX_dX2[k] * dX2_dWAngle[k];
233233

234-
dWeight_dOutputMag +=
234+
dWeight_dOutputMag[k] =
235235
dCombinedMagnitude_dSumX * dSumX_dDeltaX[k] * dDeltaX_dWeight[k] +
236236
dCombinedMagnitude_dSumY * dSumY_dDeltaY[k] * dDeltaY_dWeight[k] +
237237
dCombinedMagnitude_dSumX * dSumX_dDeltaY[k] * dDeltaY_dWeight[k] +
238238
dCombinedMagnitude_dSumY * dSumY_dDeltaX[k] * dDeltaX_dWeight[k] +
239239
dCombinedMagnitude_dSumX * dSumX_dResultMagnitude[k] * dResultMagnitude_dWeight[k] +
240240
dCombinedMagnitude_dSumY * dSumY_dResultMagnitude[k] * dResultMagnitude_dWeight[k];
241241

242-
dWeight_dOutputAngle +=
242+
dWeight_dOutputAngle[k] =
243243
dCombinedAngle_dSumX * dSumX_dDeltaX[k] * dDeltaX_dWeight[k] +
244244
dCombinedAngle_dSumY * dSumY_dDeltaY[k] * dDeltaY_dWeight[k] +
245245
dCombinedAngle_dSumX * dSumX_dDeltaY[k] * dDeltaY_dWeight[k] +
@@ -276,24 +276,27 @@ public override BackwardResult Backward(Matrix dOutput)
276276
Matrix dWeights = new Matrix(weights.Rows, weights.Cols);
277277

278278
// Loop through each element in input1
279-
Parallel.For(0, input1.Rows, i =>
279+
Parallel.For(0, this.input1.Rows, i =>
280280
{
281281
// Loop through each half of the columns in input1 (representing magnitudes and angles)
282-
for (int j = 0; j < input2.Cols / 2; j++)
282+
for (int j = 0; j < this.input2.Cols / 2; j++)
283283
{
284284
var calculatedValues = this.calculatedValues[i, j];
285-
dInput1[i, j] += dOutput[i, j] * calculatedValues.DInputMag_dOutputMag;
286-
dInput1[i, j] += dOutput[i, j + (input2.Cols / 2)] * calculatedValues.DInputMag_dOutputAngle;
287-
dInput1[i, j + (input1.Cols / 2)] += dOutput[i, j] * calculatedValues.DInputAngle_dOutputMag;
288-
dInput1[i, j + (input1.Cols / 2)] += dOutput[i, j + (input2.Cols / 2)] * calculatedValues.DInputAngle_dOutputAngle;
289-
290-
dInput2[i, j] += dOutput[i, j] * calculatedValues.DInput2Mag_dOutputMag;
291-
dInput2[i, j] += dOutput[i, j + (input2.Cols / 2)] * calculatedValues.DInput2Mag_dOutputAngle;
292-
dInput2[i, j + (input2.Cols / 2)] += dOutput[i, j] * calculatedValues.DInput2Angle_dOutputMag;
293-
dInput2[i, j + (input2.Cols / 2)] += dOutput[i, j + (input2.Cols / 2)] * calculatedValues.DInput2Angle_dOutputAngle;
294-
295-
dWeights[i, j] += dOutput[i, j] * calculatedValues.DWeight_dOutputMag;
296-
dWeights[i, j] += dOutput[i, j + (input2.Cols / 2)] * calculatedValues.DWeight_dOutputAngle;
285+
for (int k = 0; k < this.input2.Rows / 2; k++)
286+
{
287+
dInput1[i, k] += dOutput[i, j] * calculatedValues.DInputMag_dOutputMag[k];
288+
dInput1[i, k] += dOutput[i, j + (this.input2.Cols / 2)] * calculatedValues.DInputMag_dOutputAngle[k];
289+
dInput1[i, k + (this.input1.Cols / 2)] += dOutput[i, j] * calculatedValues.DInputAngle_dOutputMag[k];
290+
dInput1[i, k + (this.input1.Cols / 2)] += dOutput[i, j + (this.input2.Cols / 2)] * calculatedValues.DInputAngle_dOutputAngle[k];
291+
292+
dInput2[k, j] += dOutput[i, j] * calculatedValues.DInput2Mag_dOutputMag[k];
293+
dInput2[k, j] += dOutput[i, j + (this.input2.Cols / 2)] * calculatedValues.DInput2Mag_dOutputAngle[k];
294+
dInput2[k, j + (this.input2.Cols / 2)] += dOutput[i, j] * calculatedValues.DInput2Angle_dOutputMag[k];
295+
dInput2[k, j + (this.input2.Cols / 2)] += dOutput[i, j + (this.input2.Cols / 2)] * calculatedValues.DInput2Angle_dOutputAngle[k];
296+
297+
dWeights[k, j] += dOutput[i, j] * calculatedValues.DWeight_dOutputMag[k];
298+
dWeights[k, j] += dOutput[i, j + (this.input2.Cols / 2)] * calculatedValues.DWeight_dOutputAngle[k];
299+
}
297300
}
298301
});
299302

@@ -306,25 +309,25 @@ public override BackwardResult Backward(Matrix dOutput)
306309

307310
private struct CalculatedValues
308311
{
309-
public double DInputMag_dOutputMag { get; internal set; }
312+
public double[] DInputMag_dOutputMag { get; internal set; }
310313

311-
public double DInputMag_dOutputAngle { get; internal set; }
314+
public double[] DInputMag_dOutputAngle { get; internal set; }
312315

313-
public double DInputAngle_dOutputMag { get; internal set; }
316+
public double[] DInputAngle_dOutputMag { get; internal set; }
314317

315-
public double DInputAngle_dOutputAngle { get; internal set; }
318+
public double[] DInputAngle_dOutputAngle { get; internal set; }
316319

317-
public double DInput2Mag_dOutputMag { get; internal set; }
320+
public double[] DInput2Mag_dOutputMag { get; internal set; }
318321

319-
public double DInput2Mag_dOutputAngle { get; internal set; }
322+
public double[] DInput2Mag_dOutputAngle { get; internal set; }
320323

321-
public double DInput2Angle_dOutputMag { get; internal set; }
324+
public double[] DInput2Angle_dOutputMag { get; internal set; }
322325

323-
public double DInput2Angle_dOutputAngle { get; internal set; }
326+
public double[] DInput2Angle_dOutputAngle { get; internal set; }
324327

325-
public double DWeight_dOutputMag { get; internal set; }
328+
public double[] DWeight_dOutputMag { get; internal set; }
326329

327-
public double DWeight_dOutputAngle { get; internal set; }
330+
public double[] DWeight_dOutputAngle { get; internal set; }
328331
}
329332
}
330333
}

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/VectorNetwork/RMAD/ElementwiseVectorConstituentMultiplyOperation.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ public override BackwardResult Backward(Matrix dOutput)
164164
// Nested loop for each element in input2
165165
for (int k = 0; k < input2.Rows / 2; k++)
166166
{
167-
double magnitude = input1[i, j];
168-
double angle = input1[i, j + input1.Cols / 2];
167+
double magnitude = input1[i, k];
168+
double angle = input1[i, k + input1.Cols / 2];
169169

170170
double wMagnitude = input2[k, j];
171171
double wAngle = input2[k, j + input2.Cols / 2];
@@ -206,13 +206,13 @@ public override BackwardResult Backward(Matrix dOutput)
206206
double dY1_dMagnitude = Math.Sin(angle);
207207

208208
// Apply chain rule for dInput1
209-
dInput1[i, j] += dOutput[i, j] * (
209+
dInput1[i, k] += dOutput[i, j] * (
210210
dCombinedMagnitude_dSumX * dSumX_dDeltaX * dDeltaX_dX1 * dX1_dMagnitude +
211211
dCombinedMagnitude_dSumY * dSumY_dDeltaY * dDeltaY_dY1 * dY1_dMagnitude +
212212
dCombinedMagnitude_dSumX * dSumX_dDeltaY * dDeltaY_dY1 * dY1_dMagnitude +
213213
dCombinedMagnitude_dSumY * dSumY_dDeltaX * dDeltaX_dX1 * dX1_dMagnitude);
214214

215-
dInput1[i, j] += dOutput[i, j + (input2.Cols / 2)] * (
215+
dInput1[i, k] += dOutput[i, j + (input2.Cols / 2)] * (
216216
dCombinedAngle_dSumX * dSumX_dDeltaX * dDeltaX_dX1 * dX1_dMagnitude +
217217
dCombinedAngle_dSumY * dSumY_dDeltaY * dDeltaY_dY1 * dY1_dMagnitude +
218218
dCombinedAngle_dSumX * dSumX_dDeltaY * dDeltaY_dY1 * dY1_dMagnitude +
@@ -222,13 +222,13 @@ public override BackwardResult Backward(Matrix dOutput)
222222
double dY1_dAngle = magnitude * Math.Cos(angle);
223223

224224
// Applying the chain rule for the angle component of input1
225-
dInput1[i, j + input1.Cols / 2] += dOutput[i, j] * (
225+
dInput1[i, k + input1.Cols / 2] += dOutput[i, j] * (
226226
dCombinedMagnitude_dSumX * dSumX_dDeltaX * dDeltaX_dX1 * dX1_dAngle +
227227
dCombinedMagnitude_dSumY * dSumY_dDeltaY * dDeltaY_dY1 * dY1_dAngle +
228228
dCombinedMagnitude_dSumX * dSumX_dDeltaY * dDeltaY_dY1 * dY1_dAngle +
229229
dCombinedMagnitude_dSumY * dSumY_dDeltaX * dDeltaX_dX1 * dX1_dAngle);
230230

231-
dInput1[i, j + input1.Cols / 2] += dOutput[i, j + (input2.Cols / 2)] * (
231+
dInput1[i, k + input1.Cols / 2] += dOutput[i, j + (input2.Cols / 2)] * (
232232
dCombinedAngle_dSumX * dSumX_dDeltaX * dDeltaX_dX1 * dX1_dAngle +
233233
dCombinedAngle_dSumY * dSumY_dDeltaY * dDeltaY_dY1 * dY1_dAngle +
234234
dCombinedAngle_dSumX * dSumX_dDeltaY * dDeltaY_dY1 * dY1_dAngle +

0 commit comments

Comments
 (0)