Exemple #1
0
    public static PredictDictionary GetInputsFromParentKeys(this PredictorPredictContext ctx, Dictionary <QueryToken, object?> parentKeyValues, PredictionOptions?options = null)
    {
        if (!ctx.Predictor.MainQuery.GroupResults)
        {
            var kvp = parentKeyValues.SingleEx();

            if (kvp.Key.FullKey() != "Entity")
            {
                throw new InvalidOperationException("only Entity expected");
            }

            var filters = new List <Filter> {
                new FilterCondition(kvp.Key, FilterOperation.EqualTo, kvp.Value)
            };

            return(ctx.FromFilters(filters, options).SingleEx());
        }
        else
        {
            var filters = ctx.Predictor.MainQuery.Columns
                          .Select(a => a.Token.Token)
                          .Where(t => !(t is AggregateToken))
                          .Select(t => (Filter) new FilterCondition(t, FilterOperation.EqualTo, parentKeyValues.GetOrThrow(t)))
                          .ToList();

            return(ctx.FromFilters(filters, options).SingleEx());
        }
    }
        public List <PredictDictionary> PredictMultiple(PredictorPredictContext ctx, List <PredictDictionary> inputs)
        {
            using (HeavyProfiler.LogNoStackTrace("PredictMultiple"))
            {
                var nnSettings = (NeuralNetworkSettingsEntity)ctx.Predictor.AlgorithmSettings;
                lock (lockKey) //https://docs.microsoft.com/en-us/cognitive-toolkit/cntk-library-evaluation-on-windows#evaluation-of-multiple-requests-in-parallel
                {
                    Function calculatedOutputs = (Function)ctx.Model !;
                    var      device            = GetDevice(nnSettings);
                    Value    inputValue        = GetValueForPredict(ctx, inputs, device);

                    var inputVar = calculatedOutputs.Inputs.SingleEx(i => i.Name == "input");
                    var inputDic = new Dictionary <Variable, Value> {
                        { inputVar, inputValue }
                    };
                    var outputDic = new Dictionary <Variable, Value> {
                        { calculatedOutputs, null ! }
                    };

                    calculatedOutputs.Evaluate(inputDic, outputDic, device);

                    Value output = outputDic[calculatedOutputs];
                    IList <IList <float> > values = output.GetDenseData <float>(calculatedOutputs);
                    var result = values.Select((val, i) => GetPredictionDictionary(val.ToArray(), ctx, inputs[i].Options !)).ToList();
                    return(result);
                }
            }
        }
Exemple #3
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);
                }
            }
        }
        public static PredictDictionary GetInputsFromRequest(this PredictorPredictContext pctx, PredictRequestTS request)
        {
            ParseValues(request, pctx);

            return(new PredictDictionary(pctx.Predictor, null, null)
            {
                MainQueryValues = pctx.Predictor.MainQuery.Columns
                                  .Select((col, i) => new { col, request.columns[i].value })
                                  .Where(a => a.col !.Usage == PredictorColumnUsage.Input)
                                  .Select(a => KeyValuePair.Create(a.col !, (object?)a.value))
                                  .ToDictionaryEx(),

                SubQueries = pctx.Predictor.SubQueries.Select(sq =>
                {
                    var sqt = request.subQueries.Single(a => a.subQuery.Is(sq));
                    SplitColumns(sq, out var splitKeys, out var values);

                    return new PredictSubQueryDictionary(sq)
                    {
                        SubQueryGroups = sqt.rows.Select(array => KeyValuePair.Create(
                                                             array.Slice(0, splitKeys.Count),
                                                             values.Select((a, i) => KeyValuePair.Create(a, array[splitKeys.Count + i])).ToDictionary()
                                                             )).ToDictionary(ObjectArrayComparer.Instance)
                    };
                }).ToDictionaryEx(a => a.SubQuery)
            });
Exemple #5
0
    public static PredictorPredictContext CreatePredictContext(this PredictorEntity p)
    {
        var codifications = p.RetrievePredictorCodifications();
        var ppc           = new PredictorPredictContext(p, PredictorLogic.Algorithms.GetOrThrow(p.Algorithm), codifications);

        ppc.Algorithm.LoadModel(ppc);
        return(ppc);
    }
Exemple #6
0
    public static PredictDictionary GetInputsFromEntity(this PredictorPredictContext ctx, Lite <Entity> entity, PredictionOptions?options = null)
    {
        var qd = QueryLogic.Queries.QueryDescription(ctx.Predictor.MainQuery.Query.ToQueryName());

        var entityToken = QueryUtils.Parse("Entity", qd, 0);

        return(ctx.FromFilters(new List <Filter> {
            new FilterCondition(entityToken, FilterOperation.EqualTo, entity)
        }, options).SingleEx());
    }
        public void LoadModel(PredictorPredictContext ctx)
        {
            lock (lockKey)
            {
                this.InitialSetup();

                var nnSettings = (NeuralNetworkSettingsEntity)ctx.Predictor.AlgorithmSettings;

                ctx.Model = Function.Load(ctx.Predictor.Files.SingleEx().GetByteArray(), GetDevice(nnSettings));
            }
        }
Exemple #8
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);
    }
