Example #1
0
        public List <PredictDictionary> PredictMultiple(PredictorPredictContext ctx, List <PredictDictionary> inputs)
        {
            using (HeavyProfiler.LogNoStackTrace("PredictMultiple"))
            {
                lock (lockKey)
                {
                    var model = (TensorFlowModel)ctx.Model !;
                    tf.compat.v1.disable_eager_execution();
                    model.Session.as_default();
                    model.Graph.as_default();

                    var result = new List <PredictDictionary>();
                    foreach (var input in inputs)
                    {
                        NDArray           inputValue     = GetValueForPredict(ctx, input);
                        NDArray           outputValuesND = model.Session.run(model.CalculatedOutput, (model.InputPlaceholder, inputValue));
                        float[]           outputValues   = outputValuesND.ToArray <float>();
                        PredictDictionary dic            = GetPredictionDictionary(outputValues, ctx, input.Options !);
                        result.Add(dic);
                    }

                    return(result);
                }
            }
        }
Example #2
0
    public PredictDictionary Clone()
    {
        var result = new PredictDictionary(Predictor, this.Options, this.Entity);

        result.MainQueryValues.AddRange(MainQueryValues.ToDictionaryEx());
        result.SubQueries.AddRange(SubQueries, kvp => kvp.Key, kvp => kvp.Value.Clone());
        return(result);
    }
Example #3
0
    public static PredictDictionary GetInputsEmpty(this PredictorPredictContext ctx, PredictionOptions?options = null)
    {
        var result = new PredictDictionary(ctx.Predictor, options, null)
        {
            MainQueryValues = ctx.Predictor.MainQuery.Columns.Select((c, i) => KeyValuePair.Create(c, (object?)null)).ToDictionaryEx(),
            SubQueries      = ctx.Predictor.SubQueries.ToDictionary(sq => sq, sq => new PredictSubQueryDictionary(sq)
            {
                SubQueryGroups = new Dictionary <object?[], Dictionary <PredictorSubQueryColumnEmbedded, object?> >(ObjectArrayComparer.Instance)
            })
        };

        return(result);
    }
        private Value GetValueForPredict(PredictorPredictContext ctx, List <PredictDictionary> inputs, DeviceDescriptor device)
        {
            using (HeavyProfiler.Log("GetValueForPredict", () => $"Inputs {inputs.Count} Codifications {ctx.InputCodifications.Count}"))
            {
                if (inputs.First().SubQueries.Values.Any(a => a.SubQueryGroups.Comparer != ObjectArrayComparer.Instance))
                {
                    throw new Exception("Unexpected dictionary comparer");
                }

                float[] inputValues = new float[inputs.Count * ctx.InputCodifications.Count];
                var     groups      = ctx.InputCodificationsByColumn;
                for (int i = 0; i < inputs.Count; i++)
                {
                    PredictDictionary input = inputs[i];
                    int offset = i * ctx.InputCodifications.Count;

                    foreach (var kvp in groups)
                    {
                        PredictorColumnBase col = kvp.Key;
                        object?value;
                        if (col is PredictorColumnMain pcm)
                        {
                            value = input.MainQueryValues.GetOrThrow(pcm.PredictorColumn);
                        }
                        else if (col is PredictorColumnSubQuery pcsq)
                        {
                            var sq = input.SubQueries.GetOrThrow(pcsq.SubQuery);

                            var dic = sq.SubQueryGroups.TryGetC(pcsq.Keys);

                            value = dic == null ? null : dic.GetOrThrow(pcsq.PredictorSubQueryColumn);
                        }
                        else
                        {
                            throw new UnexpectedValueException(col);
                        }

                        using (HeavyProfiler.LogNoStackTrace("EncodeValue"))
                        {
                            var enc = Encodings.GetOrThrow(col.Encoding);
                            enc.EncodeValue(value ?? CNTKDefault.GetDefaultValue(kvp.Value.FirstOrDefault()), col, kvp.Value, inputValues, offset);
                        }
                    }
                }

                using (HeavyProfiler.LogNoStackTrace("CreateBatch"))
                    return(Value.CreateBatch <float>(new int[] { ctx.InputCodifications.Count }, inputValues, device));
            }
        }
Example #5
0
    public PredictRequestTS GetPredict(string predictorId, [Required, FromBody] Dictionary <string, object?> mainKeys)
    {
        var p = Lite.ParsePrimaryKey <PredictorEntity>(predictorId);

        PredictorPredictContext pctx = PredictorPredictLogic.GetPredictContext(p);

        PredictDictionary?fromEntity      = mainKeys == null ? null : pctx.GetInputsFromParentKeys(pctx.ParseMainKeys(mainKeys));
        PredictDictionary inputs          = fromEntity ?? pctx.GetInputsEmpty();
        PredictDictionary?originalOutputs = fromEntity;

        PredictDictionary predictedOutputs = inputs.PredictBasic();

        PredictRequestTS pmodel = pctx.CreatePredictModel(inputs, originalOutputs, predictedOutputs);

        return(pmodel);
    }
