public override Dictionary <int, Vector <double> > Assemble() { Dictionary <int, Vector <double> > globalLoadVectors = new Dictionary <int, Vector <double> >(); foreach (var loadCase in this.dbctx.LoadCases) { int vectorDimension = this.dbctx.Nodes.Count * 6; Vector <double> globalVector = SparseVector.Create(vectorDimension, 0.0); foreach (var node in this.dbctx.Nodes) { foreach (var load in node.Loads) { if (load.LoadCase.Number == loadCase.Number) { Vector <double> generatedLoad = load.GenerateLoad(); globalVector[6 * node.Number + 0] += generatedLoad[0]; globalVector[6 * node.Number + 1] += generatedLoad[1]; globalVector[6 * node.Number + 2] += generatedLoad[2]; globalVector[6 * node.Number + 3] += generatedLoad[3]; globalVector[6 * node.Number + 4] += generatedLoad[4]; globalVector[6 * node.Number + 5] += generatedLoad[5]; } } } globalLoadVectors.Add(loadCase.Number, globalVector); } return(globalLoadVectors); }
} //Получение J для оценки правильности обучения private List <SparseMatrix> getDerivatives(SparseMatrix Y, SparseMatrix X, List <List <SparseVector> > activations) { List <Vector <double> > delta = new List <Vector <double> >(); List <SparseMatrix> D = new List <SparseMatrix>(); for (int i = 0; i < _layers.Count; i++) { D.Add(SparseMatrix.Create(_layers[i].Weights.RowCount, _layers[i].Weights.ColumnCount, 0)); } for (int k = 0; k < m; k++) { delta = new List <Vector <double> >(); for (int i = 0; i < _layers.Count; i++) { delta.Add(new SparseVector(_layers[i].Activations.Count)); } for (int i = delta.Count - 1; i >= 0; i--) { if (i == delta.Count - 1) { for (int j = 0; j < activations[k][i].Count; j++) { delta[delta.Count - 1][j] = (activations[k][i][j] - Y.Row(k)[j]); } } else { delta[i] = _layers[i + 1].Weights.Transpose().Multiply((delta[i + 1])); delta[i] = SparseVector.OfVector(multiplayOnSigmoidGradient(delta[i], activations[k][i])); D[i + 1] = D[i + 1] + (SparseMatrix)(delta[i + 1].OuterProduct(activations[k][i])); } if (i == 0) { Vector <double> tempVector = SparseVector.Create(X.Row(k).Count, 1); if (tempVector.Count != D[i].ColumnCount) { SparseVector tmp = SparseVector.Create(D[i].ColumnCount, 1); for (int j = 1; j < tmp.Count; j++) { tmp[j] = X.Row(k)[j - 1]; } tempVector = tmp; } Matrix <double> tempMatrix = delta[i].OuterProduct(tempVector); D[i] = D[i] + (SparseMatrix)(tempMatrix); } } } List <SparseMatrix> derivatives = new List <SparseMatrix>(); for (int i = 0; i < D.Count; i++) { derivatives.Add(D[i] * (1f / m)); } return(derivatives); } //Получение частных производных
public static void Main(String[] args) { Stopwatch st = new Stopwatch(); st.Start(); // generate tensor int n = 100; int m = 100; int k = 100; int elems = 10000; Tensor a = new Tensor(n, m, k, elems, 1000); Console.WriteLine("Tensor generated"); Tensor aOld = a.Clone(); Console.WriteLine("Cloned"); // slice tensor by one dimension and concat received matricies consequently. for (int i = 1; i <= f; i++) { a.calcSlices(); Console.WriteLine("Slices calculated"); Solver.solve( a, a.sliceN, // n x (m*k) a.sliceM, // m x (n*k) a.sliceK, // k x (n*m) steps, SparseVector.Create(a.n, 1), SparseVector.Create(a.m, 1), SparseVector.Create(a.k, 1), i); } double av = 0; for (int i = 0; i < aOld.k; ++i) { var els = aOld.a[i].EnumerateIndexed(Zeros.AllowSkip); foreach (var elem in els) { av += Math.Abs(elem.Item3 - Solver.getAns(elem.Item1, elem.Item2, i)); } } Console.WriteLine("|| Diff || = " + (av / (a.n * a.m * a.k))); st.Stop(); Console.WriteLine(st.ElapsedMilliseconds / 1000.0); }
public override Dictionary <int, Vector <double> > Assemble() { Dictionary <int, Vector <double> > globalLoadVectors = new Dictionary <int, Vector <double> >(); foreach (var loadCase in this.dbctx.LoadCases) { int vectorDimension = this.dbctx.Nodes.Count * 6; Vector <double> globalVector = SparseVector.Create(vectorDimension, 0.0); foreach (var frameElement in this.dbctx.FrameElements) { foreach (var load in frameElement.Loads) { if (load.LoadCase.Number == loadCase.Number) { Vector <double> globalLoadVector = frameElement.TransformationMatrix.Transpose() * frameElement.GeneratedLocalLoadVectors[load.LoadCase.Number]; double constant = Math.Pow(10.0, -10.0); for (int i = 0; i < globalLoadVector.Count; i++) { globalLoadVector[i] = Math.Round(globalLoadVector[i], 5); } globalVector[6 * frameElement.Node1.Number + 0] += globalLoadVector[0]; globalVector[6 * frameElement.Node1.Number + 1] += globalLoadVector[1]; globalVector[6 * frameElement.Node1.Number + 2] += globalLoadVector[2]; globalVector[6 * frameElement.Node1.Number + 3] += globalLoadVector[3]; globalVector[6 * frameElement.Node1.Number + 4] += globalLoadVector[4]; globalVector[6 * frameElement.Node1.Number + 5] += globalLoadVector[5]; globalVector[6 * frameElement.Node2.Number + 0] += globalLoadVector[6]; globalVector[6 * frameElement.Node2.Number + 1] += globalLoadVector[7]; globalVector[6 * frameElement.Node2.Number + 2] += globalLoadVector[8]; globalVector[6 * frameElement.Node2.Number + 3] += globalLoadVector[9]; globalVector[6 * frameElement.Node2.Number + 4] += globalLoadVector[10]; globalVector[6 * frameElement.Node2.Number + 5] += globalLoadVector[11]; } } } globalLoadVectors.Add(loadCase.Number, globalVector); } return(globalLoadVectors); }
public static LPSTerm[] IdentityMatrix(int howmany) { //Matrix<double> coeffs = DenseMatrix.CreateIdentity(howmany); //Vector<double> interc = DenseVector.Create(howmany, 0.0); // return new LPSTerm[](coeffs, interc); LPSTerm[] terms = new LPSTerm[howmany]; int pos = 0; for (int i = 0; i < howmany; i++) { Vector <double> coeffs = SparseVector.Create(howmany, 0.0); coeffs[pos++] = 1.0; terms[i] = new LPSTerm(coeffs, 0.0); } return(terms); }
private List <float> forward(SparseVector input) { Layer[] tempLayers = new Layer[_layers.Count]; _layers.CopyTo(tempLayers); for (int i = 0; i < tempLayers.Length; i++) { if (i == 0) { if (input.Count != tempLayers[i].Weights.ColumnCount) { SparseVector tmp = SparseVector.Create(tempLayers[i].Weights.ColumnCount, 1); for (int j = 1; j < tmp.Count; j++) { tmp[j] = input[j - 1]; } input = tmp; } tempLayers[i].Activations = SparseVector.OfVector((tempLayers[i].Weights * input)); } else { if (tempLayers[i].Weights.ColumnCount != tempLayers[i - 1].Activations.Count) { SparseVector tmp = SparseVector.Create(tempLayers[i].Weights.ColumnCount, 1); for (int j = 1; j < tmp.Count; j++) { tmp[j] = tempLayers[i - 1].Activations[j - 1]; } tempLayers[i - 1].Activations = tmp; } else { tempLayers[i - 1].Activations[0] = 1; } tempLayers[i].Activations = SparseVector.OfVector((tempLayers[i].Weights * tempLayers[i - 1].Activations)); } tempLayers[i].Activations = parallelSigmoid(tempLayers[i].Activations); } return(_layers.Last().Activations.ToList()); }
public void ForwardPropagation() { for (int i = 0; i < _layers.Count; i++) { if (i == 0) { if (Inputs.Count != _layers[i].Weights.ColumnCount) { SparseVector tmp = SparseVector.Create(_layers[i].Weights.ColumnCount, 1); for (int j = 1; j < tmp.Count; j++) { tmp[j] = Inputs[j - 1]; } Inputs = tmp; } _layers[i].Activations = SparseVector.OfVector((_layers[i].Weights * Inputs)); } else { if (_layers[i].Weights.ColumnCount != _layers[i - 1].Activations.Count) { SparseVector tmp = SparseVector.Create(_layers[i].Weights.ColumnCount, 1); for (int j = 1; j < tmp.Count; j++) { tmp[j] = _layers[i - 1].Activations[j - 1]; } _layers[i - 1].Activations = tmp; } else { _layers[i - 1].Activations[0] = 1; } _layers[i].Activations = SparseVector.OfVector((_layers[i].Weights * _layers[i - 1].Activations)); } sigmoid(ref _layers[i].Activations); } } //Прохождение вперёд
public Vector <double> CreateVector(int capacity) { return(SparseVector.Create(capacity, 0.0)); }
private LPSTerm() { coefficients_ = SparseVector.Create(vcinfo_.total_varcount_, 0.0); intercept_ = 0.0; }
public VCInfo(int total_varcount) { total_varcount_ = total_varcount; tempmultstorage = new ThreadLocal <Vector <double> >(() => SparseVector.Create(total_varcount, 0.0)); }