Skip to content

Commit f555681

Browse files
committed
Update glyph net.
1 parent b3e0a6a commit f555681

File tree

5 files changed

+103
-74
lines changed

5 files changed

+103
-74
lines changed

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/GlyphNet.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void AdjustLearningRate(double learningRate)
8080
/// <returns>The task.</returns>
8181
public async Task Initialize()
8282
{
83-
var initialAdamIteration = 509;
83+
var initialAdamIteration = 138;
8484
var model = new GlyphNetwork.GlyphNetwork(this.numLayers, this.numNodes, this.numFeatures, this.learningRate, this.clipValue, "glyphnet");
8585
model.Parameters.AdamIteration = initialAdamIteration;
8686
this.GlyphNetwork = model;
@@ -121,7 +121,7 @@ public void SaveWeights()
121121
/// </summary>
122122
public void ApplyWeights()
123123
{
124-
var guid = "glyph_be1b3edd-d423-4714-ae20-3552bdc5cc9f_509";
124+
var guid = "glyph_c83acea3-57af-4909-adab-0c75768549de_138";
125125
var dir = $"E:\\vnnstore\\{guid}";
126126
for (int i = 0; i < this.modelLayers.Count; ++i)
127127
{
@@ -160,16 +160,35 @@ public void ApplyGradients()
160160
var targetedSum0 = gatNet.TargetedSum0;
161161
var targetedSum1 = gatNet.TargetedSum1;
162162

163-
SquaredArclengthEuclideanLossOperation arclengthLoss0 = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
164-
var loss0 = arclengthLoss0.Forward(targetedSum0, (3 * Math.PI) / 4d);
163+
int maxMag0 = 0;
164+
int maxMag1 = 0;
165+
for (int i = 0; i < 15; i++)
166+
{
167+
for (int j = 0; j < 15; j++)
168+
{
169+
if (rotationTargets[i, j] == 1)
170+
{
171+
maxMag1++;
172+
}
173+
else
174+
{
175+
maxMag0++;
176+
}
177+
}
178+
}
179+
180+
Console.WriteLine($"Max Mag 0: {maxMag0}, Max Mag 1: {maxMag1}");
181+
182+
SquaredArclengthEuclideanMagnitudeLossOperation arclengthLoss0 = SquaredArclengthEuclideanMagnitudeLossOperation.Instantiate(gatNet);
183+
var loss0 = arclengthLoss0.Forward(targetedSum0, (3 * Math.PI) / 4d, maxMag0);
165184
var gradient0 = arclengthLoss0.Backward();
166185

167-
SquaredArclengthEuclideanLossOperation arclengthLoss1 = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
168-
var loss1 = arclengthLoss1.Forward(targetedSum1, (1 * Math.PI) / 4d);
186+
SquaredArclengthEuclideanMagnitudeLossOperation arclengthLoss1 = SquaredArclengthEuclideanMagnitudeLossOperation.Instantiate(gatNet);
187+
var loss1 = arclengthLoss1.Forward(targetedSum1, (1 * Math.PI) / 4d, maxMag1);
169188
var gradient1 = arclengthLoss1.Backward();
170189

171-
SquaredArclengthEuclideanLossOperation arclengthLoss = SquaredArclengthEuclideanLossOperation.Instantiate(gatNet);
172-
var loss = arclengthLoss.Forward(output, targetAngle);
190+
SquaredArclengthEuclideanMagnitudeLossOperation arclengthLoss = SquaredArclengthEuclideanMagnitudeLossOperation.Instantiate(gatNet);
191+
var loss = arclengthLoss.Forward(output, targetAngle, 225);
173192
var gradient = arclengthLoss.Backward();
174193

175194
return (gradient, gradient0, gradient1, output, targetedSum0, targetedSum1, glyph, loss, loss0, loss1);

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/GlyphNetTrainer.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public async Task Train()
1414
CudaBlas.Instance.Initialize();
1515
GlyphNet net = new GlyphNet(512, 6144, 3, 0.01d, 4d);
1616
await net.Initialize();
17-
//net.ApplyWeights();
17+
net.ApplyWeights();
1818

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

21-
Random random = new Random(7);
21+
Random random = new Random(11);
2222
var files = pngFiles.OrderBy(x => random.Next()).ToArray();
2323
uint i = 0;
2424
await files.WithRepeatAsync(async (pngFile, token) =>
@@ -93,14 +93,16 @@ await files.WithRepeatAsync(async (pngFile, token) =>
9393

9494

9595
Console.WriteLine($"Iteration {i} Output X: {output[0, 0]}, Output Y: {output[0, 1]}, Grad: {gradient[0, 0]}, {gradient[0, 1]}");
96-
Console.WriteLine($"O1 X: {o1[0, 0]}, O1 Y: {o1[0, 1]}, Loss: {loss[0, 0]}, {loss0[0, 0]}, {loss1[0, 0]}");
96+
Console.WriteLine($"Loss: {loss[0, 0]}");
97+
Console.WriteLine($"O0 X: {o0[0, 0]}, O0 Y: {o0[0, 1]}, Loss 0: {loss0[0, 0]}");
98+
Console.WriteLine($"O1 X: {o1[0, 0]}, O1 Y: {o1[0, 1]}, Loss 1: {loss1[0, 0]}");
9799
await net.Backward(gradient, gradient0, gradient1);
98100
net.ApplyGradients();
99101
//}
100102

101103
await net.Reset();
102104
Thread.Sleep(1000);
103-
if (i % 11 == 2)
105+
if (i % 11 == 5)
104106
{
105107
net.SaveWeights();
106108
}

examples/gravnet/ParallelReverseAutoDiff.GravNetExample/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ internal class Program
44
{
55
static void Main(string[] args)
66
{
7-
TiledNetTrainer trainer = new TiledNetTrainer();
7+
GlyphNetTrainer trainer = new GlyphNetTrainer();
88
trainer.Train().Wait();
99
}
1010
}

0 commit comments

Comments
 (0)