Example #6
0
        private NDArray GetValueForPredict(PredictorPredictContext ctx, PredictDictionary input)
        {
            using (HeavyProfiler.Log("GetValueForPredict", () => $"Inputs Codifications {ctx.InputCodifications.Count}"))
            {
                if (input.SubQueries.Values.Any(a => a.SubQueryGroups.Comparer != ObjectArrayComparer.Instance))
                {
                    throw new Exception("Unexpected dictionary comparer");
                }

                float[] inputValues = new float[ctx.InputCodifications.Count];
                var     groups      = ctx.InputCodificationsByColumn;

                foreach (var kvp in groups)
                {
                    PredictorColumnBase col = kvp.Key;
                    object?value;
                    if (col is PredictorColumnMain pcm)
                    {
                        value = input.MainQueryValues.GetOrThrow(pcm.PredictorColumn);
                    }
                    else if (col is PredictorColumnSubQuery pcsq)
                    {
                        var sq = input.SubQueries.GetOrThrow(pcsq.SubQuery);

                        var dic = sq.SubQueryGroups.TryGetC(pcsq.Keys);

                        value = dic == null ? null : dic.GetOrThrow(pcsq.PredictorSubQueryColumn);
                    }
                    else
                    {
                        throw new UnexpectedValueException(col);
                    }

                    using (HeavyProfiler.LogNoStackTrace("EncodeValue"))
                    {
                        var enc = Encodings.GetOrThrow(col.Encoding);
                        enc.EncodeValue(value ?? TensorFlowDefault.GetDefaultValue(kvp.Value.FirstEx()), col, kvp.Value, inputValues, 0);
                    }
                }

                using (HeavyProfiler.LogNoStackTrace("CreateBatch"))
                    return(np.array(inputValues).reshape(-1, inputValues.Length));
            }
        }
Example #7
0
        public PredictDictionary Predict(PredictorPredictContext ctx, PredictDictionary input)
        {
            using (HeavyProfiler.LogNoStackTrace("Predict"))
            {
                lock (lockKey)
                {
                    var model = (TensorFlowModel)ctx.Model !;

                    model.Session.as_default();
                    model.Graph.as_default();

                    NDArray           inputValue     = GetValueForPredict(ctx, input);
                    NDArray           outputValuesND = model.Session.run(model.CalculatedOutput, (model.InputPlaceholder, inputValue));
                    float[]           outputValues   = outputValuesND.ToArray <float>();
                    PredictDictionary dic            = GetPredictionDictionary(outputValues, ctx, input.Options !);
                    return(dic);
                }
            }
        }
Example #8
0
    public PredictRequestTS UpdatePredict([Required, FromBody] PredictRequestTS request)
    {
        PredictorPredictContext pctx = PredictorPredictLogic.GetPredictContext(request.predictor);

        PredictDictionary inputs = pctx.GetInputsFromRequest(request);

        if (request.alternativesCount != null)
        {
            inputs.Options = new PredictionOptions {
                AlternativeCount = request.alternativesCount
            }
        }
        ;

        PredictDictionary predictedOutputs = inputs.PredictBasic();

        request.SetOutput(predictedOutputs);

        return(request);
    }
Example #9
0
        public decimal?SalesEstimation(Lite <ProductEntity> product)
        {
            var ctx  = PredictorPredictLogic.GetCurrentPredictor(ProductPredictorPublication.MonthlySales).GetPredictContext();
            var pred = ctx.Predictor;

            var input = new PredictDictionary(pred)
            {
                MainQueryValues =
                {
                    { pred.MainQuery.FindColumn(nameof(DateTime.Year)),               DateTime.Now.Year  },
                    { pred.MainQuery.FindColumn(nameof(DateTime.Month)),              DateTime.Now.Month },
                    { pred.MainQuery.FindColumn(nameof(OrderDetailEmbedded.Product)), product            },
                }
            };

            var output = input.PredictBasic();

            var obj = output.MainQueryValues.GetOrThrow(pred.MainQuery.FindColumn(nameof(OrderDetailEmbedded.Quantity)));

            return(Convert.ToDecimal(obj));
        }
