-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNNCloneForSerialization.cs
More file actions
106 lines (99 loc) · 5.09 KB
/
NNCloneForSerialization.cs
File metadata and controls
106 lines (99 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using static NeuralNetworkMyself.Helper;
namespace NeuralNetworkMyself
{
// Klon eines Neural-Network-Objekts, um die relevanten Eigenschaften zu serialisieren, inkl. der Matrizen
public class NNCloneForSerialization
{
public int NumInputNodes { get; set; }
public int NumOutputNodes { get; set; }
public int NumComputingLayers { get; set; }
public int[] NumNodesPerComputingLayer { get; set; }
public int SizeOfMiniBatch { get; set; }
public float LearningRate { get; set; }
public float Momentum { get; set; }
public string[] ActivationFunctionNames { get; set; }
public string CostFunctionName { get; set; }
public string RegularizationName { get; set; }
public float Lambda { get; set; }
public bool IsActiveShuffling { get; set; }
public EarlyStoppingType EarlyStoppingType { get; set; }
public float LearningRateAdjustmentFactor { get; set; }
public float NumMaxLearningRateAdjustments { get; set; }
public DateTime AsOf { get; set; }
public int BestSoFarEpoch { get; set; }
public float BestSoFarAccuracyValidation { get; set; }
public float[][][] Weights { get; set; }
public float[][][] Velocities { get; set; }
public float[][] Biases { get; set; }
public float[][][] BestSoFarWeights { get; set; }
public float[][][] BestSoFarVelocities { get; set; }
public float[][] BestSoFarBiases { get; set; }
public List<float> CostPerEpochTraining { get; set; }
public List<float> CostPerEpochTesting { get; set; }
public List<float> AccuracyPerEpochTraining { get; set; }
public List<float> AccuracyPerEpochTesting { get; set; }
public List<float> AccuracyPerEpochValidation { get; set; }
//Constructor für Serialisierung (wird aufgerufen aus bestehender NeuralNetwork-Klasse, wenn NN gespeichert werden soll)
public NNCloneForSerialization(NeuralNetwork network)
{
NumInputNodes = network.NumInputNodes;
NumOutputNodes = network.NumOutputNodes;
NumComputingLayers = network.NumComputingLayers;
NumNodesPerComputingLayer = network.NumNodesPerComputingLayer;
BestSoFarEpoch = network.BestSoFarEpoch;
CostFunctionName = network.CostFunction is null ? "" : network.CostFunction.Name;
ActivationFunctionNames = new string[NumComputingLayers];
for (int i = 0; i < NumComputingLayers; i++)
ActivationFunctionNames[i] = network.ActivationFunctions[i] is null ? "" : network.ActivationFunctions[i].Name;
RegularizationName = network.Regularization is null ? "" : network.Regularization.Name;
Lambda = network.Regularization is null ? 0 : network.Regularization.Lambda;
IsActiveShuffling = network.IsActiveShuffling;
SizeOfMiniBatch = network.SizeOfMiniBatch;
LearningRate = network.LearningRate;
Momentum = network.Momentum;
CostPerEpochTraining = network.CostPerEpochTraining;
CostPerEpochTesting = network.CostPerEpochTesting;
AccuracyPerEpochTraining = network.AccuracyPerEpochTraining;
AccuracyPerEpochTesting = network.AccuracyPerEpochTesting;
AccuracyPerEpochValidation = network.AccuracyPerEpochValidation;
BestSoFarAccuracyValidation = network.BestSoFarAccuracyValidation;
EarlyStoppingType = network.EarlyStoppingType;
LearningRateAdjustmentFactor = network.LearningRateAdjustmentFactor;
NumMaxLearningRateAdjustments = network.NumMaxLearningRateAdjustments;
AsOf = DateTime.Now;
// Weights & Biases & Velocities
Biases = new float[NumComputingLayers][];
Weights = new float[NumComputingLayers][][];
Velocities = new float[NumComputingLayers][][];
BestSoFarBiases = new float[NumComputingLayers][];
BestSoFarWeights = new float[NumComputingLayers][][];
BestSoFarVelocities = new float[NumComputingLayers][][];
for (int i = 0; i < NumComputingLayers; i++)
{
Biases[i] = network.Biases[i].ToArray();
Weights[i] = network.Weights[i].ToColumnArrays();
Velocities[i] = network.Velocities[i].ToColumnArrays();
BestSoFarBiases[i] = network.BestSoFarBiases[i].ToArray();
BestSoFarWeights[i] = network.BestSoFarWeights[i].ToColumnArrays();
BestSoFarVelocities[i] = network.BestSoFarVelocities[i].ToColumnArrays();
}
}
// Constructor for JSON Deserialization
public NNCloneForSerialization() {}
public void Serialize(string fileName)
{
AsOf = DateTime.Now;
if (!(fileName is null))
{
File.WriteAllText(fileName, JsonSerializer.Serialize(this));
return;
}
else
return;
}
}
}