Exemple #9
0
        public void LoadModel(PredictorPredictContext ctx)
        {
            lock (lockKey)
            {
                this.InitialSetup();

                var nnSettings = (NeuralNetworkSettingsEntity)ctx.Predictor.AlgorithmSettings;

                var dir = Path.Combine(PredictorDirectory(ctx.Predictor));

                if (Directory.Exists(dir))
                {
                    Directory.Delete(dir, true);
                }

                Directory.CreateDirectory(dir);

                foreach (var item in ctx.Predictor.Files)
                {
                    using (var fileStream = File.Create(Path.Combine(dir, item.FileName)))
                    {
                        using (var readStream = item.OpenRead())
                        {
                            readStream.CopyTo(fileStream);
                        }
                    }
                }

                var graph = tf.Graph();
                var sess  = tf.Session(graph);
                {
                    var saver = tf.train.import_meta_graph(Path.Combine(dir, $"{ModelFileName}.meta"));
                    saver.restore(sess, Path.Combine(dir, ModelFileName));

                    var array = graph.get_operations().Select(a => a.name).ToArray();

                    var inputPlaceholder = graph.get_operation_by_name("inputPlaceholder");
                    var calculatedOutput = graph.get_operation_by_name("calculatedOutput");

                    ctx.Model = new TensorFlowModel
                    {
                        InputPlaceholder = inputPlaceholder,
                        CalculatedOutput = calculatedOutput,
                        Graph            = graph,
                        Session          = sess,
                    };
                }

                //ctx.Model = Function.Load(ctx.Predictor.Files.SingleEx().GetByteArray());
            }
        }
        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));
            }
        }
Exemple #11
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);
    }
        public static Dictionary <QueryToken, object?> ParseMainKeys(this PredictorPredictContext pctx, Dictionary <string, object?> mainKeys)
        {
            Dictionary <QueryToken, object?> filters = new Dictionary <QueryToken, object?>();

            var options = SignumServer.JsonSerializerOptions;
            var qd      = QueryLogic.Queries.QueryDescription(pctx.Predictor.MainQuery.Query.ToQueryName());

            foreach (var kvp in mainKeys)
            {
                var qt = QueryUtils.Parse(kvp.Key, qd, SubTokensOptions.CanElement | SubTokensOptions.CanAggregate);

                var obj = kvp.Value is JsonElement jt?jt.ToObject(qt.Type, options) : ReflectionTools.ChangeType(kvp.Value, qt.Type);

                filters.Add(qt, obj);
            }

            return(filters);
        }
Exemple #13
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));
            }
        }
Exemple #14
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);
                }
            }
        }
Exemple #15
0
    public static PredictorPredictContext GetPredictContext(this Lite <PredictorEntity> predictor)
    {
        lock (TrainedPredictorCache)
            return(TrainedPredictorCache.GetOrCreate(predictor, () =>
            {
                using (ExecutionMode.Global())
                    using (var t = Transaction.ForceNew())
                    {
                        var p = predictor.RetrieveAndRemember();
                        if (p.State != PredictorState.Trained)
                        {
                            throw new InvalidOperationException($"Predictor '{p.Name}' not trained");
                        }

                        PredictorPredictContext ppc = CreatePredictContext(p);
                        return t.Commit(ppc);
                    }
            }));
    }
Exemple #16
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);
    }
