• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 195,  comments - 30,  trackbacks - 0
            At first, let's define the sample function:

            def sample(dist, num_samples=1):
                """
                Uses the inverse CDF method to return samples drawn from an
                (unnormalized) discrete distribution.

                Arguments:

                dist -- (unnormalized) distribution

                Keyword arguments:

                num_samples -- number of samples to draw
                
            """

                cdf = cumsum(dist)
                r = uniform(size=num_samples) * cdf[-1]

                return cdf.searchsorted(r)
            As we can see, the sample function input two parameters, one is dist, which can be an un-normalized distribution, another is the sample we want to draw.

            Let's see how to generate corpus for Dirichlet--multinomial unigram language model
            def generate_corpus(beta, mean, N):
                """
                Returns a corpus of tokens drawn from a Dirichlet--multinomial
                unigram language model. Each token is an instance of one of V
                unique word types, represented by indices 0, , V - 1.

                Arguments:

                beta -- concentration parameter for the Dirichlet prior
                mean -- V-dimensional mean of the Dirichlet prior
                N -- number of tokens to generate
                
            """

                pass # YOUR CODE GOES HERE
                #print mean
                #print beta 
                #print dot(mean,beta)
                #print dirichlet(mean*beta,size=1)
                temp=sample(dirichlet(beta*array(mean),size=1),N)
                #print temp
                return temp
            please keep in mind the dirichlet function is  “from numpy.random.mtrand import dirichlet"
            and the parameters it receives are corresponding to beta*array(mean). beta is the concentration factor, and mean is the vector which sum to 1.



            another way is to generate corpus is using the property:
            P(D'|D,H)= Nv+beta_nv/N+beta
            def generate_corpus_collapsed(beta, mean, N):
                """
                Returns a corpus of tokens drawn from a Dirichlet--multinomial
                unigram language model using the 'collapsed' generative process
                (i.e., phi is not explicitly represented). Each token is an
                instance of one of V unique word types.

                Arguments:

                beta -- concentration parameter for the Dirichlet prior
                mean -- V-dimensional mean of the Dirichlet prior
                N -- number of tokens to generate
                
            """

                V = len(mean) # vocabulary size

                corpus = zeros(N, dtype=int) # corpus

                Nv = zeros(V, dtype=int) # counts for each word type

                pass # YOUR CODE GOES HERE
                for n in xrange(N):
                    corpus[n]=sample((Nv+beta*array(mean))/(n+beta),1)
                    Nv[corpus[n]]+=1;    
                return corpus

            Let's see how to generate corpus for Mixture of Dirichlet-multinomial unigram language model 

            def generate_corpus(alpha, m, beta, n, D, Nd):
                """
                Returns a grouped corpus drawn from a mixture of
                Dirichlet--multinomial unigram language models.

                Arguments:

                alpha -- concentration parameter for the Dirichlet prior over theta
                m -- T-dimensional mean of the Dirichlet prior over theta
                beta -- concentration parameter for the Dirichlet prior over phis
                n -- V-dimensional mean of the Dirichlet prior over phis
                D -- number of documents to generate
                Nd -- number of tokens to generate per document
                
            """
                corpus = GroupedCorpus()

                pass # YOUR CODE GOES HERE
                #determine the topic the distribution for topic dirichlet(dot(m,alpha),size=1)
                #given the topic, the distribtuion for word dirichlet(dot(n,beta),size=1)
                theta=dirichlet(alpha*array(m),1)
                phis=dirichlet(beta*array(n),len(m))
                for d in range(0,D):
                    [t]=sample(theta,1)
                    #print groupVcab
                    corpus.add(str(d),str(t),[str(x) for x in sample(phis[t,:],Nd)])           
                return corpus
            注意是T個(gè)topic (group),  phis=dirichlet(beta*array(n),len(m))  產(chǎn)生了T個(gè) dirichlet distribution,相同的topic t應(yīng)該取同一個(gè) dirichlet distribution phis[t,:]
            posted on 2012-10-28 10:13 luis 閱讀(624) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): Python
            <2012年10月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            留言簿(3)

            隨筆分類(lèi)

            隨筆檔案

            文章分類(lèi)

            文章檔案

            友情鏈接

            搜索

            •  

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            性高湖久久久久久久久| 久久免费视频6| 久久99国产综合精品| 天天爽天天爽天天片a久久网| 四虎国产永久免费久久| 亚洲国产香蕉人人爽成AV片久久| 亚洲中文字幕无码久久精品1| 青青草原综合久久大伊人| 欧美丰满熟妇BBB久久久| 久久精品免费一区二区三区| 亚洲欧美另类日本久久国产真实乱对白| 色偷偷91久久综合噜噜噜噜| 久久婷婷激情综合色综合俺也去 | 久久AV高潮AV无码AV| 久久无码人妻一区二区三区| 欧美久久久久久午夜精品| 久久99精品久久久久久动态图| 久久久免费观成人影院| 久久99精品国产99久久| 国产69精品久久久久久人妻精品| 色综合久久久久网| 久久久久无码精品国产| 精品久久人人妻人人做精品 | 精品水蜜桃久久久久久久| 18禁黄久久久AAA片| 国内精品欧美久久精品| 成人久久综合网| 色婷婷综合久久久久中文 | 伊人久久大香线蕉成人| 久久精品一区二区| 久久九九精品99国产精品| 久久久久久久久久久| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久99精品国产麻豆蜜芽| 精品国际久久久久999波多野| 狠狠精品久久久无码中文字幕| 久久露脸国产精品| 久久这里有精品视频| 欧美与黑人午夜性猛交久久久 | 亚洲国产精品18久久久久久| 欧美日韩久久中文字幕|