Exemplo n.º 1
0
        public int[] LearnFromOneInstance(int[][] wordsWithActiveFeatures, int[] labelledTags)
        {
            // averaged percetron code : http://ciml.info/dl/v0_8/ciml-v0_8-ch03.pdf

            int numWords = wordsWithActiveFeatures.Length;

            Viterbi.Decode(
                wordsWithActiveFeatures,
                w_alphaTagFeature,
                w_alphaTagPreviousTag,
                latticeBuffer,
                backPointerBuffer,
                numTags,
                decodedTagsBuffer);

            //find mistakes
            int diff = 0;

            for (int t = 0; t < numWords; t++)
            {
                if (decodedTagsBuffer[t] != labelledTags[t])
                {
                    diff++;
                }
            }

            loss += diff * 1.0 / numWords;
            instances++;

            if (c % showFactor == 0)
            {
                showFactor *= 2;
                Console.WriteLine("Average loss {0} = {1}", c, loss / instances);
                instances = 0;
                loss      = 0;
            }

            //do perceptron update
            if (diff > 0)
            {
                for (int t = 0; t < numWords; t++)
                {
                    int dt = decodedTagsBuffer[t]; //decoded tag
                    int lt = labelledTags[t];      //labelled tag
                    if (decodedTagsBuffer[t] != labelledTags[t])
                    {
                        //update W
                        foreach (int f in wordsWithActiveFeatures[t])
                        {
                            w_alphaTagFeature[dt, f] -= 1;
                            c_alphaTagFeature[dt, f] -= c;

                            w_alphaTagFeature[lt, f] += 1;
                            c_alphaTagFeature[lt, f] += c;
                        }
                    }

                    if (t > 0)
                    {
                        int dpt = decodedTagsBuffer[t - 1]; //decoded previous tag
                        int lpt = labelledTags[t - 1];      //labelled previous tag

                        if (dt != lt || dpt != lpt)
                        {
                            w_alphaTagPreviousTag[dt, dpt] -= 1;
                            c_alphaTagPreviousTag[dt, dpt] -= c;

                            w_alphaTagPreviousTag[lt, lpt] += 1;
                            c_alphaTagPreviousTag[lt, lpt] += c;
                        }
                    }
                }
                c += 1;
            }

            return(decodedTagsBuffer);
        }
Exemplo n.º 2
0
 public void Label(int[][] wordsWithActiveFeatures, int[] tags)
 {
     Viterbi.Decode(wordsWithActiveFeatures, alphaTagFeature, alphaTagPreviousTag, latticeBuffer, backPointerBuffer, numTags, tags);
 }