public Insert ( |
||
v | Vector to insert | |
index | int | The zero based row / column. |
t | VectorType | Vector orientation |
insertAfter | bool | Insert after or before the last row / column |
Résultat | Matrix |
/// <summary> /// Converts the experience pair into their equivalent math forms. /// </summary> /// <param name="state">IMDPState instance.</param> /// <param name="nodes">List of nodes added to the result set.</param> /// <param name="states">Matrix to store contained successor state vectors.</param> /// <param name="actions">Vector to store the contained action values.</param> /// <param name="statesP">Matrix to store all contained successor transition state vectors.</param> /// <param name="rewards">Vector to store all contained reward values.</param> /// <returns>HashSet<string></returns> private static void Convert(this IMDPState state, ref List<string> nodes, ref Matrix states, ref Vector actions, ref Matrix statesP, ref Vector rewards) { if (state != null) { foreach (IMDPSuccessor successor in state.GetSuccessors()) { if (state.Features.Length != states.Cols) states = Matrix.Reshape(states, states.Rows, state.Features.Length); if (state.Features.Length != statesP.Cols) statesP = Matrix.Reshape(statesP, statesP.Rows, ((IMDPState) successor.State).Features.Length); string id = $"{state.Id}:{successor.State.Id}"; if (!nodes.Contains(id)) { states = states.Insert(state.ToVector(), states.Rows - 1, VectorType.Row); actions = actions.Insert(actions.Length - 1, successor.Action.Id); statesP = statesP.Insert(((IMDPState) successor.State).ToVector(), statesP.Rows - 1, VectorType.Row); rewards = rewards.Insert(rewards.Length - 1, successor.Reward); nodes.Add(id); } if (!successor.State.IsTerminal) { var successorState = ((IMDPState) successor.State); if (successorState.Id != state.Id) successorState.Convert(ref nodes, ref states, ref actions, ref statesP, ref rewards); } } } }
/// <summary>Generate Logistic Regression model based on a set of examples.</summary> /// <param name="X">The Matrix to process.</param> /// <param name="y">The Vector to process.</param> /// <returns>Model.</returns> public override IModel Generate(Matrix X, Vector y) { X = IncreaseDimensions(X, this.PolynomialFeatures); this.Preprocess(X); // guarantee 1/0 based label vector y = y.ToBinary(f => f == 1d, falseValue: 0d); // add intercept term X = X.Insert(Vector.Ones(X.Rows), 0, VectorType.Col, false); Vector theta = Vector.Rand(X.Cols); // run gradient descent var optimizer = new numl.Math.Optimization.Optimizer(theta, this.MaxIterations, this.LearningRate) { CostFunction = new numl.Math.Functions.Cost.LogisticCostFunction() { X = X, Y = y, Lambda = this.Lambda, Regularizer = new numl.Math.Functions.Regularization.L2Regularizer(), LogisticFunction = this.LogisticFunction } }; optimizer.Run(); LogisticRegressionModel model = new LogisticRegressionModel() { Descriptor = this.Descriptor, NormalizeFeatures = base.NormalizeFeatures, FeatureNormalizer = base.FeatureNormalizer, FeatureProperties = base.FeatureProperties, Theta = optimizer.Properties.Theta, LogisticFunction = this.LogisticFunction, PolynomialFeatures = this.PolynomialFeatures }; return model; }