Search in sources :

Example 31 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class DVModel method readWordVectors.

public void readWordVectors() {
    SimpleMatrix unknownNumberVector = null;
    SimpleMatrix unknownCapsVector = null;
    SimpleMatrix unknownChineseYearVector = null;
    SimpleMatrix unknownChineseNumberVector = null;
    SimpleMatrix unknownChinesePercentVector = null;
    wordVectors = Generics.newTreeMap();
    int numberCount = 0;
    int capsCount = 0;
    int chineseYearCount = 0;
    int chineseNumberCount = 0;
    int chinesePercentCount = 0;
    //Map<String, SimpleMatrix> rawWordVectors = NeuralUtils.readRawWordVectors(op.lexOptions.wordVectorFile, op.lexOptions.numHid);
    Embedding rawWordVectors = new Embedding(op.lexOptions.wordVectorFile, op.lexOptions.numHid);
    for (String word : rawWordVectors.keySet()) {
        SimpleMatrix vector = rawWordVectors.get(word);
        if (op.wordFunction != null) {
            word = op.wordFunction.apply(word);
        }
        wordVectors.put(word, vector);
        if (op.lexOptions.numHid <= 0) {
            op.lexOptions.numHid = vector.getNumElements();
        }
        // TODO: factor out all of these identical blobs
        if (op.trainOptions.unknownNumberVector && (NUMBER_PATTERN.matcher(word).matches() || DG_PATTERN.matcher(word).matches())) {
            ++numberCount;
            if (unknownNumberVector == null) {
                unknownNumberVector = new SimpleMatrix(vector);
            } else {
                unknownNumberVector = unknownNumberVector.plus(vector);
            }
        }
        if (op.trainOptions.unknownCapsVector && CAPS_PATTERN.matcher(word).matches()) {
            ++capsCount;
            if (unknownCapsVector == null) {
                unknownCapsVector = new SimpleMatrix(vector);
            } else {
                unknownCapsVector = unknownCapsVector.plus(vector);
            }
        }
        if (op.trainOptions.unknownChineseYearVector && CHINESE_YEAR_PATTERN.matcher(word).matches()) {
            ++chineseYearCount;
            if (unknownChineseYearVector == null) {
                unknownChineseYearVector = new SimpleMatrix(vector);
            } else {
                unknownChineseYearVector = unknownChineseYearVector.plus(vector);
            }
        }
        if (op.trainOptions.unknownChineseNumberVector && (CHINESE_NUMBER_PATTERN.matcher(word).matches() || DG_PATTERN.matcher(word).matches())) {
            ++chineseNumberCount;
            if (unknownChineseNumberVector == null) {
                unknownChineseNumberVector = new SimpleMatrix(vector);
            } else {
                unknownChineseNumberVector = unknownChineseNumberVector.plus(vector);
            }
        }
        if (op.trainOptions.unknownChinesePercentVector && CHINESE_PERCENT_PATTERN.matcher(word).matches()) {
            ++chinesePercentCount;
            if (unknownChinesePercentVector == null) {
                unknownChinesePercentVector = new SimpleMatrix(vector);
            } else {
                unknownChinesePercentVector = unknownChinesePercentVector.plus(vector);
            }
        }
    }
    String unkWord = op.trainOptions.unkWord;
    if (op.wordFunction != null) {
        unkWord = op.wordFunction.apply(unkWord);
    }
    SimpleMatrix unknownWordVector = wordVectors.get(unkWord);
    wordVectors.put(UNKNOWN_WORD, unknownWordVector);
    if (unknownWordVector == null) {
        throw new RuntimeException("Unknown word vector not specified in the word vector file");
    }
    if (op.trainOptions.unknownNumberVector) {
        if (numberCount > 0) {
            unknownNumberVector = unknownNumberVector.divide(numberCount);
        } else {
            unknownNumberVector = new SimpleMatrix(unknownWordVector);
        }
        wordVectors.put(UNKNOWN_NUMBER, unknownNumberVector);
    }
    if (op.trainOptions.unknownCapsVector) {
        if (capsCount > 0) {
            unknownCapsVector = unknownCapsVector.divide(capsCount);
        } else {
            unknownCapsVector = new SimpleMatrix(unknownWordVector);
        }
        wordVectors.put(UNKNOWN_CAPS, unknownCapsVector);
    }
    if (op.trainOptions.unknownChineseYearVector) {
        log.info("Matched " + chineseYearCount + " chinese year vectors");
        if (chineseYearCount > 0) {
            unknownChineseYearVector = unknownChineseYearVector.divide(chineseYearCount);
        } else {
            unknownChineseYearVector = new SimpleMatrix(unknownWordVector);
        }
        wordVectors.put(UNKNOWN_CHINESE_YEAR, unknownChineseYearVector);
    }
    if (op.trainOptions.unknownChineseNumberVector) {
        log.info("Matched " + chineseNumberCount + " chinese number vectors");
        if (chineseNumberCount > 0) {
            unknownChineseNumberVector = unknownChineseNumberVector.divide(chineseNumberCount);
        } else {
            unknownChineseNumberVector = new SimpleMatrix(unknownWordVector);
        }
        wordVectors.put(UNKNOWN_CHINESE_NUMBER, unknownChineseNumberVector);
    }
    if (op.trainOptions.unknownChinesePercentVector) {
        log.info("Matched " + chinesePercentCount + " chinese percent vectors");
        if (chinesePercentCount > 0) {
            unknownChinesePercentVector = unknownChinesePercentVector.divide(chinesePercentCount);
        } else {
            unknownChinesePercentVector = new SimpleMatrix(unknownWordVector);
        }
        wordVectors.put(UNKNOWN_CHINESE_PERCENT, unknownChinesePercentVector);
    }
    if (op.trainOptions.useContextWords) {
        SimpleMatrix start = SimpleMatrix.random(op.lexOptions.numHid, 1, -0.5, 0.5, rand);
        SimpleMatrix end = SimpleMatrix.random(op.lexOptions.numHid, 1, -0.5, 0.5, rand);
        wordVectors.put(START_WORD, start);
        wordVectors.put(END_WORD, end);
    }
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Embedding(edu.stanford.nlp.neural.Embedding)

Example 32 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class NeuralUtilsTest method testIsZero.

public void testIsZero() {
    double[][] values = new double[][] { { 0.1, 0.2, 0.3, 0.4, 0.5 }, { 0.0, 0.0, 0.0, 0.0, 0.0 } };
    SimpleMatrix vector1 = new SimpleMatrix(values);
    assertFalse(NeuralUtils.isZero(vector1));
    values = new double[][] { { 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0 } };
    vector1 = new SimpleMatrix(values);
    assertTrue(NeuralUtils.isZero(vector1));
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 33 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project jmonkeyengine by jMonkeyEngine.

the class Matrix method pseudoinverse.

@SuppressWarnings("unchecked")
public Matrix pseudoinverse(double lambda) {
    SimpleSVD<SimpleMatrix> simpleSVD = this.svd();
    SimpleMatrix U = simpleSVD.getU();
    SimpleMatrix S = simpleSVD.getW();
    SimpleMatrix V = simpleSVD.getV();
    int N = Math.min(this.numRows(), this.numCols());
    double maxSingular = 0;
    for (int i = 0; i < N; ++i) {
        if (S.get(i, i) > maxSingular) {
            maxSingular = S.get(i, i);
        }
    }
    double tolerance = FastMath.DBL_EPSILON * Math.max(this.numRows(), this.numCols()) * maxSingular;
    for (int i = 0; i < Math.min(S.numRows(), S.numCols()); ++i) {
        double a = S.get(i, i);
        if (a <= tolerance) {
            a = 0;
        } else {
            a = a / (a * a + lambda * lambda);
        }
        S.set(i, i, a);
    }
    return new Matrix(V.mult(S.transpose()).mult(U.transpose()));
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) SimpleMatrix(org.ejml.simple.SimpleMatrix)

Example 34 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project jmonkeyengine by jMonkeyEngine.

the class ConstraintDefinitionIK method bake.

@Override
public void bake(Space ownerSpace, Space targetSpace, Transform targetTransform, float influence) {
    if (influence == 0 || !trackToBeChanged || targetTransform == null || bonesCount == 0) {
        // no need to do anything
        return;
    }
    if (bones == null) {
        bones = new BonesChain((Bone) this.getOwner(), useTail, bonesAffected, alteredOmas, blenderContext);
    }
    if (bones.size() == 0) {
        bonesCount = 0;
        // no need to do anything
        return;
    }
    double distanceFromTarget = Double.MAX_VALUE;
    target.set(targetTransform.getTranslation().x, targetTransform.getTranslation().y, targetTransform.getTranslation().z);
    if (bonesCount < 0) {
        bonesCount = bones.size();
        rotationVectors = new Vector3d[bonesCount];
        for (int i = 0; i < bonesCount; ++i) {
            rotationVectors[i] = new Vector3d();
        }
        J = new Matrix(3, bonesCount);
    }
    BoneContext topBone = bones.get(0);
    for (int i = 0; i < iterations; ++i) {
        DTransform topBoneTransform = bones.getWorldTransform(topBone);
        // effector
        Vector3d e = topBoneTransform.getTranslation().add(topBoneTransform.getRotation().mult(Vector3d.UNIT_Y).multLocal(topBone.getLength()));
        distanceFromTarget = e.distance(target);
        if (distanceFromTarget <= MIN_DISTANCE) {
            break;
        }
        deltaP.setColumn(0, 0, target.x - e.x, target.y - e.y, target.z - e.z);
        int column = 0;
        for (BoneContext boneContext : bones) {
            DTransform boneWorldTransform = bones.getWorldTransform(boneContext);
            // current join position
            Vector3d j = boneWorldTransform.getTranslation();
            Vector3d vectorFromJointToEffector = e.subtract(j);
            vectorFromJointToEffector.cross(target.subtract(j), rotationVectors[column]).normalizeLocal();
            rotationVectors[column].cross(vectorFromJointToEffector, col);
            J.setColumn(col, column++);
        }
        Matrix J_1 = J.pseudoinverse();
        SimpleMatrix deltaThetas = J_1.mult(deltaP);
        if (deltaThetas.elementMaxAbs() < MIN_ANGLE_CHANGE) {
            break;
        }
        for (int j = 0; j < deltaThetas.numRows(); ++j) {
            double angle = deltaThetas.get(j, 0);
            Vector3d rotationVector = rotationVectors[j];
            tempDQuaternion.fromAngleAxis(angle, rotationVector);
            BoneContext boneContext = bones.get(j);
            Bone bone = boneContext.getBone();
            if (bone.equals(this.getOwner())) {
                if (boneContext.isLockX()) {
                    tempDQuaternion.set(0, tempDQuaternion.getY(), tempDQuaternion.getZ(), tempDQuaternion.getW());
                }
                if (boneContext.isLockY()) {
                    tempDQuaternion.set(tempDQuaternion.getX(), 0, tempDQuaternion.getZ(), tempDQuaternion.getW());
                }
                if (boneContext.isLockZ()) {
                    tempDQuaternion.set(tempDQuaternion.getX(), tempDQuaternion.getY(), 0, tempDQuaternion.getW());
                }
            }
            DTransform boneTransform = bones.getWorldTransform(boneContext);
            boneTransform.getRotation().set(tempDQuaternion.mult(boneTransform.getRotation()));
            bones.setWorldTransform(boneContext, boneTransform);
        }
    }
    // applying the results
    for (int i = bonesCount - 1; i >= 0; --i) {
        BoneContext boneContext = bones.get(i);
        DTransform transform = bones.getWorldTransform(boneContext);
        constraintHelper.applyTransform(boneContext.getArmatureObjectOMA(), boneContext.getBone().getName(), Space.CONSTRAINT_SPACE_WORLD, transform.toTransform());
    }
    // need to reload them again
    bones = null;
}
Also used : SimpleMatrix(org.ejml.simple.SimpleMatrix) Matrix(com.jme3.scene.plugins.blender.math.Matrix) SimpleMatrix(org.ejml.simple.SimpleMatrix) Vector3d(com.jme3.scene.plugins.blender.math.Vector3d) BoneContext(com.jme3.scene.plugins.blender.animations.BoneContext) DTransform(com.jme3.scene.plugins.blender.math.DTransform) Bone(com.jme3.animation.Bone)

Example 35 with SimpleMatrix

use of org.ejml.simple.SimpleMatrix in project CoreNLP by stanfordnlp.

the class AverageDVModels method main.

/**
   * Command line arguments for this program:
   * <br>
   * -output: the model file to output
   * -input: a list of model files to input
   */
public static void main(String[] args) {
    String outputModelFilename = null;
    List<String> inputModelFilenames = Generics.newArrayList();
    for (int argIndex = 0; argIndex < args.length; ) {
        if (args[argIndex].equalsIgnoreCase("-output")) {
            outputModelFilename = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-input")) {
            for (++argIndex; argIndex < args.length && !args[argIndex].startsWith("-"); ++argIndex) {
                inputModelFilenames.addAll(Arrays.asList(args[argIndex].split(",")));
            }
        } else {
            throw new RuntimeException("Unknown argument " + args[argIndex]);
        }
    }
    if (outputModelFilename == null) {
        log.info("Need to specify output model name with -output");
        System.exit(2);
    }
    if (inputModelFilenames.size() == 0) {
        log.info("Need to specify input model names with -input");
        System.exit(2);
    }
    log.info("Averaging " + inputModelFilenames);
    log.info("Outputting result to " + outputModelFilename);
    LexicalizedParser lexparser = null;
    List<DVModel> models = Generics.newArrayList();
    for (String filename : inputModelFilenames) {
        LexicalizedParser parser = LexicalizedParser.loadModel(filename);
        if (lexparser == null) {
            lexparser = parser;
        }
        models.add(DVParser.getModelFromLexicalizedParser(parser));
    }
    List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.binaryTransform);
    List<TwoDimensionalMap<String, String, SimpleMatrix>> binaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.binaryScore);
    List<Map<String, SimpleMatrix>> unaryTransformMaps = CollectionUtils.transformAsList(models, model -> model.unaryTransform);
    List<Map<String, SimpleMatrix>> unaryScoreMaps = CollectionUtils.transformAsList(models, model -> model.unaryScore);
    List<Map<String, SimpleMatrix>> wordMaps = CollectionUtils.transformAsList(models, model -> model.wordVectors);
    TwoDimensionalMap<String, String, SimpleMatrix> binaryTransformAverages = averageBinaryMatrices(binaryTransformMaps);
    TwoDimensionalMap<String, String, SimpleMatrix> binaryScoreAverages = averageBinaryMatrices(binaryScoreMaps);
    Map<String, SimpleMatrix> unaryTransformAverages = averageUnaryMatrices(unaryTransformMaps);
    Map<String, SimpleMatrix> unaryScoreAverages = averageUnaryMatrices(unaryScoreMaps);
    Map<String, SimpleMatrix> wordAverages = averageUnaryMatrices(wordMaps);
    DVModel newModel = new DVModel(binaryTransformAverages, unaryTransformAverages, binaryScoreAverages, unaryScoreAverages, wordAverages, lexparser.getOp());
    DVParser newParser = new DVParser(newModel, lexparser);
    newParser.saveModel(outputModelFilename);
}
Also used : LexicalizedParser(edu.stanford.nlp.parser.lexparser.LexicalizedParser) SimpleMatrix(org.ejml.simple.SimpleMatrix) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap) Map(java.util.Map) TwoDimensionalMap(edu.stanford.nlp.util.TwoDimensionalMap)

