Search in sources :

Example 1 with EmbeddingExtractor

use of edu.stanford.nlp.coref.neural.EmbeddingExtractor in project CoreNLP by stanfordnlp.

the class ConvertModels method readFastCoref.

public static FastNeuralCorefModel readFastCoref(ObjectInputStream in) throws IOException, ClassNotFoundException {
    Function<List<List<Double>>, SimpleMatrix> f = (x) -> toMatrix(x);
    boolean conll = ErasureUtils.uncheckedCast(in.readObject());
    boolean hasStatic = ErasureUtils.uncheckedCast(in.readObject());
    Embedding staticEmbedding = (hasStatic) ? readEmbedding(in) : null;
    Embedding tunedEmbedding = readEmbedding(in);
    String naEmbedding = ErasureUtils.uncheckedCast(in.readObject());
    EmbeddingExtractor embedding = new EmbeddingExtractor(conll, staticEmbedding, tunedEmbedding, naEmbedding);
    Map<String, Integer> pairFeatures = ErasureUtils.uncheckedCast(in.readObject());
    Map<String, Integer> mentionFeatures = ErasureUtils.uncheckedCast(in.readObject());
    List<SimpleMatrix> weights = CollectionUtils.transformAsList(ErasureUtils.uncheckedCast(in.readObject()), f);
    return new FastNeuralCorefModel(embedding, pairFeatures, mentionFeatures, weights);
}
Also used : DVModelReranker(edu.stanford.nlp.parser.dvparser.DVModelReranker) ErasureUtils(edu.stanford.nlp.util.ErasureUtils) ObjectInputStream(java.io.ObjectInputStream) LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser) Function(java.util.function.Function) ArrayList(java.util.ArrayList) FastNeuralCorefModel(edu.stanford.nlp.coref.fastneural.FastNeuralCorefModel) RNNOptions(edu.stanford.nlp.sentiment.RNNOptions) Locale(java.util.Locale) Map(java.util.Map) ObjectOutputStream(java.io.ObjectOutputStream) SentimentModel(edu.stanford.nlp.sentiment.SentimentModel) CollectionUtils(edu.stanford.nlp.util.CollectionUtils) SimpleMatrix(org.ejml.simple.SimpleMatrix) Properties(java.util.Properties) IOUtils(edu.stanford.nlp.io.IOUtils) FileOutputStream(java.io.FileOutputStream) NeuralCorefModel(edu.stanford.nlp.coref.neural.NeuralCorefModel) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) EmbeddingExtractor(edu.stanford.nlp.coref.neural.EmbeddingExtractor) StringUtils(edu.stanford.nlp.util.StringUtils) DVModel(edu.stanford.nlp.parser.dvparser.DVModel) Generics(edu.stanford.nlp.util.Generics) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap) EmbeddingExtractor(edu.stanford.nlp.coref.neural.EmbeddingExtractor) FastNeuralCorefModel(edu.stanford.nlp.coref.fastneural.FastNeuralCorefModel) SimpleMatrix(org.ejml.simple.SimpleMatrix) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with EmbeddingExtractor

use of edu.stanford.nlp.coref.neural.EmbeddingExtractor in project CoreNLP by stanfordnlp.

the class ConvertModels method writeFastCoref.

public static void writeFastCoref(FastNeuralCorefModel model, ObjectOutputStream out) throws IOException {
    Function<SimpleMatrix, List<List<Double>>> f = (SimpleMatrix x) -> fromMatrix(x);
    EmbeddingExtractor embedding = model.getEmbeddingExtractor();
    out.writeObject(embedding.isConll());
    Embedding staticEmbedding = embedding.getStaticWordEmbeddings();
    if (staticEmbedding == null) {
        out.writeObject(false);
    } else {
        out.writeObject(true);
        writeEmbedding(staticEmbedding, out);
    }
    writeEmbedding(embedding.getTunedWordEmbeddings(), out);
    out.writeObject(embedding.getNAEmbedding());
    out.writeObject(model.getPairFeatureIds());
    out.writeObject(model.getMentionFeatureIds());
    out.writeObject(CollectionUtils.transformAsList(model.getAllWeights(), f));
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) EmbeddingExtractor(edu.stanford.nlp.coref.neural.EmbeddingExtractor) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with EmbeddingExtractor

use of edu.stanford.nlp.coref.neural.EmbeddingExtractor in project CoreNLP by stanfordnlp.

the class FastNeuralCorefModel method loadFromTextFiles.

public static FastNeuralCorefModel loadFromTextFiles(String path) {
    List<SimpleMatrix> weights = NeuralUtils.loadTextMatrices(path + "weights.txt");
    weights.set(weights.size() - 2, weights.get(weights.size() - 2).transpose());
    Embedding embeddings = new Embedding(path + "embeddings.txt");
    EmbeddingExtractor extractor = new EmbeddingExtractor(false, null, embeddings, "<missing>");
    Map<String, Integer> pairFeatureIds = loadMapFromTextFile(path + "pair_features.txt");
    Map<String, Integer> mentionFeatureIds = loadMapFromTextFile(path + "mention_features.txt");
    return new FastNeuralCorefModel(extractor, pairFeatureIds, mentionFeatureIds, weights);
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) EmbeddingExtractor(edu.stanford.nlp.coref.neural.EmbeddingExtractor) Embedding(edu.stanford.nlp.neural.Embedding)

Aggregations

EmbeddingExtractor (edu.stanford.nlp.coref.neural.EmbeddingExtractor)3 SimpleMatrix (org.ejml.simple.SimpleMatrix)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FastNeuralCorefModel (edu.stanford.nlp.coref.fastneural.FastNeuralCorefModel)1 NeuralCorefModel (edu.stanford.nlp.coref.neural.NeuralCorefModel)1 IOUtils (edu.stanford.nlp.io.IOUtils)1 Embedding (edu.stanford.nlp.neural.Embedding)1 DVModel (edu.stanford.nlp.parser.dvparser.DVModel)1 DVModelReranker (edu.stanford.nlp.parser.dvparser.DVModelReranker)1 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)1 RNNOptions (edu.stanford.nlp.sentiment.RNNOptions)1 SentimentModel (edu.stanford.nlp.sentiment.SentimentModel)1 CollectionUtils (edu.stanford.nlp.util.CollectionUtils)1 ErasureUtils (edu.stanford.nlp.util.ErasureUtils)1 Generics (edu.stanford.nlp.util.Generics)1 StringUtils (edu.stanford.nlp.util.StringUtils)1 TwoDimensionalMap (edu.stanford.nlp.util.TwoDimensionalMap)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1