/// <summary> /// Tests the case c using classification (majority vote) against the ensemble learner and assumes /// that its last attribute is the target label. Also requires the target attribute so then it knows /// how many variants there are. Unlike the ID3 node, this is a full /// learner or something, so it actually contains its own testing functions. /// </summary> /// <param name="c"></param> /// <returns></returns> public int TestEnsembleClassificaiton(Case c, DAttribute target) { double[] voting = new double[target.numVariants()]; for (int i = 0; i < VoteWeights.Length; i++) { int currentResult = ID3Tools.TestWithTree(c, Trees[i]); voting[currentResult] += VoteWeights[i]; //add the tree's voting power to the bucket for its answer } //find the majority vote in the voting pool int max = -1; double highest = -1; for (int i = 0; i < target.numVariants(); i++) { if (voting[i] > highest) { max = i; highest = voting[i]; } } //max should contain the winning variant number for the attribute. return(max); }
/// <summary> /// Calculates the Final label (output, found in data as #attributeNum) purity for each variant of a dataset and returns it. /// </summary> /// <param name="Data"></param> /// <returns></returns> public static double[] GetLabelDistribution(List <Case> Data, DAttribute attribute) { int numVars = attribute.numVariants(); double[] output = new double[numVars]; double sumWeight = 0; foreach (Case c in Data) { int AVal = (int)c.AttributeVals[attribute.ID]; // the varID of the attribute value held by C if (AVal <= -1) { continue; //value is undefined. proceed to the next value } output[AVal] += c.getWeight(); //increment the corresponding attribute variant by the case's weight (summing number of hits for each) sumWeight += c.getWeight(); } for (int i = 0; i < numVars; i++) //divide each by count to get the relative proportion of the label as oppsed to the count. { output[i] = output[i] / sumWeight; } return(output); }
/* * public imports only affect the directly superior module: * * module A: * import B; * * foo(); // Will fail, because foo wasn't found * * --------------------------- * module B: * import C; * * --------------------------- * module C: * public import D; * * --------------------------- * module D: * void foo() {} * * --------------------------- * Whereas * module B: * public import C; * * -- will compile because we have a closed import hierarchy in which all imports are public. * */ /// <summary> /// Handle the node's static statements (but not the node itself) /// </summary> bool HandleDBlockNode(DBlockNode dbn, MemberFilter VisibleMembers, bool takePublicImportsOnly = false) { if (dbn != null && dbn.StaticStatements != null) { foreach (var stmt in dbn.StaticStatements) { var dstmt = stmt as IDeclarationContainingStatement; if (dstmt != null) { if (takePublicImportsOnly && dstmt is ImportStatement && !DAttribute.ContainsAttribute(dstmt.Attributes, DTokens.Public)) { continue; } /* * Mainly used for selective imports/import module aliases */ if (dstmt.Declarations != null) { foreach (var d in dstmt.Declarations) { if (HandleItem(d)) //TODO: Handle visibility? { return(true); } } } if (dstmt is ImportStatement) { var impStmt = (ImportStatement)dstmt; foreach (var imp in impStmt.Imports) { if (string.IsNullOrEmpty(imp.ModuleAlias)) { if (HandleNonAliasedImport(imp, VisibleMembers)) { return(true); } } } } } } } // Every module imports 'object' implicitly if (!takePublicImportsOnly) { if (HandleNonAliasedImport(_objectImport, VisibleMembers)) { return(true); } } return(false); }
void ApplyAttributes(IStatement n) { var attributes = new List <DAttribute>(); foreach (var attr in BlockAttributes.ToArray()) { attributes.Add(attr); } while (DeclarationAttributes.Count > 0) { var attr = DeclarationAttributes.Pop(); // If accessor already in attribute array, remove it if (DTokens.VisModifiers[attr.Token]) { DAttribute.CleanupAccessorAttributes(attributes); } if (attr.IsProperty || !DAttribute.ContainsAttribute(attributes, attr.Token)) { attributes.Add(attr); } } n.Attributes = attributes.Count == 0 ? null : attributes.ToArray(); }
public static DBlockNode ParseMixinDeclaration(MixinStatement mx, ResolutionContext ctxt, out VariableValue vv) { var literal = GetMixinContent(mx, ctxt, false, out vv); if (literal == null) { return(null); } var ast = DParser.ParseDeclDefs(literal); if (ast == null) { return(null); } ast.Parent = mx.ParentNode; foreach (var ch in ast) { if (mx.Attributes != null) { var dn = ch as DNode; if (dn != null) { if (dn.Attributes == null) { dn.Attributes = new List <DAttribute>(mx.Attributes); } else { dn.Attributes.AddRange(mx.Attributes); } } } ch.Parent = mx.ParentNode; } if (mx.Attributes != null) { foreach (var ss in ast.StaticStatements) { if (ss.Attributes == null) { ss.Attributes = mx.Attributes; } else { var attrs = new DAttribute[mx.Attributes.Length + ss.Attributes.Length]; mx.Attributes.CopyTo(attrs, 0); ss.Attributes.CopyTo(attrs, mx.Attributes.Length); } } } return(ast); }
public bool ContainsAttribute(DAttribute attr) { if(attr is Modifier) return ContainsAttribute((attr as Modifier).Token); else if(attr is BuiltInAtAttribute) return ContainsPropertyAttribute((attr as BuiltInAtAttribute).Kind); else if(attr is UserDeclarationAttribute) return ContainsPropertyAttribute((attr as UserDeclarationAttribute).AttributeExpression); return false; }
void PushAttribute(DAttribute attr, bool BlockAttributes) { var stk = BlockAttributes?this.BlockAttributes:this.DeclarationAttributes; // If attr would change the accessability of an item, remove all previously found (so the most near attribute that's next to the item is significant) if (DTokens.VisModifiers[attr.Token]) { DAttribute.CleanupAccessorAttributes(stk); } stk.Push(attr); }
} // End Sub private static bool IsRoaming(SettingsProperty prop) { // Determine if the setting is marked as Roaming foreach (DictionaryEntry d in prop.Attributes) { DAttribute a = DirectCast(d.Value, Attribute); if (typeof(a) == System.Configuration.SettingsManageabilityAttribute) { return true; } // End If } // Next return false; } // End Function
/// <summary> /// Given an array of the same attributes used to build a tree containing the current node, list the node's information. As a note, the /// attributes don't need to be ordered. /// </summary> /// <param name="attributes"></param> /// <returns></returns> public String WriteToString(DAttribute[] attributes) { String output = ""; int depth = this.depth(); for (int i = 0; i < depth; i++) { output += "\t"; } //find the attribute by its ID and print out the relevant information. output += DAttribute.findByID(attributes, this.AttributeID).Name + " " + depth + "\t"; if (Children == null) { output += "FinalLabel == " + attributes[AttributeID].GetVariants()[Value]; } output += "\n"; return(output); }
void PushAttribute(DAttribute attr, bool BlockAttributes) { var stk = BlockAttributes?this.BlockAttributes:this.DeclarationAttributes; var m = attr as Modifier; if (m != null) { // If attr would change the accessability of an item, remove all previously found (so the most near attribute that's next to the item is significant) if (DTokens.IsVisibilityModifier(m.Token)) { Modifier.CleanupAccessorAttributes(stk, m.Token); } else { Modifier.RemoveFromStack(stk, m.Token); } } stk.Push(attr); }
void ApplyAttributes(DNode n) { foreach (var attr in BlockAttributes.ToArray()) { n.Attributes.Add(attr); } while (DeclarationAttributes.Count > 0) { var attr = DeclarationAttributes.Pop(); // If accessor already in attribute array, remove it if (DTokens.VisModifiers[attr.Token]) { DAttribute.CleanupAccessorAttributes(n.Attributes); } if (attr.IsProperty || !DAttribute.ContainsAttribute(n.Attributes.ToArray(), attr.Token)) { n.Attributes.Add(attr); } } }
static bool CanShowAttribute(DAttribute attr, bool showStorageClasses) { if (attr is DeclarationCondition) { return(false); } var mod = attr as Modifier; if (showStorageClasses || mod == null) { return(true); } switch (mod.Token) { case DTokens.Auto: case DTokens.Enum: return(false); default: return(true); } }
public static void Main() { // ========= Part 1 ============= // if (BuildCarTrees) { //This is the car example. List <DAttribute> attributeCars = new List <DAttribute>(7); //while I could auto detect this, it's much easier to read the trees if I name the DataAttributes ahead of time //below data descriptions come from data-desc.txt, located near the data for this training data. string[] AVariants = new string[] { "vhigh", "high", "med", "low" }; //array of attribute variants to pass in to an attribute attributeCars.Add(new DAttribute("buying", 0, new List <string>(AVariants), DAttribute.Type.Categorical, false)); attributeCars.Add(new DAttribute("maint", 1, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "2", "3", "4", "5more" }; attributeCars.Add(new DAttribute("doors", 2, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "2", "4", "more" }; attributeCars.Add(new DAttribute("persons", 3, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "small", "med", "big" }; attributeCars.Add(new DAttribute("lug_boot", 4, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "low", "med", "high" }; attributeCars.Add(new DAttribute("safety", 5, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "unacc", "acc", "good", "vgood" }; attributeCars.Add(new DAttribute("label", 6, new List <string>(AVariants), DAttribute.Type.Categorical, true)); List <Case> TrainCars = DRT.ParseCSV(attributeCars.ToArray(), TestPath + @"\car\train.csv"); List <Case> TestCars = DRT.ParseCSV(attributeCars.ToArray(), TestPath + @"\car\test.csv"); StringBuilder TreeLayout = new StringBuilder(); for (int depth = 1; depth < 7; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.IG); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree); Double TestError = ID3Tools.FindTestError(TestCars, attributeCars, Tree); TreeLayout.Append("Information Gain Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an IG Tree"); } for (int depth = 1; depth < 7; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.GI); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree); Double TestError = ID3Tools.FindTestError(TestCars, attributeCars, Tree); TreeLayout.Append("Gini Index Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished a GI Tree"); } for (int depth = 1; depth < 7; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeCars, TrainCars, depth, ID3Tools.EntropyCalucalation.ME); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainCars, attributeCars, Tree); Double TestError = ID3Tools.FindTestError(TestCars, attributeCars, Tree); TreeLayout.Append("Majority Error Cars, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeCars.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an ME Tree"); } Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsCars.txt"); System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsCars.txt", TreeLayout.ToString()); } // ========= Part 2 ============= // // bank information if (BuildBankTrees) { List <DAttribute> attributeBank = new List <DAttribute>(7); //Once again, could auto detect, but doing so makes the data harder to read. Furthermore, autodetecting doesn't work for filling in missing values. //below data descriptions come from data-desc.txt, located near the data for this training data. string[] AVariants; //age being numeric means that the actual variants will be figured out at run time. The variant will be overwritten when we pull in the testing data. attributeBank.Add(new DAttribute("age", 0, null, DAttribute.Type.BinaryNumeric, false)); AVariants = new string[] { "admin.", "unknown", "unemployed", "management", "housemaid", "entrepreneur", "student", "blue-collar", "self-employed", "retired", "technician", "services" }; attributeBank.Add(new DAttribute("job", 1, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "married", "divorced", "single" }; attributeBank.Add(new DAttribute("marital", 2, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "unknown", "secondary", "primary", "tertiary" }; attributeBank.Add(new DAttribute("education", 3, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "yes", "no" }; attributeBank.Add(new DAttribute("default", 4, new List <string>(AVariants), DAttribute.Type.Categorical, false)); attributeBank.Add(new DAttribute("balance", 5, null, DAttribute.Type.BinaryNumeric, false)); AVariants = new string[] { "yes", "no" }; attributeBank.Add(new DAttribute("housing", 6, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "yes", "no" }; attributeBank.Add(new DAttribute("loan", 7, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "unknown", "telephone", "cellular" }; attributeBank.Add(new DAttribute("contact", 8, new List <string>(AVariants), DAttribute.Type.Categorical, false)); attributeBank.Add(new DAttribute("day", 9, null, DAttribute.Type.BinaryNumeric, false)); AVariants = new string[] { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; attributeBank.Add(new DAttribute("month", 10, new List <string>(AVariants), DAttribute.Type.Categorical, false)); attributeBank.Add(new DAttribute("duration", 11, null, DAttribute.Type.BinaryNumeric, false)); attributeBank.Add(new DAttribute("campaign", 12, null, DAttribute.Type.BinaryNumeric, false)); attributeBank.Add(new DAttribute("pdays", 13, null, DAttribute.Type.BinaryNumeric, false)); attributeBank.Add(new DAttribute("previous", 14, null, DAttribute.Type.BinaryNumeric, false)); AVariants = new string[] { "unknown", "other", "failure", "success" }; //If unknown needs to be filled in, remove it from this list. attributeBank.Add(new DAttribute("poutcome", 15, new List <string>(AVariants), DAttribute.Type.Categorical, false)); AVariants = new string[] { "yes", "no" }; attributeBank.Add(new DAttribute("result", 16, new List <string>(AVariants), DAttribute.Type.Categorical, true)); if (BuildBankTreeNormal) { List <Case> TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true); List <Case> TestBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\test.csv", false); StringBuilder TreeLayout = new StringBuilder(); for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.IG); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Information Gain Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an IG Tree"); } for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.GI); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Gini Index Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished a GI Tree"); } for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.ME); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Majority Error Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an ME Tree"); } Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsBankNormal.txt"); System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankNormal.txt", TreeLayout.ToString()); } if (BuildBankMissingVals) { //In this case, the "unknown" values in poutcome attributeBank[15] = new DAttribute("poutcome", 15, new List <string>(new string[] { "unknown", "other", "failure", "success" }), DAttribute.Type.Categorical, false); //Now we rebuild all the datasets, which will have elements filled in by the majority elements. List <Case> TrainBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\train.csv", true); List <Case> TestBank = DRT.ParseCSV(attributeBank.ToArray(), TestPath + @"\bank\test.csv", false); StringBuilder TreeLayout = new StringBuilder(); for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.IG); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Information Gain Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an IG Tree"); } for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.GI); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Gini Index Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished a GI Tree"); } for (int depth = 1; depth < 17; depth++) { ID3_Node Tree = ID3Tools.ID3(attributeBank, TrainBank, depth, ID3Tools.EntropyCalucalation.ME); //add the tree to the string builder and prepare to write it to a file. Double TrainError = ID3Tools.FindTestError(TrainBank, attributeBank, Tree); Double TestError = ID3Tools.FindTestError(TestBank, attributeBank, Tree); TreeLayout.Append("Majority Error Bank, Max Depth of " + depth + ". Test Error = " + TestError + ". TrainError = " + TrainError + " \n \n" + Tree.PrintTree(attributeBank.ToArray()) + "\n ----------------------------------------------------------------- \n"); Console.WriteLine("Finished an ME Tree"); } Console.WriteLine("Writing all results to DecisionTree/TestingData/RunResults/ResultsBankMissingVals.txt"); System.IO.File.WriteAllText(TestPath + @"/RunResults/ResultsBankMissingVals.txt", TreeLayout.ToString()); } } }
public static void Main() { //Attributes for the data DAttribute[] Attributes = new DAttribute[5]; Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false); Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false); Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false); Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false); Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false); List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false); List <Case> TestBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false); //Convert output to -1,1 as opposed to 0,1 Case.ColXtoY(TrainBank, 4, 0, -1); Case.ColXtoY(TestBank, 4, 0, -1); StringBuilder output = new StringBuilder(); //begin testing double[] C = new double[] { 100.0 / 873.0, 500.0 / 873.0, 700.0 / 873.0 }; Console.WriteLine("Starting primal Subgradient Decent with C = { 100/873, 500/873, 700/873 }"); Console.WriteLine("====================================================================================="); Console.WriteLine("\nUsing NewLR = LR / (1 + LR * T / D) for learning rate. \n"); //set up parameters double LearningRate = .2; double LearningAdjust = .75; int Seed = 1500; //report Console.WriteLine("\tBase Learning Rate = " + LearningRate); Console.WriteLine("\tNum epochs (T) = 100"); Console.WriteLine("\tLearning Rate Adjustment = " + LearningAdjust); SVMGradient current; //set up the variable for the SVM for the all the tests for (int j = 0; j < 3; j++) { current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank); Console.WriteLine("\nCreated new primal learner with C = " + C[j]); for (int i = 0; i < 100; i++) { if (i % 10 == 9) { Console.WriteLine("\tCompleted " + (i + 1) + " Epochs."); Console.WriteLine("\tTraining error = " + current.getTrainingError()); } current.PGradientEpoch(1); //do 100 epochs } Console.WriteLine("\n\tTraining error = " + current.getTrainingError()); Console.WriteLine("\tTesting error = " + current.getTestError(TestBank)); double[] weight = current.getWeight(); Console.Write("\tWeight = { " + weight[0]); for (int i = 1; i < weight.Length; i++) { Console.Write(", " + weight[i]); } Console.Write("}\n"); Console.WriteLine("\tBias = " + current.getBias()); } Console.WriteLine("-------------------------------------------------------------------------------------\n"); Console.WriteLine("Using NewLR = LR/ (1 + T) for learning rate.\n"); Console.WriteLine("\tBase Learning Rate = " + LearningRate); Console.WriteLine("\tNum epochs (T) = 100"); Console.WriteLine("\tLearning Rate Adjustment = " + LearningAdjust); for (int j = 0; j < 3; j++) { current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank); Console.WriteLine("\nCreated new primal learner with C = " + C[j]); for (int i = 0; i < 100; i++) { if (i % 10 == 9) { Console.WriteLine("\tCompleted " + (i + 1) + " Epochs."); Console.WriteLine("\tTraining error = " + current.getTrainingError()); } current.PGradientEpoch(1); //do 100 epochs } Console.WriteLine("\n\tTraining error = " + current.getTrainingError()); Console.WriteLine("\tTesting error = " + current.getTestError(TestBank)); double[] weight = current.getWeight(); Console.Write("\tWeight = { " + weight[0]); for (int i = 1; i < weight.Length; i++) { Console.Write(", " + weight[i]); } Console.Write("}\n"); Console.WriteLine("\tBias = " + current.getBias()); } //let the user read the stuff on screen. Console.WriteLine("\n\n\nFinished execution. Hit any key to exit."); Console.Read(); }
public static void Main() { //Attributes for the data DAttribute[] Attributes = new DAttribute[5]; Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false); Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false); Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false); Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false); Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false); List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false); List <Case> TestBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false); StringBuilder output = new StringBuilder(); //start testing here PerceptronLearner NormalPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Normal); output.Append("NormalPerceptron \nTrain,Test\n"); for (int i = 1; i < 12; i++) { NormalPerceptron.SingleEpoch(); //do an epoch then test it double trainError = NormalPerceptron.GetError(TrainBank); double testError = NormalPerceptron.GetError(TestBank); Console.WriteLine("Training error Normal Epoch# " + i + " = " + trainError); Console.WriteLine("Testing error Normal Epoch# " + i + " = " + testError); output.Append(trainError + "," + testError + "\n"); } Console.Write("Final Weight = {"); foreach (double d in NormalPerceptron.getWeight()) { Console.Write(d + ", "); } Console.Write("} with a bias of " + NormalPerceptron.getBias() + ". \n"); PerceptronLearner VotedPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Voted); output.Append("\nVotedPerceptron \nTrain,Test\n"); for (int i = 1; i < 12; i++) { VotedPerceptron.SingleEpoch(); double trainError = VotedPerceptron.GetError(TrainBank); double testError = VotedPerceptron.GetError(TestBank); Console.WriteLine("Training error Voted Epoch# " + i + " = " + trainError); Console.WriteLine("Testing error Voted Epoch# " + i + " = " + testError); output.Append(trainError + "," + testError + "\n"); } Console.Write("Final Weight = {"); foreach (double d in VotedPerceptron.getWeight()) { Console.Write(d + ", "); } Console.Write("} with a bias of " + VotedPerceptron.getBias() + ". \n"); PerceptronLearner AveragedPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Averaged); output.Append("\nAveragedPerceptron \nTrain,Test\n"); for (int i = 1; i < 12; i++) { AveragedPerceptron.SingleEpoch(); double trainError = AveragedPerceptron.GetError(TrainBank); double testError = AveragedPerceptron.GetError(TestBank); Console.WriteLine("Training error Averaged Epoch# " + i + " = " + trainError); Console.WriteLine("Testing error Averaged Epoch# " + i + " = " + testError); output.Append(trainError + "," + testError + "\n"); } Console.Write("Final Weight = {"); foreach (double d in AveragedPerceptron.getWeight()) { Console.Write(d + ", "); } Console.Write("} with a bias of " + AveragedPerceptron.getBias() + ". \n"); PerceptronLearner MarginPerceptron = new PerceptronLearner(10, TrainBank, 1, 1500, PerceptronLearner.PType.Margin, 6); output.Append("\nMarginPerceptron \nTrain,Test\n"); for (int i = 1; i < 12; i++) { MarginPerceptron.SingleEpoch(); double trainError = MarginPerceptron.GetError(TrainBank); double testError = MarginPerceptron.GetError(TestBank); Console.WriteLine("Training error Margin Epoch# " + i + " = " + trainError); Console.WriteLine("Testing error Margin Epoch# " + i + " = " + testError); output.Append(trainError + "," + testError + "\n"); } Console.Write("Final Weight = {"); foreach (double d in NormalPerceptron.getWeight()) { Console.Write(d + ", "); } Console.Write("} with a bias of " + NormalPerceptron.getBias() + ". \n"); Console.WriteLine("\n\n\n\n\n\n Writing all results to TestingData/RunResults/Perceptron.csv"); System.IO.File.WriteAllText(TestPath + @"/RunResults/Perceptron.csv", output.ToString()); Console.Read(); }
public static void Main() { //Attributes for the data DAttribute[] Attributes = new DAttribute[5]; Attributes[0] = new DAttribute("Varaince", 1, null, DAttribute.Type.Numeric, false); Attributes[1] = new DAttribute("Skew", 1, null, DAttribute.Type.Numeric, false); Attributes[2] = new DAttribute("Curtosis", 1, null, DAttribute.Type.Numeric, false); Attributes[3] = new DAttribute("Entropy", 1, null, DAttribute.Type.Numeric, false); Attributes[4] = new DAttribute("Genuine", 1, new List <String>(new String[] { "0", "1" }), DAttribute.Type.Categorical, false); List <Case> TrainBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\train.csv", false); List <Case> TestBank = DRT.ParseCSV(Attributes, TestPath + @"\bank-note\bank-note\test.csv", false); //Convert output to -1,1 as opposed to 0,1 Case.ColXtoY(TrainBank, 4, 0, -1); Case.ColXtoY(TestBank, 4, 0, -1); //StringBuilder output = new StringBuilder(); //begin testing int[] NumNeurons = new int[] { 5, 10, 25, 50, 100 }; Console.WriteLine("Testing 3 layer neural nets with X neurons per layer = { 5, 10, 25, 50, 100 }"); Console.WriteLine("====================================================================================="); Console.WriteLine("\nUsing NewLR = Base LR / (1 + Base LR * T / D) for learning rate. \n"); //set up parameters double LearningRate = 1; //arbitrary number int Seed = 1500; //report Console.WriteLine("\tBase Learning Rate = " + LearningRate); Console.WriteLine("\tNum epochs (T) = 100"); NeuralNet current; //set up the variable for the SVM for the all the tests for (int j = 0; j < NumNeurons.Length; j++) { current = new NeuralNet(LearningRate, Seed, 4, NumNeurons[j], 2); //two hidden layers + 1 output, always Console.WriteLine("\nCreated new three layer Neural Net with " + NumNeurons[j] + " Neurons per layer."); for (int i = 0; i < 20; i++) { current.runEpochs(10, TrainBank); //do 100 epochs Console.WriteLine("\tCompleted " + (i + 1) * 10 + " Epochs."); Console.WriteLine("\tTraining error at " + (i + 1) * 10 + " epochs = " + current.getError(TrainBank)); } Console.WriteLine("\n\tFinal Training error = " + current.getError(TrainBank)); Console.WriteLine("\tTesting error \t= " + current.getError(TestBank)); } /* * Console.WriteLine("-------------------------------------------------------------------------------------\n"); * * Console.WriteLine("Using NewLR = LR/ (1 + T) for learning rate.\n"); * Console.WriteLine("\tBase Learning Rate = " + LearningRate); * Console.WriteLine("\tNum epochs (T) = 100"); * * for (int j = 0; j < 3; j++) * { * current = new SVMGradient(C[j], LearningRate, LearningAdjust, Seed, TrainBank); * * Console.WriteLine("\nCreated new primal learner with C = " + C[j]); * * for (int i = 0; i < 100; i++) * { * if (i % 10 == 9) * { * Console.WriteLine("\tCompleted " + (i + 1) + " Epochs."); * Console.WriteLine("\tTraining error = " + current.getTrainingError()); * } * current.PGradientEpoch(1); //do 100 epochs * * } * * Console.WriteLine("\n\tTraining error = " + current.getTrainingError()); * Console.WriteLine("\tTesting error = " + current.getTestError(TestBank)); * double[] weight = current.getWeight(); * Console.Write("\tWeight = { " + weight[0]); * for (int i = 1; i < weight.Length; i++) * { * Console.Write(", " + weight[i]); * } * Console.Write("}\n"); * Console.WriteLine("\tBias = " + current.getBias()); * } */ //let the user read the stuff on screen. Console.WriteLine("\n\n\nFinished execution. Hit any key to exit."); Console.Read(); }
void PushAttribute(DAttribute attr, bool BlockAttributes) { var stk=BlockAttributes?this.BlockAttributes:this.DeclarationAttributes; var m = attr as Modifier; if(m!=null) // If attr would change the accessability of an item, remove all previously found (so the most near attribute that's next to the item is significant) if (DTokens.VisModifiers[m.Token]) Modifier.CleanupAccessorAttributes(stk, m.Token); else Modifier.RemoveFromStack(stk, m.Token); stk.Push(attr); }
public static DModule ParseMixinDeclaration(MixinStatement mx, ResolutionContext ctxt) { ISyntaxRegion sr; var literal = GetMixinContent(mx, ctxt, false, out sr); if(sr is DModule) return (DModule)sr; else if(literal == null) return null; var ast = (DModule)DParser.ParseString(literal, true); mixinDeclCache.Add(ctxt, mx, ast); if(ast == null) return null; foreach(var ch in ast) { if(mx.Attributes!=null) { var dn = ch as DNode; if(dn!=null) { if(dn.Attributes==null) dn.Attributes = new List<DAttribute>(mx.Attributes); else dn.Attributes.AddRange(mx.Attributes); } } ch.Parent = mx.ParentNode; } if(mx.Attributes!=null) foreach(var ss in ast.StaticStatements) { if(ss.Attributes == null) ss.Attributes = mx.Attributes; else{ var attrs = new DAttribute[mx.Attributes.Length + ss.Attributes.Length]; mx.Attributes.CopyTo(attrs,0); ss.Attributes.CopyTo(attrs,mx.Attributes.Length); } } return ast; }
public AttributeMetaDeclarationSection(DAttribute attr) : base(attr) { }
public AttributeCompletionProvider(DAttribute attr, ICompletionDataGenerator gen) : base(gen) { this.Attribute = attr; }
public static DModule ParseMixinDeclaration(MixinStatement mx, ResolutionContext ctxt) { ISyntaxRegion sr; var literal = GetMixinContent(mx, ctxt, false, out sr); if (sr is DModule) { return((DModule)sr); } else if (literal == null) { return(null); } var ast = (DModule)DParser.ParseString(literal, true); mixinDeclCache.Add(ctxt, mx, ast); if (ast == null) { return(null); } foreach (var ch in ast) { if (mx.Attributes != null) { var dn = ch as DNode; if (dn != null) { if (dn.Attributes == null) { dn.Attributes = new List <DAttribute>(mx.Attributes); } else { dn.Attributes.AddRange(mx.Attributes); } } } ch.Parent = mx.ParentNode; } if (mx.Attributes != null) { foreach (var ss in ast.StaticStatements) { if (ss.Attributes == null) { ss.Attributes = mx.Attributes; } else { var attrs = new DAttribute[mx.Attributes.Length + ss.Attributes.Length]; mx.Attributes.CopyTo(attrs, 0); ss.Attributes.CopyTo(attrs, mx.Attributes.Length); } } } return(ast); }
List<INode> Decl(bool HasStorageClassModifiers,IBlockNode Scope, DAttribute StorageClass = null) { var startLocation = la.Location; var initialComment = GetComments(); ITypeDeclaration ttd = null; CheckForStorageClasses(Scope as DBlockNode); // Autodeclaration if(StorageClass == null) StorageClass = DTokens.ContainsStorageClass(DeclarationAttributes); if (laKind == Enum) { Step(); PushAttribute(StorageClass = new Modifier(Enum) { Location = t.Location, EndLocation = t.EndLocation },false); } // If there's no explicit type declaration, leave our node's type empty! if ((StorageClass != Modifier.Empty && laKind == (Identifier) && (DeclarationAttributes.Count > 0 || Lexer.CurrentPeekToken.Kind == OpenParenthesis))) // public auto var=0; // const foo(...) {} { if (Lexer.CurrentPeekToken.Kind == Assign || Lexer.CurrentPeekToken.Kind ==OpenParenthesis) { } else if (Lexer.CurrentPeekToken.Kind == Semicolon) { SemErr(t.Kind, "Initializer expected for auto type, semicolon found!"); } else ttd = BasicType(); } else ttd = BasicType(); if (IsEOF) { /* * T! -- tix.Arguments == null * T!(int, -- last argument == null * T!(int, bool, -- ditto * T!(int) -- now every argument is complete */ var tix=ttd as TemplateInstanceExpression; if (tix != null) { if (tix.Arguments == null || tix.Arguments.Length == 0 || (tix.Arguments [tix.Arguments.Length - 1] is TokenExpression && (tix.Arguments [tix.Arguments.Length - 1] as TokenExpression).Token == DTokens.INVALID)) { LastParsedObject = ttd; return null; } } else if (ttd is MemberFunctionAttributeDecl && (ttd as MemberFunctionAttributeDecl).InnerType == null) { LastParsedObject = ttd; return null; } } // Declarators var firstNode = Declarator(ttd,false, Scope); if (firstNode == null) return null; firstNode.Description = initialComment; firstNode.Location = startLocation; // Check for declaration constraints if (laKind == (If)) Constraint(firstNode); // BasicType Declarators ; if (laKind==Assign || laKind==Comma || laKind==Semicolon) { // DeclaratorInitializer if (laKind == (Assign)) { TrackerVariables.InitializedNode = firstNode; var dv = firstNode as DVariable; if(dv!=null) dv.Initializer = Initializer(Scope); } firstNode.EndLocation = t.EndLocation; var ret = new List<INode>(); ret.Add(firstNode); // DeclaratorIdentifierList while (laKind == Comma) { Step(); if (Expect(Identifier)) { var otherNode = new DVariable(); LastParsedObject = otherNode; /// Note: In DDoc, all declarations that are made at once (e.g. int a,b,c;) get the same pre-declaration-description! otherNode.Description = initialComment; otherNode.AssignFrom(firstNode); otherNode.Location = t.Location; otherNode.Name = t.Value; otherNode.NameLocation = t.Location; if (laKind == (Assign)) { TrackerVariables.InitializedNode = otherNode; otherNode.Initializer = Initializer(Scope); } otherNode.EndLocation = t.EndLocation; ret.Add(otherNode); } else break; } if (Expect(Semicolon)) LastParsedObject = null; // Note: In DDoc, only the really last declaration will get the post semicolon comment appended if (ret.Count > 0) ret[ret.Count - 1].Description += CheckForPostSemicolonComment(); return ret; } // BasicType Declarator FunctionBody else if (firstNode is DMethod && (IsFunctionBody || IsEOF)) { firstNode.Description += CheckForPostSemicolonComment(); FunctionBody((DMethod)firstNode); firstNode.Description += CheckForPostSemicolonComment(); var ret = new List<INode> (); ret.Add (firstNode); return ret; } else SynErr(OpenCurlyBrace, "; or function body expected after declaration stub."); return null; }
/// <summary> /// /// </summary> /// <param name="module"></param> /// <param name="previouslyParsedAttribute"></param> /// <param name="RequireDeclDef">If no colon and no open curly brace is given as lookahead, a DeclDef may be parsed otherwise, if parameter is true.</param> /// <returns></returns> IMetaDeclaration AttributeTrail(DBlockNode module, DAttribute previouslyParsedAttribute, bool RequireDeclDef = false) { if (laKind == Colon) { Step(); PushAttribute(previouslyParsedAttribute, true); AttributeMetaDeclarationSection metaDecl = null; //TODO: Put all remaining block/decl(?) attributes into the section definition.. if(module!=null) module.Add(metaDecl = new AttributeMetaDeclarationSection(previouslyParsedAttribute) { EndLocation = t.EndLocation }); return metaDecl; } else PushAttribute(previouslyParsedAttribute, false); if (laKind == OpenCurlyBrace) return AttributeBlock(module); else { if (RequireDeclDef) DeclDef(module); return new AttributeMetaDeclaration(previouslyParsedAttribute) { EndLocation = previouslyParsedAttribute.EndLocation }; } }