Exemple #1
0
        /// <summary>
        /// Trains new model
        /// </summary>
        /// <param name="modelId">Model ID of the model to create</param>
        /// <param name="trainingParameters">Parameters of the new model to train</param>
        /// <param name="progressMessageReportDelegate">A delegate for handling progress messages</param>
        /// <param name="cancellationToken">A cancellation token used to abort the operation</param>
        /// <returns>The model training result</returns>
        public async Task <ModelTrainResult> TrainAsync(Guid modelId, ModelTrainingParameters trainingParameters,
                                                        Action <string> progressMessageReportDelegate, CancellationToken cancellationToken)
        {
            Trace.TraceVerbose($"Model training started for model with id '{modelId}'.");
            progressMessageReportDelegate = progressMessageReportDelegate ?? (_ => { });

            // create a temporary local folder for the model training files
            string trainingTempPath = Path.Combine(_trainedModelsLocalRootPath,
                                                   Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

            Directory.CreateDirectory(trainingTempPath);
            IDocumentStore modelDocumentStore = null;

            try
            {
                // report progress
                progressMessageReportDelegate("Downloading Training blobs");

                // download the training files
                TrainingLocalFilePaths localFilePaths =
                    await DownloadTrainingBlobsAsync(modelId, trainingTempPath, trainingParameters, cancellationToken);

                // check if the operation was cancelled
                cancellationToken.ThrowIfCancellationRequested();

                // create user history store if user-to-item is enabled
                if (trainingParameters.EnableUserToItemRecommendations)
                {
                    Trace.TraceInformation($"Creating user history document store for model '{modelId}'");
                    modelDocumentStore = _documentStoreProvider.GetDocumentStore(modelId);
                    modelDocumentStore.CreateIfNotExists();
                }

                // create a model trainer
                var modelTrainer = new ModelTrainer(new Tracer(nameof(ModelTrainer)), modelDocumentStore,
                                                    progressMessageReportDelegate);

                // train the model
                ModelTrainResult result = TrainModel(modelTrainer, modelId, trainingParameters,
                                                     localFilePaths, cancellationToken);
                if (!result.IsCompletedSuccessfuly)
                {
                    Trace.TraceWarning($"Model training failed for model with id '{modelId}'.");
                    return(result);
                }

                // serialize and upload the trained model
                using (Stream modelStream = new MemoryStream())
                {
                    Trace.TraceInformation("Serializing the trained model to a stream");
                    SerializeTrainedModel(result.Model, modelStream, modelId);

                    // rewind the stream before reading
                    modelStream.Seek(0, SeekOrigin.Begin);

                    // upload the serialized model to blob storage
                    await UploadTrainedModelAsync(modelStream, modelId, cancellationToken);
                }

                // return the result
                Trace.TraceInformation($"Model training completed for model with id '{modelId}'. Result: {result}");
                return(result);
            }
            finally
            {
                Trace.TraceInformation($"Deleting the training temporary local folder '{trainingTempPath}'.");
                Directory.Delete(trainingTempPath, true);
            }
        }