Exemplo n.º 1
0
        /**
         * init parameter for continue estimating or for later inference
         */
        public bool initEstimatedModel(LDACommandLineOptions option)
        {
            if (!init(option))
            {
                return(false);
            }

            int m, n, w;

            p = new double[K];

            // load model, i.e., read z and trndata
            if (!loadModel())
            {
                Console.WriteLine("Fail to load word-topic assignment file of the model!\n");
                return(false);
            }

            Console.WriteLine("Model loaded:");
            Console.WriteLine("\talpha:" + alpha);
            Console.WriteLine("\tbeta:" + beta);
            Console.WriteLine("\tM:" + M);
            Console.WriteLine("\tV:" + V);


            nw = ArrayInitializers.ZerosInt(V, K);
            nd = ArrayInitializers.ZerosInt(M, K);

            nwsum = ArrayInitializers.ZerosInt(K);
            ndsum = ArrayInitializers.ZerosInt(M);

            for (m = 0; m < data.M; m++)
            {
                int N = data.Docs[m].Length;

                // assign values for nw, nd, nwsum, and ndsum
                for (n = 0; n < N; n++)
                {
                    w = data.Docs[m].Words[n];
                    int topic = z[m][n];

                    // number of instances of word i assigned to topic j
                    nw[w][topic] += 1;
                    // number of words in document i assigned to topic j
                    nd[m][topic] += 1;
                    // total number of words assigned to topic j
                    nwsum[topic] += 1;
                }
                // total number of words in document i
                ndsum[m] = N;
            }


            theta    = ArrayInitializers.Empty(M, K);
            phi      = ArrayInitializers.Empty(K, V);
            dir      = option.dir;
            savestep = option.savestep;

            return(true);
        }
Exemplo n.º 2
0
        /**
         * Init parameters for inference
         * @param newData DataSet for which we do inference
         */
        public bool initNewModel(LDACommandLineOptions option, LDADataset newData, Model trnModel)
        {
            if (!init(option))
            {
                return(false);
            }

            int m, n;

            var rnd = new Random();

            K     = trnModel.K;
            alpha = trnModel.alpha;
            beta  = trnModel.beta;

            p = new double[K];
            Console.WriteLine("K:" + K);

            data = newData;

            //+ allocate memory and assign values for variables
            M        = data.M;
            V        = data.V;
            dir      = option.dir;
            savestep = option.savestep;
            Console.WriteLine("M:" + M);
            Console.WriteLine("V:" + V);

            // K: from command line or default value
            // alpha, beta: from command line or default values
            // niters, savestep: from command line or default values

            nw = ArrayInitializers.ZerosInt(V, K);
            nd = ArrayInitializers.ZerosInt(M, K);

            nwsum = ArrayInitializers.ZerosInt(K);
            ndsum = ArrayInitializers.ZerosInt(M);

            z = new List <int> [M];
            for (m = 0; m < data.M; m++)
            {
                int N = data.Docs[m].Length;
                z[m] = new List <int>();

                //initilize for z
                for (n = 0; n < N; n++)
                {
                    int topic = (int)Math.Floor(rnd.NextDouble() * K);
                    z[m].Add(topic);

                    // number of instances of word assigned to topic j
                    nw[data.Docs[m].Words[n]][topic] += 1;
                    // number of words in document i assigned to topic j
                    nd[m][topic] += 1;
                    // total number of words assigned to topic j
                    nwsum[topic] += 1;
                }
                // total number of words in document i
                ndsum[m] = N;
            }

            theta = ArrayInitializers.Empty(M, K);
            phi   = ArrayInitializers.Empty(K, V);

            return(true);
        }