本文整理汇总了Java中org.deeplearning4j.models.word2vec.wordstore.VocabCache.numWords方法的典型用法代码示例。如果您正苦于以下问题:Java VocabCache.numWords方法的具体用法?Java VocabCache.numWords怎么用?Java VocabCache.numWords使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.deeplearning4j.models.word2vec.wordstore.VocabCache
的用法示例。
在下文中一共展示了VocabCache.numWords方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: writeSequenceVectors
import org.deeplearning4j.models.word2vec.wordstore.VocabCache; //导入方法依赖的package包/类
/**
* This method saves specified SequenceVectors model to target OutputStream
*
* @param vectors SequenceVectors model
* @param factory SequenceElementFactory implementation for your objects
* @param stream Target output stream
* @param <T>
*/
public static <T extends SequenceElement> void writeSequenceVectors(@NonNull SequenceVectors<T> vectors,
@NonNull SequenceElementFactory<T> factory, @NonNull OutputStream stream) throws IOException {
WeightLookupTable<T> lookupTable = vectors.getLookupTable();
VocabCache<T> vocabCache = vectors.getVocab();
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream, "UTF-8")));
// at first line we save VectorsConfiguration
writer.write(vectors.getConfiguration().toEncodedJson());
// now we have elements one by one
for (int x = 0; x < vocabCache.numWords(); x++) {
T element = vocabCache.elementAtIndex(x);
String json = factory.serialize(element);
INDArray d = Nd4j.create(1);
double[] vector = lookupTable.vector(element.getLabel()).dup().data().asDouble();
ElementPair pair = new ElementPair(json, vector);
writer.println(pair.toEncodedJson());
writer.flush();
}
writer.flush();
writer.close();
}
示例2: writeWordVectors
import org.deeplearning4j.models.word2vec.wordstore.VocabCache; //导入方法依赖的package包/类
/**
* This mehod writes word vectors to the given OutputStream.
* Please note: this method doesn't load whole vocab/lookupTable into memory, so it's able to process large vocabularies served over network.
*
* @param lookupTable
* @param stream
* @param <T>
* @throws IOException
*/
public static <T extends SequenceElement> void writeWordVectors(WeightLookupTable<T> lookupTable,
OutputStream stream) throws IOException {
VocabCache<T> vocabCache = lookupTable.getVocabCache();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
// saving header as "NUM_WORDS VECTOR_SIZE NUM_DOCS"
String str = vocabCache.numWords() + " " + lookupTable.layerSize() + " " + vocabCache.totalNumberOfDocs();
log.debug("Saving header: {}", str);
writer.println(str);
// saving vocab content
for (int x = 0; x < vocabCache.numWords(); x++) {
T element = vocabCache.elementAtIndex(x);
StringBuilder builder = new StringBuilder();
builder.append(encodeB64(element.getLabel())).append(" ");
INDArray vec = lookupTable.vector(element.getLabel());
for (int i = 0; i < vec.length(); i++) {
builder.append(vec.getDouble(i));
if (i < vec.length() - 1)
builder.append(" ");
}
writer.println(builder.toString());
}
writer.flush();
writer.close();
}
示例3: writeVocabCache
import org.deeplearning4j.models.word2vec.wordstore.VocabCache; //导入方法依赖的package包/类
/**
* This method saves vocab cache to provided OutputStream.
* Please note: it saves only vocab content, so it's suitable mostly for BagOfWords/TF-IDF vectorizers
*
* @param vocabCache
* @param stream
* @throws UnsupportedEncodingException
*/
public static void writeVocabCache(@NonNull VocabCache<VocabWord> vocabCache, @NonNull OutputStream stream)
throws IOException {
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream, "UTF-8")));
for (int x = 0; x < vocabCache.numWords(); x++) {
VocabWord word = vocabCache.elementAtIndex(x);
writer.println(word.toJSON());
}
writer.flush();
writer.close();
}