Alex
Alex

Reputation: 235

word2vec deeplearning4j ArrayIndexOutOfBoundsException in WordVectorSerializer.loadTxtVectors()

I trained the word2vec model from http://deeplearning4j.org/word2vec successfully and now get this exception when trying to apply the wordsNearest:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 99
at 
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxt(WordVectorSerializer.java:1107)
at 
org.deeplearning4j.models.embeddings.loader.WordVectorSerializer.loadTxtVectors(WordVectorSerializer.java:1033)
at 
org.deeplearning4j.examples.nlp.word2vec.NearestWords.main(NearestWords.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

This is my code:

package org.deeplearning4j.examples.nlp.word2vec;
import java.io.File;
import java.util.Collection;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
public class NearestWords {
    public static void main(String[] args) throws Exception{
        File file = new File("pathToWriteto.txt");
        WordVectors vec = WordVectorSerializer.loadTxtVectors(file);
        Collection<String> similar = vec.wordsNearest("day", 10);
        System.out.println(similar);
    }
}

Upvotes: 0

Views: 308

Answers (1)

philipskokoh
philipskokoh

Reputation: 93

The current release (0.4-rc3.10) has a bug in loadTxt function. It's fixed in the master repo and will be reflected in next release. Read this github issue for your problem: https://github.com/deeplearning4j/deeplearning4j/issues/1721

An easy fix at the moment is to copy the latest WordVectorSerializer.java to your project. https://github.com/deeplearning4j/deeplearning4j/blob/master/deeplearning4j-scaleout/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/embeddings/loader/WordVectorSerializer.java

Upvotes: 0

Related Questions