本文整理汇总了Java中cc.mallet.util.Randoms.nextInt方法的典型用法代码示例。如果您正苦于以下问题:Java Randoms.nextInt方法的具体用法?Java Randoms.nextInt怎么用?Java Randoms.nextInt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cc.mallet.util.Randoms
的用法示例。
在下文中一共展示了Randoms.nextInt方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addInstances
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public void addInstances (InstanceList training) {
initializeForTypes (training.getDataAlphabet());
ArrayList<LabelSequence> topicSequences = new ArrayList<LabelSequence>();
for (Instance instance : training) {
LabelSequence topicSequence = new LabelSequence(topicAlphabet, new int[instanceLength(instance)]);
if (false)
// This method not yet obeying its last "false" argument, and must be for this to work
sampleTopicsForOneDoc((FeatureSequence)instance.getData(), topicSequence, false, false);
else {
Randoms r = new Randoms();
int[] topics = topicSequence.getFeatures();
for (int i = 0; i < topics.length; i++)
topics[i] = r.nextInt(numTopics);
}
topicSequences.add (topicSequence);
}
addInstances (training, topicSequences);
}
示例2: sample
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public Assignment sample (Randoms r)
{
int ri = r.nextInt (numRows ());
Object[] vals = (Object[]) values.get (ri);
Assignment assn = new Assignment ();
Variable[] varr = (Variable[]) vars.toArray (new Variable [numVariables ()]);
assn.addRow (varr, vals);
return assn;
}
示例3: createRandomClustering
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public static Clustering createRandomClustering (InstanceList instances,
Randoms random) {
Clustering clustering = createSingletonClustering(instances);
int numMerges = 2 + random.nextInt(instances.size() - 2);
for (int i = 0; i < numMerges; i++)
clustering = mergeInstances(clustering,
random.nextInt(instances.size()),
random.nextInt(instances.size()));
return clustering;
}
示例4: estimate
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public void estimate (InstanceList documents, int numIterations, int showTopicsInterval,
int outputModelInterval, String outputModelFilename,
Randoms r)
{
ilist = documents;
uniAlphabet = ilist.getDataAlphabet();
biAlphabet = ((FeatureSequenceWithBigrams)ilist.get(0).getData()).getBiAlphabet();
numTypes = uniAlphabet.size();
numBitypes = biAlphabet.size();
int numDocs = ilist.size();
topics = new int[numDocs][];
grams = new int[numDocs][];
docTopicCounts = new int[numDocs][numTopics];
typeNgramTopicCounts = new int[numTypes][2][numTopics];
unitypeTopicCounts = new int[numTypes][numTopics];
bitypeTopicCounts = new int[numBitypes][numTopics];
tokensPerTopic = new int[numTopics];
bitokensPerTopic = new int[numTypes][numTopics];
tAlpha = alpha * numTopics;
vBeta = beta * numTypes;
vGamma = gamma * numTypes;
long startTime = System.currentTimeMillis();
// Initialize with random assignments of tokens to topics
// and finish allocating this.topics and this.tokens
int topic, gram, seqLen, fi;
for (int di = 0; di < numDocs; di++) {
FeatureSequenceWithBigrams fs = (FeatureSequenceWithBigrams) ilist.get(di).getData();
seqLen = fs.getLength();
numTokens += seqLen;
topics[di] = new int[seqLen];
grams[di] = new int[seqLen];
// Randomly assign tokens to topics
int prevFi = -1, prevTopic = -1;
for (int si = 0; si < seqLen; si++) {
// randomly sample a topic for the word at position si
topic = r.nextInt(numTopics);
// if a bigram is allowed at position si, then sample a gram status for it.
gram = (fs.getBiIndexAtPosition(si) == -1 ? 0 : r.nextInt(2));
if (gram != 0) biTokens++;
topics[di][si] = topic;
grams[di][si] = gram;
docTopicCounts[di][topic]++;
fi = fs.getIndexAtPosition(si);
if (prevFi != -1)
typeNgramTopicCounts[prevFi][gram][prevTopic]++;
if (gram == 0) {
unitypeTopicCounts[fi][topic]++;
tokensPerTopic[topic]++;
} else {
bitypeTopicCounts[fs.getBiIndexAtPosition(si)][topic]++;
bitokensPerTopic[prevFi][topic]++;
}
prevFi = fi; prevTopic = topic;
}
}
for (int iterations = 0; iterations < numIterations; iterations++) {
sampleTopicsForAllDocs (r);
if (iterations % 10 == 0) System.out.print (iterations); else System.out.print (".");
System.out.flush();
if (showTopicsInterval != 0 && iterations % showTopicsInterval == 0 && iterations > 0) {
System.out.println ();
printTopWords (5, false);
}
if (outputModelInterval != 0 && iterations % outputModelInterval == 0 && iterations > 0) {
this.write (new File(outputModelFilename+'.'+iterations));
}
}
System.out.println ("\nTotal time (sec): " + ((System.currentTimeMillis() - startTime)/1000.0));
}
示例5: estimate
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public void estimate (InstanceList documents, int numIterations, int showTopicsInterval,
int outputModelInterval, String outputModelFilename,
Randoms r)
{
ilist = documents.shallowClone();
numTypes = ilist.getDataAlphabet().size ();
int numDocs = ilist.size();
topics = new int[numDocs][];
docTopicCounts = new int[numDocs][numTopics];
typeTopicCounts = new int[numTypes][numTopics];
tokensPerTopic = new int[numTopics];
tAlpha = alpha * numTopics;
vBeta = beta * numTypes;
long startTime = System.currentTimeMillis();
// Initialize with random assignments of tokens to topics
// and finish allocating this.topics and this.tokens
int topic, seqLen;
FeatureSequence fs;
for (int di = 0; di < numDocs; di++) {
try {
fs = (FeatureSequence) ilist.get(di).getData();
} catch (ClassCastException e) {
System.err.println ("LDA and other topic models expect FeatureSequence data, not FeatureVector data. "
+"With text2vectors, you can obtain such data with --keep-sequence or --keep-bisequence.");
throw e;
}
seqLen = fs.getLength();
numTokens += seqLen;
topics[di] = new int[seqLen];
// Randomly assign tokens to topics
for (int si = 0; si < seqLen; si++) {
topic = r.nextInt(numTopics);
topics[di][si] = topic;
docTopicCounts[di][topic]++;
typeTopicCounts[fs.getIndexAtPosition(si)][topic]++;
tokensPerTopic[topic]++;
}
}
this.estimate(0, numDocs, numIterations, showTopicsInterval, outputModelInterval, outputModelFilename, r);
// 124.5 seconds
// 144.8 seconds after using FeatureSequence instead of tokens[][] array
// 121.6 seconds after putting "final" on FeatureSequence.getIndexAtPosition()
// 106.3 seconds after avoiding array lookup in inner loop with a temporary variable
}
示例6: addDocuments
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public void addDocuments(InstanceList additionalDocuments,
int numIterations, int showTopicsInterval,
int outputModelInterval, String outputModelFilename,
Randoms r)
{
if (ilist == null) throw new IllegalStateException ("Must already have some documents first.");
for (Instance inst : additionalDocuments)
ilist.add(inst);
assert (ilist.getDataAlphabet() == additionalDocuments.getDataAlphabet());
assert (additionalDocuments.getDataAlphabet().size() >= numTypes);
numTypes = additionalDocuments.getDataAlphabet().size();
int numNewDocs = additionalDocuments.size();
int numOldDocs = topics.length;
int numDocs = numOldDocs+ numNewDocs;
// Expand various arrays to make space for the new data.
int[][] newTopics = new int[numDocs][];
for (int i = 0; i < topics.length; i++)
newTopics[i] = topics[i];
topics = newTopics; // The rest of this array will be initialized below.
int[][] newDocTopicCounts = new int[numDocs][numTopics];
for (int i = 0; i < docTopicCounts.length; i++)
newDocTopicCounts[i] = docTopicCounts[i];
docTopicCounts = newDocTopicCounts; // The rest of this array will be initialized below.
int [][] newTypeTopicCounts = new int[numTypes][numTopics];
for (int i = 0; i < typeTopicCounts.length; i++)
for (int j = 0; j < numTopics; j++)
newTypeTopicCounts[i][j] = typeTopicCounts[i][j]; // This array further populated below
FeatureSequence fs;
for (int di = numOldDocs; di < numDocs; di++) {
try {
fs = (FeatureSequence) additionalDocuments.get(di-numOldDocs).getData();
} catch (ClassCastException e) {
System.err.println ("LDA and other topic models expect FeatureSequence data, not FeatureVector data. "
+"With text2vectors, you can obtain such data with --keep-sequence or --keep-bisequence.");
throw e;
}
int seqLen = fs.getLength();
numTokens += seqLen;
topics[di] = new int[seqLen];
// Randomly assign tokens to topics
for (int si = 0; si < seqLen; si++) {
int topic = r.nextInt(numTopics);
topics[di][si] = topic;
docTopicCounts[di][topic]++;
typeTopicCounts[fs.getIndexAtPosition(si)][topic]++;
tokensPerTopic[topic]++;
}
}
}
示例7: initialize
import cc.mallet.util.Randoms; //导入方法依赖的package包/类
public void initialize(InstanceList instances, InstanceList testing,
int numLevels, Randoms random) {
this.instances = instances;
this.testing = testing;
this.numLevels = numLevels;
this.random = random;
if (! (instances.get(0).getData() instanceof FeatureSequence)) {
throw new IllegalArgumentException("Input must be a FeatureSequence, using the --feature-sequence option when impoting data, for example");
}
numDocuments = instances.size();
numTypes = instances.getDataAlphabet().size();
etaSum = eta * numTypes;
// Initialize a single path
NCRPNode[] path = new NCRPNode[numLevels];
rootNode = new NCRPNode(numTypes);
levels = new int[numDocuments][];
documentLeaves = new NCRPNode[numDocuments];
// Initialize and fill the topic pointer arrays for
// every document. Set everything to the single path that
// we added earlier.
for (int doc=0; doc < numDocuments; doc++) {
FeatureSequence fs = (FeatureSequence) instances.get(doc).getData();
int seqLen = fs.getLength();
path[0] = rootNode;
rootNode.customers++;
for (int level = 1; level < numLevels; level++) {
path[level] = path[level-1].select();
path[level].customers++;
}
node = path[numLevels - 1];
levels[doc] = new int[seqLen];
documentLeaves[doc] = node;
for (int token=0; token < seqLen; token++) {
int type = fs.getIndexAtPosition(token);
levels[doc][token] = random.nextInt(numLevels);
node = path[ levels[doc][token] ];
node.totalTokens++;
node.typeCounts[type]++;
}
}
}