Example #10
0
    public void SavePredictions(PredictorTrainingContext ctx)
    {
        using (HeavyProfiler.Log("SavePredictions"))
        {
            var p             = ctx.Predictor.ToLite();
            var outputColumn  = AssertOnlyOutput(ctx.Predictor);
            var isCategorical = outputColumn.Encoding.Is(DefaultColumnEncodings.OneHot);

            var keys = !ctx.Predictor.MainQuery.GroupResults ? null : ctx.Predictor.MainQuery.Columns.Where(c => !(c.Token.Token is AggregateToken)).ToList();
            var key0 = keys?.ElementAtOrDefault(0);
            var key1 = keys?.ElementAtOrDefault(1);
            var key2 = keys?.ElementAtOrDefault(2);

            using (HeavyProfiler.Log("Delete Old Predictions"))
            {
                ctx.ReportProgress($"Deleting old {typeof(PredictSimpleResultEntity).NicePluralName()}");
                {
                    var query      = Database.Query <PredictSimpleResultEntity>().Where(a => a.Predictor.Is(p));
                    int chunkSize  = 5000;
                    var totalCount = query.Count();
                    var deleted    = 0;
                    while (totalCount - deleted > 0)
                    {
                        int num = query.OrderBy(a => a.Id).Take(chunkSize).UnsafeDelete();
                        deleted += num;
                        ctx.ReportProgress($"Deleting old {typeof(PredictSimpleResultEntity).NicePluralName()}", deleted / (decimal)totalCount);
                    }
                }
            }

            using (HeavyProfiler.Log("SavePredictions"))
            {
                ctx.ReportProgress($"Creating {typeof(PredictSimpleResultEntity).NicePluralName()}");
                {
                    var dictionary = ctx.ToPredictDictionaries();
                    var toInsert   = new List <PredictSimpleResultEntity>();

                    var pc      = PredictorPredictLogic.CreatePredictContext(ctx.Predictor);
                    int grIndex = 0;
                    foreach (var gr in dictionary.Chunk(PredictionBatchSize))
                    {
                        using (HeavyProfiler.LogNoStackTrace("Group"))
                        {
                            ctx.ReportProgress($"Creating {typeof(PredictSimpleResultEntity).NicePluralName()}", (grIndex++ *PredictionBatchSize) / (decimal)dictionary.Count);

                            var inputs = gr.Select(a => a.Value).ToList();

                            var outputs = pc.Algorithm.PredictMultiple(pc, inputs);

                            using (HeavyProfiler.LogNoStackTrace("Create SimpleResults"))
                            {
                                for (int i = 0; i < inputs.Count; i++)
                                {
                                    PredictDictionary input  = inputs[i];
                                    PredictDictionary output = outputs[i];

                                    object?inValue  = input.MainQueryValues.GetOrThrow(outputColumn);
                                    object?outValue = output.MainQueryValues.GetOrThrow(outputColumn);

                                    toInsert.Add(new PredictSimpleResultEntity
                                    {
                                        Predictor         = p,
                                        Target            = ctx.Predictor.MainQuery.GroupResults ? null : input.Entity,
                                        Type              = ctx.Validation.Contains(gr[i].Key) ? PredictionSet.Validation : PredictionSet.Training,
                                        Key0              = key0 == null ? null : input.MainQueryValues.GetOrThrow(key0)?.ToString(),
                                        Key1              = key1 == null ? null : input.MainQueryValues.GetOrThrow(key1)?.ToString(),
                                        Key2              = key2 == null ? null : input.MainQueryValues.GetOrThrow(key2)?.ToString(),
                                        OriginalValue     = isCategorical ? null : ReflectionTools.ChangeType <double?>(inValue),
                                        OriginalCategory  = isCategorical ? inValue?.ToString() : null,
                                        PredictedValue    = isCategorical ? null : ReflectionTools.ChangeType <double?>(outValue),
                                        PredictedCategory = isCategorical ? outValue?.ToString() : null,
                                    });
                                }
                            }
                        }
                    }

                    ctx.Predictor.RegressionTraining       = isCategorical ? null : GetRegressionStats(toInsert.Where(a => a.Type == PredictionSet.Training).ToList());
                    ctx.Predictor.RegressionValidation     = isCategorical ? null : GetRegressionStats(toInsert.Where(a => a.Type == PredictionSet.Validation).ToList());
                    ctx.Predictor.ClassificationTraining   = !isCategorical ? null : GetClassificationStats(toInsert.Where(a => a.Type == PredictionSet.Training).ToList());
                    ctx.Predictor.ClassificationValidation = !isCategorical ? null : GetClassificationStats(toInsert.Where(a => a.Type == PredictionSet.Validation).ToList());

                    using (OperationLogic.AllowSave <PredictorEntity>())
                        ctx.Predictor.Save();

                    if (SaveAllResults)
                    {
                        var groups = toInsert.Chunk(PredictionBatchSize).ToList();
                        foreach (var iter in groups.Iterate())
                        {
                            ctx.ReportProgress($"Inserting {typeof(PredictSimpleResultEntity).NicePluralName()}", iter.Position / (decimal)groups.Count);
                            iter.Value.BulkInsert();
                        }
                    }
                }
            }
        }
    }
 public PredictDictionary Predict(PredictorPredictContext ctx, PredictDictionary input)
 {
     return(PredictMultiple(ctx, new List <PredictDictionary> {
         input
     }).SingleEx());
 }