コード例 #1
0
        public static (IDictionary <string, IEnumerable <float> > weights, IDictionary <string, float> biases) GetModelParameters(TransformerChain <MulticlassPredictionTransformer <LinearMulticlassModelParameters> > modelForContributions, string[] categories)
        {
            var modelParameters = modelForContributions.Last() as MulticlassPredictionTransformer <LinearMulticlassModelParameters>;

            VBuffer <float>[] weights = default;
            modelParameters.Model.GetWeights(ref weights, out int _);

            var weightsDictionary = new Dictionary <string, IEnumerable <float> >();
            var i = 0;

            foreach (var weight in weights)
            {
                weightsDictionary.Add(categories[i++], (weight as VBuffer <float>?).Value.DenseValues());
            }

            var biases           = modelParameters.Model.GetBiases();
            var biasesDictionary = new Dictionary <string, float>();

            i = 0;
            foreach (var bias in biases)
            {
                biasesDictionary.Add(categories[i++], bias);
            }

            return(weightsDictionary, biasesDictionary);
        }