Aggregations

SimpleMatrix (org.ejml.simple.SimpleMatrix)52 Tree (edu.stanford.nlp.trees.Tree)8 Map (java.util.Map)7 DeepTree (edu.stanford.nlp.trees.DeepTree)5 TwoDimensionalMap (edu.stanford.nlp.util.TwoDimensionalMap)5 SimpleTensor (edu.stanford.nlp.neural.SimpleTensor)4 LexicalizedParser (edu.stanford.nlp.parser.lexparser.LexicalizedParser)4 Pair (edu.stanford.nlp.util.Pair)4 IdentityHashMap (java.util.IdentityHashMap)4 Mention (edu.stanford.nlp.coref.data.Mention)3 BufferedWriter (java.io.BufferedWriter)3 File (java.io.File)3 FileWriter (java.io.FileWriter)3 ArrayList (java.util.ArrayList)3 CoreLabel (edu.stanford.nlp.ling.CoreLabel)2 Embedding (edu.stanford.nlp.neural.Embedding)2 ParserQuery (edu.stanford.nlp.parser.common.ParserQuery)2 RerankingParserQuery (edu.stanford.nlp.parser.lexparser.RerankingParserQuery)2 FileFilter (java.io.FileFilter)2 Bone (com.jme3.animation.Bone)1