static void Main(string[] args) { ILabellingJobRepository jobRepo = new LabellingJobRepositoryFactory().Create(); IJobIterationRepository iterationRepo = new JobIterationRepositoryFactory().Create(); IDataStore dataStore = new DataStoreFactory().CreateOrReplace("TestJack", DataFormats.CIFAR10.GetFormat()); IPredictiveModelFactory modelFactory = new PredictiveModelFactory(); Dictionary <string, string> modelParameters = new Dictionary <string, string>(); modelParameters.Add("k", "15"); modelParameters.Add("numberOfClasses", "10"); IPredictiveModel model = modelFactory.Create("KNearestNeighbour", modelParameters); ISelectionStrategyFactory ssFactory = new SelectionStrategyFactory(); Dictionary <string, string> ssParameters = new Dictionary <string, string>(); ssParameters.Add("diversityFunction", DiversityFunctions.COSINE_DISTANCE.ToString()); ISelectionStrategy selectionStrategy = ssFactory.Create("DiversitySelectionStrategy", ssParameters); IStoppingCriterionFactory scFactory = new StoppingCriterionFactory(); Dictionary <string, string> scParameters = new Dictionary <string, string>(); scParameters.Add("maxLabels", "15"); IStoppingCriterion stoppingCriterion = scFactory.Create("LabelLimit", scParameters); ISeedingStrategyFactory seedingFactory = new SeedingStrategyFactory(); Dictionary <string, string> seedingParameters = new Dictionary <string, string>(); seedingParameters.Add("randomSeed", "15"); ISeedingStrategy seedingStrategy = seedingFactory.Create("RandomSeedingStrategy", seedingParameters); IJobIterationNotifier notifier = new JobIterationNotifierFactory().Create(); IDataFormat dataFormat = DataFormats.CIFAR10.GetFormat(); }
//[TestMethod] public void KNN_Does_Its_Stuff() { const int TRAIN_LIMIT = 50; const int TEST_LIMIT = 500; var parser = new CIFAR10Parser(); IDataFormat dataFormat = parser.Format; //gather the data IEnumerable <object[]> dataInput = parser.ExtractFeaturesAndLabels(File.Open(Path.Combine(basePath, "dataBatchFull.bin"), FileMode.Open), TRAIN_LIMIT); //clear and repopulate the datastore IDataStore dataStore = new DataStoreFactory().CreateOrReplace("TestJack", dataFormat); dataStore.Clear(); var addTask = dataStore.AddLabelledRow(dataInput); Task.WaitAll(addTask); IPredictiveModelFactory modelFactory = new PredictiveModelFactory(); Dictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("k", "5"); parameters.Add("numberOfClasses", "10"); IPredictiveModel model = modelFactory.Create("KNearestNeighbour", parameters); object[][] data = dataStore.GetLabelled().Result.Take(TRAIN_LIMIT).ToArray(); var features = data.GetFeatures <double>(dataFormat); //we know our ids are sequential from 1 so generate a list of ids for ourselves var ids = Enumerable.Range(1, TRAIN_LIMIT).Select(i => (long)i); var labels = dataStore.GetLabelById(ids).Result.Take(TRAIN_LIMIT).Select(l => Convert.ToDouble(l)); model.Train(features, labels); var testData = parser.ExtractFeatureValues(File.Open(Path.Combine(basePath, "dataBatch5.bin"), FileMode.Open)).Take(TEST_LIMIT); var testLabels = parser.ExtractLabels(File.Open(Path.Combine(basePath, "dataBatch5_2.bin"), FileMode.Open)).Take(TEST_LIMIT).ToDictionary(k => k.Key, v => v.Value); int correct = 0; int incorrect = 0; object[][] testArr = testData.ToArray(); foreach (object[] row in testArr) { var prediction = model.Compute(dataFormat.GetFeatures <double>(row).ToArray()); if (System.Convert.ToInt32(prediction) == System.Convert.ToInt32(testLabels[System.Convert.ToInt32(row[0])])) { correct++; } else { incorrect++; } } Assert.IsTrue(correct > 0); }
public static async Task <LabellingJob> RunCreateJobTest() { ILabellingJobRepository jobRepo = new LabellingJobRepositoryFactory().Create(); IJobIterationRepository iterationRepo = new JobIterationRepositoryFactory().Create(); IDataParser parser = new DataParserFactory().Create(DataFormats.CIFAR10); IDataFormat dataFormat = parser.Format; IDataStore dataStore = new DataStoreFactory().CreateOrConnect("TestJack", dataFormat); IPredictiveModelFactory modelFactory = new PredictiveModelFactory(); IPredictiveModel model = modelFactory.Create("LinearRegression"); ISelectionStrategyFactory ssFactory = new SelectionStrategyFactory(); Dictionary <string, string> ssParameters = new Dictionary <string, string>(); ssParameters.Add("randomSeed", "15"); ISelectionStrategy selectionStrategy = ssFactory.Create("RandomSelectionStrategy", ssParameters); IStoppingCriterionFactory scFactory = new StoppingCriterionFactory(); Dictionary <string, string> scParameters = new Dictionary <string, string>(); scParameters.Add("maxLabels", "15"); IStoppingCriterion stoppingCriterion = scFactory.Create("LabelLimit", scParameters); ISeedingStrategyFactory seedingFactory = new SeedingStrategyFactory(); Dictionary <string, string> seedingParameters = new Dictionary <string, string>(); seedingParameters.Add("randomSeed", "15"); ISeedingStrategy seedingStrategy = seedingFactory.Create("RandomSeedingStrategy", seedingParameters); IJobIterationNotifier notifier = new JobIterationNotifierFactory().Create(); int batchSize = 3; int seedSize = 3; var job = await ActiveLoop.CreateLabellingJob( jobRepo : jobRepo, iterationRepo : iterationRepo, dataStore : dataStore, model : model, selectionStrategy : selectionStrategy, stoppingCriterion : stoppingCriterion, seedingStrategy : seedingStrategy, notifier : notifier, dataFormat : dataFormat, batchSize : batchSize, seedSize : seedSize); return(job); }