У меня есть ИНС, реализованная с нуля на C#. Однако не сходилось, в чем может быть проблема? Я пробовал разные функции активации, другой набор данных. Набор данных генерируется случайным образом, но ИНС в Python работает с тем же набором данных, поэтому можно обучить нейронную сеть данным.
У меня есть ИНС, реализованная с нуля на C#. Однако не сходилось, в чем может быть проблема? Я пробовал разные функции активации, другой набор данных. Набор данных генерируется случайным образом, но ИНС в Python работает с тем же набором данных, поэтому можно обучить нейронную сеть данным. [code] private NeuralNetworkData ReLU(NeuralNetworkData x) { double[,] res = new double[x.Data.GetLength(0), x.Data.GetLength(1)]; for (int i = 0; i < res.Length; i++) { res[i, 0] = MathF.Max(0, (float)x.Data[i,0]); } return new NeuralNetworkData( res); } private NeuralNetworkData Softmax(NeuralNetworkData x) { double[,] res = new double[x.Data.GetLength(0), x.Data.GetLength(1)]; double max = x.ToVector().Max(x => x); double[] exps = x.ToVector().Select(x => Math.Exp(x - max)).ToArray(); double sumExps = exps.Sum();
for (int i = 0; i < res.Length; i++) { res[i, 0] = exps[i] / sumExps; } return new NeuralNetworkData(res); } private NeuralNetworkData ReLUDerivative(NeuralNetworkData x) { double[,] res = new double[x.Data.GetLength(0), x.Data.GetLength(1)]; for (int i = 0; i < res.Length; i++) { res[i, 0] = x.Data[i, 0] > 0 ? 1 : 0; } return new NeuralNetworkData(res); } private void FeedForward(int index, int epoch) { if (epoch is 0 && index is 0) { Weights1 = Weights1.Transpose(); Weights2 = Weights2.Transpose(); } Input = Scale(DataFile.Data[index].Values).ToMatrix(); Z1 = (Weights1 * Input) + Bias1; Hidden = ReLU(Z1); Z2 = (Weights2 * Hidden) + Bias2; Output = Softmax(Z2);
Weights1 = Weights1 - (LearningRate * Hidden_Gradient); Bias1 = Bias1 - (LearningRate * Bias1_Gradient); } private double[] Scale(double[] data) { double[] res = new double[data.Length]; double sum = data.Sum(); for (int i = 0; i < res.Length; i++) { res[i] = data[i] / sum; } return res; } } public static class DoubleExtensions { public static bool Any(this double[,] matrix, Func func) { for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { if (!func.Invoke(matrix[i, j])) { return true; } } } return false; } public static bool All(this double[,] matrix, Func func) { List isTrue = new List(); for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { if (func.Invoke(matrix[i, j])) { isTrue.Add(true); } else { isTrue.Add(false); } } } return isTrue.All(x => x is true); } public static NeuralNetworkData ToMatrix(this double[] vector) { if (vector.Any(x => x != 0)) { double[,] res = new double[vector.Length,1]; for (int i = 0; i < res.GetLength(0); i++) { res[i, 0] = vector[i]; } return new NeuralNetworkData(res); } else { throw new Exception("No input."); } } public static NeuralNetworkData Transpose(this NeuralNetworkData matrix) { if (matrix != null) { double[,] res = new double[matrix.Data.GetLength(1), matrix.Data.GetLength(0)]; for (int i = 0; i < matrix.Data.GetLength(0); i++) { for (int j = 0; j < matrix.Data.GetLength(1); j++) { res[j, i] = matrix.Data[i, j]; } } return new NeuralNetworkData(res); } else { throw new Exception("No input provided!"); } } public static double[] ToVector(this NeuralNetworkData matrix) { if (matrix.Data.GetLength(1) == 1) { double[] res = new double[matrix.Data.GetLength(0)]; for (int i = 0; i < res.Length; i++) { res[i] = matrix.Data[i, 0]; }
return (res); } else { throw new Exception("Matrix too big!"); } } public static double[] Multiply(this double[] first, double second) { double[] res = new double[first.Length]; for (int i = 0; i < res.Length; i++) { res[i] = first[i] * second; } return res; } }
[/code] Вот данные NeuralNetworkData: [code]public class NeuralNetworkData {
private void FillMatrix() { for (int i = 0; i < Data.GetLength(0); i++) { for (int j = 0; j < Data.GetLength(1); j++) { Data[i, j] = rnd.NextDouble() * 0.01; } } } public static NeuralNetworkData operator *(NeuralNetworkData first, NeuralNetworkData second) { if (first.Data.GetLength(1) != second.Data.GetLength(0)) throw new Exception("Matrices cannot be multiplied!");
double[,] res = new double[first.Data.GetLength(0), second.Data.GetLength(1)];
for (int i = 0; i < res.GetLength(0); i++) { for (int j = 0; j < res.GetLength(1); j++) { double sum = 0; for (int k = 0; k < first.Data.GetLength(1); k++) { sum += first.Data[i, k] * second.Data[k, j]; } res[i, j] = sum; } }
return new NeuralNetworkData(res); } public static NeuralNetworkData operator *(double first, NeuralNetworkData second) { double[,] res = new double[second.Data.GetLength(0), second.Data.GetLength(1)]; for (int i = 0; i < second.Data.GetLength(0); i++) { for (int j = 0; j < second.Data.GetLength(1); j++) { res[i, j] = second.Data[i, j] * first; } } return new NeuralNetworkData(res); } public static NeuralNetworkData operator +(NeuralNetworkData first, NeuralNetworkData second) { if (first.Data.GetLength(1) == second.Data.GetLength(1) && first.Data.GetLength(0) == second.Data.GetLength(0)) { double[,] res = new double[first.Data.GetLength(0), second.Data.GetLength(1)]; for (int i = 0; i < res.GetLength(0); i++) { for (int j = 0; j < res.GetLength(1); j++) { res[i, j] = first.Data[i, j] + second.Data[i, j]; } } return new NeuralNetworkData(res); } else { throw new Exception("Not the same length."); } } public static NeuralNetworkData operator -(NeuralNetworkData first, NeuralNetworkData second) { if (first.Data.GetLength(1) == second.Data.GetLength(1) && first.Data.GetLength(0) == second.Data.GetLength(0)) { double[,] res = new double[first.Data.GetLength(0), second.Data.GetLength(1)]; for (int i = 0; i < res.GetLength(0); i++) { for (int j = 0; j < res.GetLength(1); j++) { res[i, j] = first.Data[i, j] - second.Data[i, j]; } } return new NeuralNetworkData(res); } else { throw new Exception("Not the same length."); } } } [/code] Заранее спасибо за все советы. Я пробовал разные скорости обучения, разное количество нейронов в скрытом слое. К сожалению, ничего не помогло.
Я использовал optuna для создания сотен моделей для набора данных. Несколько моделей оказались полезными, я случайно не сохранил гиперпараметры для хороших.
Я сохранил модель в формате .txt. Можно ли как-нибудь извлечь гиперпараметры из уже...
Я работаю с набором данных NSL-KDD, и моя задача — повысить точность алгоритмов классификации с помощью scikit-learn. В частности, я заинтересован в достижении показателя точности более 80%.
Я реализовал различные алгоритмы классификации из...