Exemple #17
0
        private PredictDictionary GetPredictionDictionary(float[] outputValues, PredictorPredictContext ctx, PredictionOptions options)
        {
            using (HeavyProfiler.LogNoStackTrace("GetPredictionDictionary"))
            {
                return(new PredictDictionary(ctx.Predictor, options, null)
                {
                    MainQueryValues = ctx.MainOutputCodifications.SelectDictionary(col => col,
                                                                                   (col, list) => Encodings.GetOrThrow(col.Encoding).DecodeValue(list.First().Column, list, outputValues, options)),

                    SubQueries = ctx.Predictor.SubQueries.ToDictionary(sq => sq, sq => new PredictSubQueryDictionary(sq)
                    {
                        SubQueryGroups = ctx.SubQueryOutputCodifications.TryGetC(sq)?.Groups.ToDictionary(
                            kvp => kvp.Key,
                            kvp => kvp.Value
                            .Where(a => a.Key.Usage == PredictorSubQueryColumnUsage.Output)
                            .ToDictionary(a => a.Key, a => Encodings.GetOrThrow(a.Key.Encoding).DecodeValue(a.Value.FirstEx().Column, a.Value, outputValues, options)),
                            ObjectArrayComparer.Instance
                            ) ?? new Dictionary <object?[], Dictionary <PredictorSubQueryColumnEmbedded, object?> >(ObjectArrayComparer.Instance),
                    })
                });
            }
        }
 public PredictDictionary Predict(PredictorPredictContext ctx, PredictDictionary input)
 {
     return(PredictMultiple(ctx, new List <PredictDictionary> {
         input
     }).SingleEx());
 }
Exemple #19
0
    public static List <PredictDictionary> FromFilters(this PredictorPredictContext ctx, List <Filter> filters, PredictionOptions?options = null)
    {
        var qd = QueryLogic.Queries.QueryDescription(ctx.Predictor.MainQuery.Query.ToQueryName());

        var qr = new QueryRequest
        {
            QueryName = qd.QueryName,

            GroupResults = ctx.Predictor.MainQuery.GroupResults,

            Filters = filters, /*Filters of Main Query not considered*/

            Columns = ctx.Predictor.MainQuery.Columns.Select(c => new Column(c.Token.Token, null)).ToList(),

            Pagination = new Pagination.All(),
            Orders     = Enumerable.Empty <Order>().ToList(),
        };

        var mainQueryKeys = PredictorLogic.GetParentKeys(ctx.Predictor.MainQuery);

        var rt = QueryLogic.Queries.ExecuteQuery(qr);

        var subQueryResults = ctx.Predictor.SubQueries.ToDictionaryEx(sq => sq, sqe =>
        {
            List <QueryToken> parentKeys = sqe.Columns.Where(a => a.Usage == PredictorSubQueryColumnUsage.ParentKey).Select(a => a.Token.Token).ToList();

            QueryDescription sqd = QueryLogic.Queries.QueryDescription(sqe.Query.ToQueryName());

            Dictionary <string, string> tokenReplacements = mainQueryKeys.ZipStrict(parentKeys, (m, p) => KeyValuePair.Create(m.FullKey(), p.FullKey())).ToDictionaryEx();

            Filter[] mainFilters = filters.Select(f => Replace(f, tokenReplacements, sqd)).ToArray();

            List <Filter> additionalFilters = sqe.Filters.ToFilterList();

            List <Column> allColumns = sqe.Columns.Select(c => new Column(c.Token.Token, null)).ToList();

            var qgr = new QueryRequest
            {
                QueryName    = sqe.Query.ToQueryName(),
                GroupResults = true,
                Filters      = mainFilters.Concat(additionalFilters).ToList(),
                Columns      = allColumns,
                Orders       = new List <Order>(),
                Pagination   = new Pagination.All(),
            };

            ResultTable resultTable = QueryLogic.Queries.ExecuteQuery(qgr);

            var tuples = sqe.Columns.Zip(resultTable.Columns, (sqc, rc) => (sqc: sqc, rc: rc)).ToList();
            ResultColumn[] entityGroupKey = tuples.Extract(t => t.sqc.Usage == PredictorSubQueryColumnUsage.ParentKey).Select(a => a.rc).ToArray();
            ResultColumn[] remainingKeys  = tuples.Extract(t => t.sqc.Usage == PredictorSubQueryColumnUsage.SplitBy).Select(a => a.rc).ToArray();
            var valuesTuples = tuples;

            return(resultTable.Rows.AgGroupToDictionary(
                       row => row.GetValues(entityGroupKey),
                       gr => gr.ToDictionaryEx(
                           row => row.GetValues(remainingKeys),
                           row => valuesTuples.ToDictionaryEx(t => t.sqc, t => row[t.rc]),
                           ObjectArrayComparer.Instance
                           )
                       ));
        });