public double[,] SortRank(double[,] orgRanks) { Matrix <double> matRanks = CreateMatrix.DenseOfArray(orgRanks); double[] projects = matRanks.Column(0).ToArray(); double[] ranks = matRanks.Column(1).ToArray(); Array.Sort(ranks, projects, Comparer <double> .Create((a, b) => { if (a == 0 && b != 0) { return(1); } if (a != 0 && b == 0) { return(-1); } if (a > b) { return(1); } if (a < b) { return(-1); } return(0); })); matRanks.SetColumn(0, CreateVector.DenseOfArray(projects)); matRanks.SetColumn(1, CreateVector.DenseOfArray(ranks)); return(matRanks.ToArray()); }
private double[] To1DAwards(double[,] awards2D) { // [1,2] // [2,2] => [1,1,2,2,3,3] // [3,2] Matrix <double> matAwards2D = CreateMatrix.DenseOfArray(awards2D); int totalAwards = (int)matAwards2D.Column(1).Sum(); double[] awardRanks = new double[totalAwards]; int index = 0; for (int r = 0; r < matAwards2D.RowCount; r++) { double award = awards2D[r, 0]; double number = awards2D[r, 1]; for (int n = 0; n < number; n++) { awardRanks[index] = award; index++; } } return(awardRanks); }
void Construct(bool zeroInit) { // TODO: repair a bug (Maybe, last matrix is null? Or just index disorder?) weights = new Matrix <double> [conf.numHidLayers + 2]; int prev, next; prev = conf.numInputs; for (int i = 0; i < conf.numHidLayers; i++) { next = conf.numHiddens; if (zeroInit) { weights[i] = CreateMatrix.Dense <double>(next, prev); } else { weights[i] = CreateMatrix.Random <double>(next, prev, distrib); } prev = next; } next = conf.numOutputs; if (zeroInit) { weights[conf.numHidLayers + 1] = CreateMatrix.Dense <double>(next, prev); } else { weights[conf.numHidLayers + 1] = CreateMatrix.Random <double>(next, prev, distrib); } }
public Matrix <double> Recognize(Matrix <double> pattern) { Console.WriteLine("Memory:"); Console.Write(Memory.ToMatrixString()); if (pattern.RowCount != Dimension || pattern.ColumnCount != Dimension) { throw new Exception("Incompatible image dimensions"); } //var rtnVector = CreateMatrix.Dense<double>(Dimension, Dimension).AsColumnMajorArray(); // convert 0 => -1 pattern.MapInplace(x => x == 0 ? -1 : x, Zeros.Include); // to row vector var row = pattern.AsColumnMajorArray(); var input = CreateMatrix.DenseOfRowArrays(row); var rtnVector = input.Clone().ToRowMajorArray(); for (int e = 0; e < numOfEpoch; e++) { for (int i = 0; i < Memory.ColumnCount; i++) { var memoryColumn = Memory.Column(i).ToColumnMatrix(); rtnVector[i] = Math.Sign(DotProduct(rtnVector, memoryColumn)); } } var rtnMatrix = CreateMatrix.DenseOfColumnMajor <double>(Dimension, Dimension, rtnVector); rtnMatrix.MapInplace(x => x == 0 ? -1 : x, Zeros.Include); return(rtnMatrix); }
protected override Vector <double> CalculateSearchDirection(ref Matrix <double> inversePseudoHessian, out double maxLineSearchStep, out double startingStepSize, IObjectiveFunction previousPoint, IObjectiveFunction candidate, Vector <double> step) { startingStepSize = 1.0; maxLineSearchStep = double.PositiveInfinity; Vector <double> lineSearchDirection; var y = candidate.Gradient - previousPoint.Gradient; double sy = step * y; inversePseudoHessian = inversePseudoHessian + ((sy + y * inversePseudoHessian * y) / Math.Pow(sy, 2.0)) * step.OuterProduct(step) - ((inversePseudoHessian * y.ToColumnMatrix()) * step.ToRowMatrix() + step.ToColumnMatrix() * (y.ToRowMatrix() * inversePseudoHessian)) * (1.0 / sy); lineSearchDirection = -inversePseudoHessian * candidate.Gradient; if (lineSearchDirection * candidate.Gradient >= 0.0) { lineSearchDirection = -candidate.Gradient; inversePseudoHessian = CreateMatrix.DenseIdentity <double>(candidate.Point.Count); } return(lineSearchDirection); }
private void SaveAsButton_Click(object sender, EventArgs e) { Readjust(); if (TFadjustedZ == null) { MessageBox.Show(this, "Cannot save an adjusted TF while in an error state.", "Error saving", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Matlab MAT (*.mat)|*.mat|All Files (*.*)|*.*"; sfd.FileName = Path.GetFileNameWithoutExtension(TFFilename) + "_adj.mat"; if (sfd.ShowDialog() != DialogResult.OK) { return; } var Z = CreateMatrix.DenseOfColumns(new IEnumerable <double>[] { TFadjustedZ }); //var Sr = CreateMatrix.DenseOfColumnVectors(new Vector<Complex>[] { TFadjustedSr }); var Sr = CreateMatrix.DenseOfColumns(new Vector <Complex>[] { TFadjustedSr }); var matrices = new List <MatlabMatrix>(); matrices.Add(MatlabWriter.Pack(Z, "z")); matrices.Add(MatlabWriter.Pack(Sr, "Sr")); MatlabWriter.Store(sfd.FileName, matrices); }
private void Reshuffle(StateOperator state, SystemBipartition bipartition) { EvaluatedMatrix = new Transformation(CreateMatrix.Dense <Complex>(state.RowDimension, state.ColumnDimension)); int rowOffset = 0, columnOffset = 0; for (int i = 0; i < state.RowDimension; i++) { int reshRowStart = rowOffset * bipartition.DimensionA, reshColumnStart = columnOffset * bipartition.DimensionB; int reshRowIterator = reshRowStart, reshColumnIterator = reshColumnStart; int reshColumnLimit = reshColumnStart + bipartition.DimensionB; for (int j = 0; j < state.RowDimension; j++) { EvaluatedMatrix[reshRowIterator, reshColumnIterator] = state[i, j]; reshColumnIterator++; if (reshColumnIterator == reshColumnLimit) { reshColumnIterator = reshColumnStart; reshRowIterator++; } } columnOffset++; if (columnOffset == bipartition.DimensionA) { columnOffset = 0; rowOffset++; } } }
/// <summary> /// Helper function used to create the masking map from a single image represented as a /// vector. We do this step so we can do ALL convolutions or pools for an image in one single /// computational step. /// </summary> /// <param name="kernelSideLength"> /// The length of one side of the convolution/pool. All filters are assumed square. /// </param> /// <param name="strideSize">The stride length that will be used with this map.</param> /// <param name="startingImage"> /// An images represented as a vector that we are creating the map for. /// </param> /// <returns></returns> internal Matrix <double> CreateMaskingMap(int kernelSideLength, int strideSize, Vector <double> startingImage) { int _imageSideDimension = (int)Math.Sqrt(startingImage.Count); int _endingImageSideDimension = (_imageSideDimension - kernelSideLength) / strideSize + 1; Matrix <double> _result = CreateMatrix.Dense <double>((int)Math.Pow(kernelSideLength, 2), (int)Math.Pow(_endingImageSideDimension, 2)); for (int i = 0; i < _endingImageSideDimension; i += strideSize) { for (int j = 0; j < _endingImageSideDimension; j += strideSize) { int _arrayIndex = i * _imageSideDimension + j; Vector <double> _dataPatch = Vector <double> .Build.Sparse((int)Math.Pow(kernelSideLength, 2)); for (int k = 0; k < kernelSideLength; k++) { for (int m = 0; m < kernelSideLength; m++) { _dataPatch[k * kernelSideLength + m] = startingImage[_arrayIndex + m + kernelSideLength * k]; } } _result.SetColumn(j + i * _endingImageSideDimension, _dataPatch); } } return(_result); }
/// <summary> /// Runs through a single convolution layer. /// </summary> /// <param name="layerInfo">The layer info used for this layer.</param> /// <param name="inputImages"> /// A matrix of all the images that will be convolved. Each row is an image. /// </param> /// <returns>A matrix of all the resulting images. Each row is an image.</returns> internal Matrix <double> Convolve(ConvolutionalLayerInformation layerInfo, Matrix <double> inputImages) { // Construct out return matrix that will include all layers of our images. Matrix <double> _outputImages = null; foreach (Tuple <int, Vector <double> > _imageDimensionAndIndex in inputImages.EnumerateRowsIndexed()) { // Create the matrix so we can do all of the convolutions at once. Matrix <double> _preConvolutionMap = this.CreateMaskingMap(layerInfo.KernelSize, layerInfo.Stride, _imageDimensionAndIndex.Item2); Matrix <double> _filtersForThisDimension = CreateMatrix.Dense <double>(layerInfo.FlattenedFilters.Count, layerInfo.FlattenedFilters[0].ColumnCount); foreach (Matrix <double> _filter in layerInfo.FlattenedFilters) { _filtersForThisDimension.InsertRow(_imageDimensionAndIndex.Item1, _filter.Row(_imageDimensionAndIndex.Item1)); } // Create the result image matrix if it's not created yet if (_outputImages == null) { _outputImages = CreateMatrix.Dense <double>(layerInfo.FilterCount, _preConvolutionMap.ColumnCount, 0.0); } // Store off the result of our filters multiplied by our map. This ends up being every filter passing over // the entire image, and returning a dimentions for each kernel in the layer. We sum all the dimensional results in one dimension. _outputImages = _outputImages.Add(_filtersForThisDimension.Multiply(_preConvolutionMap)); } // Return all the resulting dimensions of the images after convolution return(_outputImages); }
/// <summary> /// Runs through the network and makes a prediction. /// </summary> /// <param name="input">The input image to run the prediction on.</param> /// <returns>The values from the final layer. The largest value is the networks prediction.</returns> public double[] FeedForward(double[] input) { Matrix <double> _currentImages = CreateMatrix.DenseOfRowVectors(CreateVector.DenseOfArray <double>(input)); foreach (ILayerInformation _layerInformation in this.LayerInformation) { switch (_layerInformation.LayerType) { case (LayerType.Convolutional): ConvolutionalLayerInformation _convInfo = _layerInformation as ConvolutionalLayerInformation; _currentImages = this.Convolve(_convInfo, _currentImages); break; case (LayerType.Pooling): PoolingLayerInformation _poolInfo = _layerInformation as PoolingLayerInformation; _currentImages = this.Pool(_poolInfo, _currentImages); break; case (LayerType.NonLinear): NonLinearLayerInformation _nonLinearInfo = _layerInformation as NonLinearLayerInformation; _currentImages = this.NonLinear(_nonLinearInfo, _currentImages); break; } } double[] _fullyConnectedInput = CreateVector.DenseOfEnumerable <double>(_currentImages.EnumerateRows().SelectMany(a => a)).ToArray(); return(this.FullyConnectedNetwork.FeedForward(_currentImages.Enumerate().ToArray())); }
public static double[][] GramSchmidt(double[][] vectors) { Matrix <double> matrix = CreateMatrix.DenseOfColumnArrays(vectors); var result = matrix.GramSchmidt(); return(result.Q.ToColumnArrays()); }
private void GetSparseKernel() { int sampleRate = 1; if (InputArgs.ContainsKey("SAMPLE_RATE")) { if (InputArgs["SAMPLE_RATE"].GetType().Name == "Int32") { sampleRate = (int)InputArgs["SAMPLE_RATE"]; } } float threshold = 0.000054f; int numOctaves = int.Parse(Settings["OCTAVES"][0]); int binsPerOctave = int.Parse(Settings["BINS_PER_OCTAVE"][0]); double minFreq = double.Parse(Settings["MIN_FREQ"][0]); int numBins = numOctaves * binsPerOctave; double Q = 1 / (Math.Pow(2, 1 / (double)binsPerOctave) - 1); int fftLen = 1; while (fftLen < Q * sampleRate / minFreq) { fftLen *= 2; } NAudio.Dsp.Complex[] tempKernel = new NAudio.Dsp.Complex[fftLen]; List <Complex[]> sparKernel = new List <Complex[]>(); for (int k = 0; k < numBins; k++) { int N = (int)Math.Ceiling(Q * sampleRate / (minFreq * Math.Pow(2, k / (double)binsPerOctave))); for (int n = 0; n < N; n++) { Complex temp = NAudio.Dsp.FastFourierTransform.HammingWindow(n, N) / (N * (1 + (double.Parse(Settings["N_WEIGHTING"][0]) * N))) * Complex.Exp(2 * Math.PI * Complex.ImaginaryOne * n * (Q / N)) * (1000 * (1 + (double.Parse(Settings["N_WEIGHTING"][0]) * 1000))); tempKernel[n].X = (float)temp.Real; tempKernel[n].Y = (float)temp.Imaginary; } NAudio.Dsp.FastFourierTransform.FFT(true, (int)Math.Log(fftLen, 2.0), tempKernel); Complex[] compKernel = new Complex[tempKernel.Length]; for (int i = 0; i < tempKernel.Length; i++) { if (tempKernel[i].X < threshold && tempKernel[i].Y < threshold) { compKernel[i] = new Complex(0, 0); } else { compKernel[i] = new Complex(tempKernel[i].X, tempKernel[i].Y); } } sparKernel.Add(compKernel); } Matrix <Complex> kernelMat = CreateMatrix.SparseOfRowArrays(sparKernel.ToArray()); kernelMat.Multiply(1000); kernel = kernelMat.ConjugateTranspose(); }
public Neural() { nodes = new Vector <float> [nodeSetup.Length]; weights = new Matrix <float> [nodeSetup.Length - 1]; biases = new Vector <float> [nodeSetup.Length - 1]; gammaValues = new Vector <float> [nodeSetup.Length - 1]; deltaWeights = new Matrix <float> [nodeSetup.Length - 1]; deltaBiases = new Vector <float> [nodeSetup.Length - 1]; for (int i = 0; i < nodeSetup.Length; i++) { nodes[i] = CreateVector.Dense <float>(nodeSetup[i]); } for (int i = 0; i < nodeSetup.Length - 1; i++) { weights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]); deltaWeights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]); biases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); deltaBiases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); gammaValues[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); } InitialiseWeights(); InitialiseBiases(); }
/// <summary> /// 求矩阵的逆 /// </summary> /// <returns></returns> public MatrixIntGF26 Inverse() { if (detInverse == -1) { throw new Exception("希尔密码的加密矩阵无法求出其在有限域内的逆矩阵"); } // A的逆 = A的伴随式 / A的行列式 // = A的伴随式 × A的行列式的逆元 var matrix = CreateMatrix.DenseOfArray(elements); var inverse = matrix.Inverse(); // 逆矩阵 var adjoint = inverse * determinant; // 伴随矩阵 // 将矩阵化为整数并求模 var adjointInt = ToIntMatrix(adjoint); // 再求矩阵的有限域下的逆 var result = new int[row, column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { result[i, j] = Mod(adjointInt[i, j] * detInverse); } } return(new MatrixIntGF26(result)); }
//Assumes full observability and no D matrix. //Generates an empty set of matrices representing the state-space model. public LinearStateSpaceModel(int numberOfStates = 6, int numberOfInputs = 2) { A = CreateMatrix.Dense <double>(numberOfStates, numberOfStates); B = CreateMatrix.Dense <double>(numberOfStates, numberOfInputs); C = CreateMatrix.DenseIdentity <double>(numberOfStates); D = CreateMatrix.Dense <double>(0, 0); }
public static Matrix <float> SVDPseudoInverse(this Matrix <float> mat) { if (mat.RowCount < mat.ColumnCount) { var svd = mat.Svd(); var u = svd.U; var dVec = svd.S; var dStar = CreateMatrix.Diagonal(mat.RowCount, mat.RowCount, (x) => dVec[x] < 10 ? 0f : 1f / dVec[x]); var vt = svd.VT; while (vt.RowCount > mat.RowCount) { vt = vt.RemoveRow(mat.RowCount); } // V D* UT return(vt.Transpose() * dStar * u.Transpose()); } else { throw new NotImplementedException(); } }
public int Query(float[] inputArray) { Matrix <float> inputs = CreateMatrix.DenseOfColumnMajor(784, 1, inputArray); var hidden_inputs = wih * inputs; var hidden_outputs = Activate(hidden_inputs); var final_inputs = who * hidden_outputs; var final_outputs = Activate(final_inputs); float[] fArray = final_outputs.ToRowMajorArray(); int maxInd = -1; float max = 0; for (int i = 0; i < fArray.Length; i++) { if (fArray[i] > max) { max = fArray[i]; maxInd = i; } } Printer.WriteLine(string.Format("Outputs: {0}, Answer: {1}", ArrayToChar(final_outputs.ToColumnMajorArray()), maxInd)); return(maxInd); }
public void Train(IList <double> datas, IList <double> expectedValues) { var resultMatrix = expectedValues.ToMatrix(); var inputMatrix = datas.ToMatrix(); var resultM = CreateMatrix.DenseOfRows(resultMatrix); var inputM = CreateMatrix.DenseOfRows(inputMatrix); var guessM = Predict(inputMatrix); //Hidden-Out var errM = resultM - guessM; var deltaWho = errM.GetDeltaError(guessM, LEARNING_RATE) * ActivationHidenSigmoid.Transpose(); var deltaBho = errM.GetDeltaError(guessM, LEARNING_RATE); //In-Hidden var errHiddenM = WeightHO.Transpose() * errM; var deltaWih = errHiddenM.GetDeltaError(ActivationHidenSigmoid, LEARNING_RATE) * inputM.Transpose(); var deltaBih = errHiddenM.GetDeltaError(ActivationHidenSigmoid, LEARNING_RATE); WeightHO = WeightHO + deltaWho; WeightIH = WeightIH + deltaWih; BiaisHO = BiaisHO + deltaBho; BiaisIH = BiaisIH + deltaBih; }
public void ParseDirectory(String path) { FileList = Directory.EnumerateFiles(path).Select(x => Path.GetFileName(x)).ToList(); IEnumerable <char> distinctSymbols = FileList. Aggregate <String>((x, y) => x + y). ToCharArray(). Distinct(). Where(x => !Char.IsLetter(x)). OrderByDescending(x => x); List <string[]> splitNames = FileList.Select(x => x.Split(distinctSymbols.ToArray <char>(), StringSplitOptions.RemoveEmptyEntries)).ToList(); IEnumerable <IEnumerable <Tuple <string, string> > > splitSingleNames = splitNames.Select(x => x.Select(a => Tuple.Create("", a.ToLower()))); IEnumerable <IEnumerable <Tuple <string, string> > > splitPairNames = splitNames.Select(x => x.Zip(x.Skip(1), (a, b) => Tuple.Create(a.ToLower(), b.ToLower()))); IEnumerable <IEnumerable <Tuple <string, string> > > union = splitPairNames;//splitSingleNames.Zip(splitPairNames, (a, b) => a.Concat(b)); HashSet <Tuple <string, string> > vocabSet = new HashSet <Tuple <string, string> >(); foreach (IEnumerable <Tuple <string, string> > tokens in union) { foreach (Tuple <string, string> token in tokens) { vocabSet.Add(token); } } Vocab = vocabSet.ToList(); ParsedNames = union.Select(x => x.Select(y => Vocab.FindIndex(z => z.Item1.ToLower() == y.Item1.ToLower() && z.Item2.ToLower() == y.Item2.ToLower())).ToList()).ToList(); IEnumerable <double> zeros = Enumerable.Repeat(0.0, Vocab.Count); double[] v = ParsedNames .Select(x => zeros.Select((a, i) => x.Contains(i) ? /*IndexLookup.IsNumeric(Vocab[i].Item2)?5.0:*/ 15.0 : 0.0)) .Aggregate((x, y) => x.Concat(y)) .ToArray(); Input = CreateMatrix.Dense(Vocab.Count, ParsedNames.Count, v).Transpose().NormalizeRows(2); }
public void Train(Matrix <double> pattern) { if (pattern.RowCount != Dimension || pattern.ColumnCount != Dimension) { throw new Exception("Incompatible image dimensions"); } if (PatternCount >= MaxPatternCount) { throw new Exception("Full Memory"); } var tempMatrix = CreateMatrix.Dense <double>(NeuronsCount, NeuronsCount); // convert 0 => -1 pattern.MapInplace(x => x == 0 ? -1 : x, Zeros.Include); // to column vector var col = pattern.AsColumnMajorArray(); var columnVector = CreateMatrix.DenseOfColumnArrays(col); // create weighted matrix columnVector.TransposeAndMultiply(columnVector, tempMatrix); //set diagonal to 0 tempMatrix = tempMatrix.Subtract(CreateMatrix.DenseIdentity <double>(NeuronsCount, NeuronsCount)); // add pattern Memory = Memory + tempMatrix; PatternCount++; }
/// <summary> /// Objective model with a user supplied jacobian for non-linear least squares regression. /// </summary> public static IObjectiveModel NonlinearModel(Func <Vector <double>, double, double> function, Func <Vector <double>, double, Vector <double> > derivatives, Vector <double> observedX, Vector <double> observedY, Vector <double> weight = null) { Vector <double> func(Vector <double> point, Vector <double> x) { var functionValues = CreateVector.Dense <double>(x.Count); for (int i = 0; i < x.Count; i++) { functionValues[i] = function(point, x[i]); } return(functionValues); } Matrix <double> prime(Vector <double> point, Vector <double> x) { var derivativeValues = CreateMatrix.Dense <double>(x.Count, point.Count); for (int i = 0; i < x.Count; i++) { derivativeValues.SetRow(i, derivatives(point, x[i])); } return(derivativeValues); } var objective = new NonlinearObjectiveFunction(func, prime); objective.SetObserved(observedX, observedY, weight); return(objective); }
//Called after gathering training data public Vector <double>[] Solve(double[,] X, double[,] yArray) { //Solve matrix to generate weights for runtime calibration double[] y = new double[yArray.GetLength(0)]; Vector <double>[] theta = new Vector <double> [yArray.GetLength(1)]; //Process each collum one by one for (int i = 0; i < yArray.GetLength(1); i++) { //Clone raw data to manipulate double[,] Xclone = X.Clone() as double[, ]; //Disable channels not used for this solve Xclone = DisableChannels(Xclone, ActiveSensors, i); Matrix <double> XMatrix = CreateMatrix.DenseOfArray(Xclone); RemoveChannels temp = RemoveEmptyChannels(XMatrix); XMatrix = temp.ReducedMatrix; //Get a solution vector y = getCollumn(yArray, i); Matrix <double> tempMat = XMatrix; //Perform Regression fit Vector <double> yVector = CreateVector.DenseOfArray(y); theta[i] = MultipleRegression.Svd <double>(tempMat, yVector); //Add removed collumns theta[i] = PadArray(temp.usedCol, theta[i]); } return(theta); }
public void GetGlobalKeepMatrix() { int NumberOfActiveDOF = 0; int NumberOfAvailableDOF = 0; int RowIndex; foreach (Element i in Elements) { NumberOfActiveDOF += Convert.ToInt32(GetElementStateExistanceVector(i.ElementId).Sum()) / 2; NumberOfAvailableDOF += 6; } Matrix <double> GlobalKeepMatrix = CreateMatrix.Dense <double>(NumberOfActiveDOF * 3, NumberOfAvailableDOF * 3); foreach (Element i in Elements) { Vector <double> DOFExist = GetElementStateExistanceVector(i.ElementId); RowIndex = 0; for (int index = 0; index < 6; index++) { if (DOFExist[index] == 1) { GlobalKeepMatrix.InsertAtIndex(CreateMatrix.DenseIdentity <double>(3), index * 3, RowIndex); RowIndex += 3; } } } this.KeepMatrix = GlobalKeepMatrix; }
public Neural(List <float> flatValues) { nodes = new Vector <float> [nodeSetup.Length]; weights = new Matrix <float> [nodeSetup.Length - 1]; biases = new Vector <float> [nodeSetup.Length - 1]; gammaValues = new Vector <float> [nodeSetup.Length - 1]; deltaWeights = new Matrix <float> [nodeSetup.Length - 1]; deltaBiases = new Vector <float> [nodeSetup.Length - 1]; for (int i = 0; i < nodeSetup.Length; i++) { nodes[i] = CreateVector.Dense <float>(nodeSetup[i]); } for (int i = 0; i < nodeSetup.Length - 1; i++) { weights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]); deltaWeights[i] = CreateMatrix.Dense <float>(nodeSetup[i + 1], nodeSetup[i]); biases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); deltaBiases[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); gammaValues[i] = CreateVector.Dense <float>(nodeSetup[i + 1]); } LoadFromAllValues(flatValues); }
public void TrainingSet(double[,] x, int[] y) { if (x.GetUpperBound(0) + 1 < y.Length) { throw new Exception("You have more labels than training examples!"); } if (x.GetUpperBound(0) + 1 > y.Length) { throw new Exception("You have more training examples than labels!"); } int cv_size = (int)(0.2 * (x.GetUpperBound(0) + 1)); double[] _y = new double[y.Length - 2 * cv_size]; y = (int[])y.Clone(); X = CreateMatrix.DenseOfArray(x); Random r = new Random(); for (int i = 0; i < X.RowCount - 2; i++) { int j = r.Next(i, X.RowCount); var a = X.Row(i); var b = X.Row(j); X.SetRow(j, a); X.SetRow(i, b); int t = y[i]; y[i] = y[j]; y[j] = t; } CV = CreateMatrix.DenseOfRowVectors(X.Row(0)); X = X.RemoveRow(0); Test = CreateMatrix.DenseOfRowVectors(X.Row(0)); X = X.RemoveRow(0); for (int i = 0; i < cv_size - 1; i++) { CV = CV.InsertRow(i + 1, X.Row(0)); X = X.RemoveRow(0); Test = Test.InsertRow(i + 1, X.Row(0)); X = X.RemoveRow(0); } double[] _cvy = new double[cv_size]; double[] _testy = new double[cv_size]; for (int i = 0; i < 2 * cv_size; i++) { _cvy[i / 2] = y[i++]; _testy[i / 2] = y[i]; } for (int i = 2 * cv_size; i < y.Length; i++) { _y[i - 2 * cv_size] = y[i]; } Y = CreateVector.DenseOfArray(_y); CVY = CreateVector.DenseOfArray(_cvy); TestY = CreateVector.DenseOfArray(_testy); }
private List <Vector <double> > Normalize(List <Vector <double> > values) { var columns = CreateMatrix.DenseOfRowVectors(values).EnumerateColumns(); var mean = CreateVector.DenseOfEnumerable(columns.Select(Statistics.Mean)); var std = CreateVector.DenseOfEnumerable(columns.Select(Statistics.StandardDeviation)); return(values.Select(v => (v - mean) / std).ToList()); }
/// <summary> /// Calculates the local mass matrix /// </summary> /// <returns>The local mass matrix</returns> private Matrix <double> GetLocalMassMatrix() { Matrix <double> LocalMassMatrix = CreateMatrix.Dense <double>(6, 6); LocalMassMatrix.InsertAtIndex(CreateMatrix.DenseIdentity <double>(3) * this.Mass, 0); LocalMassMatrix.InsertAtIndex(this.Inertia, 3); return(LocalMassMatrix); }
//Also assumes no D matrix. public LinearStateSpaceModel(double[,] AMatrix, double[,] BMatrix, double[,] CMatrix) { A = CreateMatrix.DenseOfArray <double>(AMatrix); B = CreateMatrix.DenseOfArray <double>(BMatrix); C = CreateMatrix.DenseOfArray <double>(CMatrix); D = CreateMatrix.Dense <double>(0, 0); }
public NeuralNetwork(NeuralNetworkParameters parameters, IContinuousDistribution rng) { NetworkParameters = parameters; HiddenWeights = CreateMatrix.Random <float>(NetworkParameters.HiddenLayerNeuronCount, NetworkParameters.InputCount, rng); HiddenBiases = CreateVector.Random <float>(NetworkParameters.HiddenLayerNeuronCount, rng); OutputWeights = CreateMatrix.Random <float>(NetworkParameters.OutputCount, NetworkParameters.HiddenLayerNeuronCount, rng); OutputBiases = CreateVector.Random <float>(NetworkParameters.OutputCount, rng); }
public Network(int dimension, int numOfEpoch = 100) { this.numOfEpoch = numOfEpoch; Dimension = dimension; NeuronsCount = Dimension * Dimension; MaxPatternCount = (int)(NeuronsCount / 2 * Math.Log(NeuronsCount)); Memory = CreateMatrix.Dense <double>(NeuronsCount, NeuronsCount); }