Exemplo n.º 1
0
        public List <float> Forward(int [] feaidx, float[] feaval)
        {
            Layer_Output        output    = new Layer_Output(this);
            List <Sample_Input> inputList = new List <Sample_Input>();

            Sample_Input input = new Sample_Input();

            if (TYPE == 0 || TYPE == 2) //DSSM -- bag of word feature
            {
                Dictionary <int, float> fea = new Dictionary <int, float>();

                for (int i = 0; i < feaidx.Length; i++)
                {
                    fea.Add(feaidx[i], feaval[i]);
                }

                input.Load_BOW(fea);
            }
            else if (TYPE == 1 || TYPE == 3) //CDSSM -- seq of word feature
            {
                throw new Exception("cdssm for this kind of feature NOT SUPPORTED yet!");
            }
            inputList.Add(input);

            forward_activate(inputList, output);

            List <float> result = new List <float>();

            for (int i = 0; i < output.layerOutputs[output.Layer_TOP].Length; i++)
            {
                result.Add(output.layerOutputs[output.Layer_TOP][i]);
            }
            return(result);
        }
Exemplo n.º 2
0
        public List <float> Forward(string text, Dictionary <string, Dictionary <int, float> > dic, FeatureList featureList)
        {
            Layer_Output        output    = new Layer_Output(this);
            List <Sample_Input> inputList = new List <Sample_Input>();

            string[] sentenceList = PoolIdx >= 1 ?
                                    text.Split(new char[] { '.', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                : new string[] { text };
            if (sentenceList.Length > MaxPoolSentenceNumber)
            {
                string[] sentenceListTmp = new string[MaxPoolSentenceNumber];
                for (int i = 0; i < MaxPoolSentenceNumber - 1; ++i)
                {
                    sentenceListTmp[i] = sentenceList[i];
                }
                sentenceListTmp[MaxPoolSentenceNumber - 1] = string.Join(" ", sentenceList.Skip(MaxPoolSentenceNumber - 1));
                sentenceList = sentenceListTmp;
            }

            foreach (string sentence in sentenceList)
            {
                Sample_Input input = new Sample_Input();


                if (TYPE == 0 || TYPE == 2) //DSSM -- bag of word feature
                {
                    Dictionary <int, float> fea = new Dictionary <int, float>();

                    int pos = 0;
                    if (featureList.l3g == true)
                    {
                        Dictionary <int, float> tmp = LFE.Feature_Extract_BOW(sentence, pos);     //pos
                        fea  = fea.Concat(tmp).ToDictionary(k => k.Key, v => v.Value);
                        pos += LFE.vocab_dict.Count;
                    }
                    if (featureList.root == true)
                    {
                        int count      = 0;
                        var featStrFeq = TextUtils.String2FeatStrSeq(sentence, 3, 20, FeatureType.root);  // list of root
                        List <Dictionary <int, double> > tmpList = TextUtils.StrFreq2IdFreq(featStrFeq, FeatureType.root, pos, ref count);
                        Dictionary <int, float>          tmp     = TextUtils.MergeList(tmpList).ToDictionary(k => k.Key, v => (float)(v.Value));
                        fea  = fea.Concat(tmp).ToDictionary(k => k.Key, v => v.Value);
                        pos += count;
                    }
                    if (featureList.infl == true)
                    {
                        int count      = 0;
                        var featStrFeq = TextUtils.String2FeatStrSeq(sentence, 3, 20, FeatureType.infl);  // list of inflections
                        List <Dictionary <int, double> > tmpList = TextUtils.StrFreq2IdFreq(featStrFeq, FeatureType.infl, pos, ref count);
                        Dictionary <int, float>          tmp     = TextUtils.MergeList(tmpList).ToDictionary(k => k.Key, v => (float)(v.Value));
                        fea  = fea.Concat(tmp).ToDictionary(k => k.Key, v => v.Value);
                        pos += count;
                    }


                    input.Load_BOW(fea);
                }



                //need to updata
                else if (TYPE == 1 || TYPE == 3) //CDSSM -- seq of word feature
                {
                    List <Dictionary <int, float> > feas = new List <Dictionary <int, float> >();

                    //need to update
                    //if (featureType == FeatureType.we)
                    //{
                    //    feas = LFE.Feature_Extractor_SOW(sentence, dic);
                    //}
                    //else
                    {
                        feas = LFE.Feature_Extractor_SOW(sentence);
                    }

                    input.Load_SOW(feas);
                }



                inputList.Add(input);
            }

            forward_activate(inputList, output);

            List <float> result = new List <float>();

            for (int i = 0; i < output.layerOutputs[output.Layer_TOP].Length; i++)
            {
                result.Add(output.layerOutputs[output.Layer_TOP][i]);
            }
            return(result);
        }