-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.cs
More file actions
129 lines (114 loc) · 4.31 KB
/
Helper.cs
File metadata and controls
129 lines (114 loc) · 4.31 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using MathNet.Numerics.LinearAlgebra;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace NeuralNetworkMyself
{
public static class Helper
{
public static int[] indices = new int[0];
public enum LearningRateAdjType
{
Absolute,
Relative,
Nothing
}
public enum ActivationFunctionType
{
Sigmoid,
BipolarSigmoid,
Identity,
ReLu,
Softmax
}
public enum CostFunctionType
{
Quadratic,
CrossEntropy,
LogLikelihood
}
public enum RegularizationType
{
NoRegularization,
L2Regularization
}
public enum EarlyStoppingType
{
Off,
Simple,
Advanced
}
public enum MessageTrigger
{
NoNetwork,
OutputHeader,
UpdateResults,
TrainingFinished,
DisplayParam
}
// Matrix erzeugen, bei der jede Spalte aus demselben Vektor besteht
public static Matrix<float> BuildMatrixOfColumnVector(Vector<float> vector, int numColumns)
{
//Idee: Inputvektor mal [1,1, ... ,1]-Vektor transponiert (numColumns lang) ergibt die gesuchte Matrix
Vector<float> multiplier = Vector<float>.Build.Dense(numColumns, 1);
return vector.ToColumnMatrix() * multiplier.ToRowMatrix();
}
//In-place Version der Methode
public static void BuildMatrixOfColumnVector(Vector<float> vector, int numColumns, out Matrix<float> result)
{
//Idee: Inputvektor mal [1,1, ... ,1]-Vektor transponiert (numColumns lang) ergibt die gesuchte Matrix
Vector<float> multiplier = Vector<float>.Build.Dense(numColumns, 1);
result = vector.ToColumnMatrix() * multiplier.ToRowMatrix();
}
// Matrix erzeugen, bei der jede Zeile aus demselben Vektor besteht
public static Matrix<float> BuildMatrixOfRowVector(Vector<float> vector, int numRows)
{
Vector<float> multiplier = Vector<float>.Build.Dense(numRows, 1);
return multiplier.ToColumnMatrix() * vector.ToRowMatrix();
}
// Fisher Yates Algorithm zum Mischen der Spalten einer Matrix (in-place-suffling)
public static void ShuffleColumnsOfMatricesUsingFisherYates(Matrix<float>[] matrices)
{
Random rnd = new Random();
for (int i = matrices[0].ColumnCount - 1; i > 0; i--)
{
int swapWithPos = rnd.Next(i + 1);
// Do the same swap in all matrices
for (int j = 0; j < matrices.Length; j++)
{
Vector<float> helper = matrices[j].Column(i);
matrices[j].SetColumn(i, matrices[j].Column(swapWithPos));
matrices[j].SetColumn(swapWithPos, helper);
}
}
}
// Performace-Test - Fisher Yates wie oben, aber wir mischen nur die Indizes und erzeugen daraus eine neue Matrix
public static Matrix<float> ShuffleColumnsOfMatrixUsingFisherYatesNewMatrix(Matrix<float> matrix)
{
if (indices.Length != matrix.ColumnCount)
{
indices = new int[matrix.ColumnCount];
for (int i = 0; i < indices.Length; i++)
indices[i] = i;
}
int[] newOrderedIndices = new int[indices.Length];
indices.CopyTo(newOrderedIndices, 0);
Random rnd = new Random();
for (int i = newOrderedIndices.Length - 1; i > 0; i--)
{
int swapWithPos = rnd.Next(i + 1);
int helper = newOrderedIndices[i];
newOrderedIndices[i] = newOrderedIndices[swapWithPos];
newOrderedIndices[swapWithPos] = helper;
}
List<Vector<float>> listOfNewColumns = new List<Vector<float>>();
for (int i = 0; i < newOrderedIndices.Length; i++)
{
listOfNewColumns.Add(matrix.Column(newOrderedIndices[i]));
}
return Matrix<float>.Build.DenseOfColumnVectors(listOfNewColumns);
}
}
}