Beispiel #1
0
        public Model inference(string[] strs)
        {
            //System.out.println("inference");
            Model newModel = new Model();

            //System.out.println("read dataset");
            LDADataset dataset = LDADataset.ReadDataset(strs, globalDict);

            return(inference(dataset));
        }
Beispiel #2
0
        /**
         * Init parameters for inference
         * reading new dataset from file
         */
        public bool initNewModel(LDACommandLineOptions option, Model trnModel)
        {
            if (!init(option))
            {
                return(false);
            }

            LDADataset dataset = LDADataset.ReadDataset(dir + "\\" + dfile, trnModel.data.LocalDictionary);

            if (dataset == null)
            {
                Console.WriteLine("Fail to read dataset!\n");
                return(false);
            }

            return(initNewModel(option, dataset, trnModel));
        }
Beispiel #3
0
        /**
         * Init parameters for estimation
         */
        public bool initNewModel(LDACommandLineOptions option)
        {
            //if (!init(option))
            //return false;
            var rnd = new Random();
            int m, n, w, k;

            p = new double[K];

            data = LDADataset.ReadDataset(dir + "\\" + dfile);
            if (data == null)
            {
                Console.WriteLine("Fail to read training data!\n");
                return(false);
            }

            //+ allocate memory and assign values for variables
            M        = data.M;
            V        = data.V;
            dir      = option.dir;
            savestep = option.savestep;

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

            nw = new int[V][];
            for (w = 0; w < V; w++)
            {
                nw[w] = new int[K];
                for (k = 0; k < K; k++)
                {
                    nw[w][k] = 0;
                }
            }

            nd = new int[M][];
            for (m = 0; m < M; m++)
            {
                nd[m] = new int[K];
                for (k = 0; k < K; k++)
                {
                    nd[m][k] = 0;
                }
            }

            nwsum = new int[K];
            for (k = 0; k < K; k++)
            {
                nwsum[k] = 0;
            }

            ndsum = new int[M];
            for (m = 0; m < M; m++)
            {
                ndsum[m] = 0;
            }

            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 = new double[M][];
            for (m = 0; m < M; m++)
            {
                theta[m] = new double[K];
            }
            phi = new double[K][];
            for (k = 0; k < K; k++)
            {
                phi[k] = new double[V];
            }

            return(true);
        }