Ejemplo n.º 1
0
        public void Run()
        {
            Evaluate();

            var req = new TrainRequest();


            for (var g = 0; g < 1000; g++)
            {
                var timer = Stopwatch.StartNew();

                //Parallel.For(0, 8, (i) =>
                for (var i = 0; i < 8; i++)
                {
                    Console.WriteLine("Starting game " + i);
                    var p0Samples = new List <TrainSample>();
                    var p1Samples = new List <TrainSample>();

                    var state = new SimulationState(SimulationHelpers.GetRandomPieces(), 0);
                    state.Fidelity = SimulationFidelity.NoPiecePlacing;

                    while (!state.GameHasEnded)
                    {
                        var sampleSet = state.ActivePlayer == 0 ? p0Samples : p1Samples;

                        var sample = _ai.MakeMoveWithResult(state);
                        sampleSet.Add(sample);
                    }

                    //Set the winner on the samples from the winner
                    var winningSamples = state.WinningPlayer == 0 ? p0Samples : p1Samples;
                    for (var j = 0; j < winningSamples.Count; j++)
                    {
                        winningSamples[j].IsWin = true;
                    }

                    //TODO: AddRange is terrible performance
                    lock (req)
                    {
                        req.Samples.AddRange(p0Samples);
                        req.Samples.AddRange(p1Samples);
                    }
                }                //);
                timer.Stop();
                Console.WriteLine("Took " + timer.ElapsedMilliseconds);

                //if (req.Samples.Count >= 256)
                {
                    Console.WriteLine("Training " + req.Samples.Count);

                    _client.Train(req);

                    req.Samples.Clear();

                    Evaluate();
                }
            }
            Console.WriteLine("Hi");
            Console.ReadLine();
        }
        /// <summary>
        /// Trains a model from a collection of custom forms in a blob storage container.
        /// </summary>
        /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri.
        /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-training-data-set#upload-your-training-data"/>.</param>
        /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the &lt;link-to-label-tool-doc&gt; to provide training-time labels for training a model. If <c>false</c>, the model will be trained from forms only.</param>
        /// <param name="trainingOptions">A set of options available for configuring the training request.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>
        /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its Value upon successful
        /// completion will contain meta-data about the trained model.</para>
        /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status.
        /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</para>
        /// </returns>
        public virtual async Task <TrainingOperation> StartTrainingAsync(Uri trainingFilesUri, bool useTrainingLabels, TrainingOptions trainingOptions = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri));
            trainingOptions ??= new TrainingOptions();

            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)}");
            scope.Start();

            try
            {
                var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri)
                {
                    SourceFilter = trainingOptions.TrainingFileFilter,
                    UseLabelFile = useTrainingLabels,
                    ModelName    = trainingOptions.ModelName
                };

                ResponseWithHeaders <FormRecognizerTrainCustomModelAsyncHeaders> response = await ServiceClient.TrainCustomModelAsyncAsync(trainRequest).ConfigureAwait(false);

                return(new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> TrainAndSaveModel([FromBody] TrainRequest trainModel)
        {
            var dateString = DateTime.Now.ToString("dd_MM_yyyy_HHmm");
            // @"../CodeGen.MLNET/Data/heart-dataset.csv", "../CodeGen.MLNET/Data/heart-model-"+ dateString + ".zip",',');
            var model = await MachineLearningCore.TrainAndSaveModel <HeartData, HeartPrediction>(trainModel);

            return(Ok(true));
        }
Ejemplo n.º 4
0
 private void DownloadTrainProblems(IEnumerable <Problem> problems)
 {
     foreach (Problem problem in problems.Where(p => p.Size > 3).Shuffle())
     {
         var           trainRequest = new TrainRequest(problem.Size, problem.FoldOperators);
         TrainResponse trainProblem = webApi.Train(trainRequest);
         Source.SaveTrainProblem(trainProblem, problem);
     }
 }
        /// <summary>
        /// Train and Save model by TrainRequest obj.
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <typeparam name="TPrediction"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static async Task <bool> TrainAndSaveModel <TData, TPrediction>(TrainRequest request) where TData : class where TPrediction : class, new()
        {
            if (File.Exists(request.ModelFilePath))
            {
                File.Delete(request.ModelFilePath);
            }

            var model = await CreateModelWithPipeline <TData, TPrediction>(request);

            await model.WriteAsync(request.ModelFilePath);

            return(true);
        }
Ejemplo n.º 6
0
        public TrainResponse Train(TrainProblemType trainProblemType = TrainProblemType.Any, int?size = null)
        {
            var request = new TrainRequest(size);

            switch (trainProblemType)
            {
            case TrainProblemType.Any:
                request.operators = null;
                break;

            case TrainProblemType.Simple:
                request.operators = new string[0];
                break;

            case TrainProblemType.Fold:
                request.operators = new[] { "fold" };
                break;

            case TrainProblemType.Tfold:
                request.operators = new[] { "tfold" };
                break;

            case TrainProblemType.Bonus42:
                request.size = 42;
                break;

            case TrainProblemType.Bonus137:
                request.size = 137;
                break;

            default:
                throw new ArgumentOutOfRangeException("trainProblemType");
            }

            var response = webApi.Train(request);

            return(response);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Trains a model from a collection of custom forms in a blob storage container.
        /// </summary>
        /// <param name="trainingFilesUri">An externally accessible Azure storage blob container Uri.
        /// For more information see <a href="https://docs.microsoft.com/azure/cognitive-services/form-recognizer/build-training-data-set#upload-your-training-data"/>.</param>
        /// <param name="useTrainingLabels">If <c>true</c>, use a label file created in the &lt;link-to-label-tool-doc&gt; to provide training-time labels for training a model. If <c>false</c>, the model will be trained from forms only.</param>
        /// <param name="trainingFileFilter">Filter to apply to the documents in the source path for training.</param>
        /// <param name="cancellationToken">A <see cref="CancellationToken"/> controlling the request lifetime.</param>
        /// <returns>
        /// <para>A <see cref="TrainingOperation"/> to wait on this long-running operation. Its <see cref="TrainingOperation.Value"/> upon successful
        /// completion will contain meta-data about the trained model.</para>
        /// <para>Even if training fails, a model is created in the Form Recognizer account with an "invalid" status.
        /// A <see cref="RequestFailedException"/> will be raised containing the modelId to access this invalid model.</para>
        /// </returns>
        public virtual TrainingOperation StartTraining(Uri trainingFilesUri, bool useTrainingLabels, TrainingFileFilter trainingFileFilter = default, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(trainingFilesUri, nameof(trainingFilesUri));

            using DiagnosticScope scope = Diagnostics.CreateScope($"{nameof(FormTrainingClient)}.{nameof(StartTraining)}");
            scope.Start();

            try
            {
                var trainRequest = new TrainRequest(trainingFilesUri.AbsoluteUri)
                {
                    SourceFilter = trainingFileFilter, UseLabelFile = useTrainingLabels
                };

                ResponseWithHeaders <ServiceTrainCustomModelAsyncHeaders> response = ServiceClient.TrainCustomModelAsync(trainRequest);
                return(new TrainingOperation(response.Headers.Location, ServiceClient, Diagnostics));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
        /// <summary>
        /// Create the pipeline with data send in request
        /// </summary>
        /// <typeparam name="TData"></typeparam>
        /// <typeparam name="TPrediction"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        private static async Task <PredictionModel> CreateModelWithPipeline <TData, TPrediction>(TrainRequest request) where TData : class where TPrediction : class, new()
        {
            List <string> properties = GetPropertiesFromDataSetByType <TData>();

            try
            {
                // Pipeline build.
                var learningPipeline = new LearningPipeline
                {
                    new TextLoader(request.FilePath).CreateFrom <TData>(useHeader: request.UseHeader, separator: request.Separator),
                    new ColumnConcatenator("Features", properties.ToArray()),
                    request.Algorythm,
                };

                var model = learningPipeline.Train <TData, TPrediction>();

                return(model);
            }
            catch (Exception ex)
            {
                return(null);
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Train Model
 /// </summary>
 /// <remarks>
 /// Create and train a custom model. The train request must include a source
 /// parameter that is either an externally accessible Azure Storage blob
 /// container Uri (preferably a Shared Access Signature Uri) or valid path to a
 /// data folder in a locally mounted drive. When local paths are specified,
 /// they must follow the Linux/Unix path format and be an absolute path rooted
 /// to the input mount configuration
 /// setting value e.g., if '{Mounts:Input}' configuration setting value is
 /// '/input' then a valid source path would be '/input/contosodataset'. All
 /// data to be trained is expected to be directly under the source folder.
 /// Subfolders are not supported. Models are trained using documents that are
 /// of the following content type - 'application/pdf', 'image/jpeg' and
 /// 'image/png'."
 /// Other type of content is ignored.
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='trainRequest'>
 /// Request object for training.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <TrainResult> TrainCustomModelAsync(this IFormRecognizerClient operations, TrainRequest trainRequest, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.TrainCustomModelWithHttpMessagesAsync(trainRequest, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Ejemplo n.º 10
0
        private static async Task checkRequestsAsyncHelper()
        {
            IEnumerable <Request> requests = AppDataManager.RetrieveRequests();
            int addedForRequests           = 0;

            try
            {
                foreach (Request req in requests)
                {
                    bool         found    = false;
                    TrainRequest trainReq = new TrainRequest()
                    {
                        from      = req.from,
                        to        = req.to,
                        date      = req.depDateFrame.since,
                        time      = req.depTimeFrame.since,
                        roundTrip = false
                    };
                    TrainResponse trainResp;
                    try
                    {
                        trainResp = Factory.Instance.getTrainResponse(
                            await Requests.makeRequestAsync(Requests.TRAIN_URL, trainReq.getParameters()));
                    }
                    catch (Exception) { continue; }
                    foreach (Train train in trainResp.Trains)
                    {
                        int available = 0;
                        foreach (CoachType type in train.CoachTypes)
                        {
                            try
                            {
                                CoachResponse coachResp = Factory.Instance.getCoachResponse(
                                    await Requests.makeRequestAsync(Requests.COACH_URL, train.getParameters(type, false)));
                                foreach (Coach coach in coachResp.Wrapper.Coaches)
                                {
                                    CarriageResponse carResp = Factory.Instance.getCarriageResponse(
                                        await Requests.makeRequestAsync(Requests.CARRIAGE_URL, train.getParameters(coach)));

                                    available += countAvailavle(req, train, type, coach, carResp.Value.Places);
                                }
                            }
                            catch (Exception) { }
                        }

                        if (!req.available.ContainsKey(train.Number))
                        {
                            req.available[train.Number] = 0;
                        }
                        if (available > req.available[train.Number] + 1)
                        {
                            Notification.SendToast(
                                "З'явилось " + (available - req.available[train.Number]) + " місць",
                                train.Number + " " + req.from.Name + " - " + req.to.Name + ", " + train.From.Date,
                                DateTimeOffset.Now.AddSeconds(15),
                                "UZTracer.TrainPage?" + JsonConvert.SerializeObject(train));

                            found = true;
                        }
                        req.available[train.Number] = available;
                    }
                    addedForRequests += found ? 1 : 0;
                }
            }
            catch (Exception) { }
            finally
            {
                Notification.UpdateBadge(addedForRequests);
                AppDataManager.SaveRequests(requests);
            }
        }
Ejemplo n.º 11
0
 void Train(TrainRequest req, Func <bool> cancelled)
 {
     TrainerCommon.Train(Database, new ObjectId(req.MixtureId), TrainingSet, req.Chromosome, cancelled);
 }
Ejemplo n.º 12
0
 public TrainReply Train(TrainRequest request)
 {
     return(Train(request, null));
 }
Ejemplo n.º 13
0
 void Handle(TrainRequest req)
 {
     TrainerCommon.Train(Database, new ObjectId(req.MixtureId), TrainingSet, req.Chromosome);
 }