Examples of SparseDoubleVector


Examples of edu.ucla.sspace.vector.SparseDoubleVector

     * @param word a word that requires a semantic vector
     *
     * @return the {@code SemanticVector} representing {@code word}
     */
    private SparseDoubleVector getSemanticVector(String word) {
        SparseDoubleVector v = termToVector.get(word);
        if (v == null) {
            // lock on the word in case multiple threads attempt to add it at
            // once
            synchronized(this) {
                // recheck in case another thread added it while we were waiting
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

    /**
     * {@inheritDoc}
     */
    public Vector getVector(String term) {
        SparseDoubleVector v = termToVector.get(term);
        return (v == null) ? null : Vectors.immutable(
            Vectors.subview(v, 0, basisMapping.numDimensions()));
    }
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

            for (int wordIndex = 0; wordIndex < nodes.length; ++wordIndex) {

                String focusWord = nodes[wordIndex].word();             

                // Acquire the semantic vector for the focus word.
                SparseDoubleVector focusMeaning = getSemanticVector(focusWord);

                // Get all the valid paths starting from this word.  The
                // acceptor will filter out any paths that don't contain the
                // semantic connections we're looking for.
                Iterator<DependencyPath> paths = new FilteredDependencyIterator(
                            nodes[wordIndex], acceptor, pathLength);
               
                // For each of the paths rooted at the focus word, update the
                // co-occurrences of the focus word in the dimension that the
                // BasisFunction states.
                while (paths.hasNext()) {
                    DependencyPath path = paths.next();

                    // Get the dimension associated with the relation and/or
                    // words in the path from the basis function.  The basis
                    // function creates a specific dimension for the syntactic
                    // context in order to meaningfully comparable vectors.
                    int dimension = basisMapping.getDimension(path);

                    // Then calculate the weight for the feature presence in the
                    // dimension.  For example, the weighter might score paths
                    // inversely proportional to their length.
                    double weight = weighter.scorePath(path);

                    // Last, update the focus word's semantic vector based on
                    // the dimension and weight
                    synchronized(focusMeaning) {
                        focusMeaning.add(dimension, weight);                   
                    }
                }
            }
        }
        document.close();
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

                        ? 1
                        : 1 + focusFreq.intValue());

                // Get the temprorary semantics for the focus word, create a new
                // vector for them if needed.
                SparseDoubleVector focusSemantics = wordDocSemantics.get(
                        focusWord);
                if (focusSemantics == null) {
                    focusSemantics = new SparseHashDoubleVector(
                            Integer.MAX_VALUE);
                    wordDocSemantics.put(focusWord, focusSemantics);
                }

                // Process the previous words.
                int offset = 4 - prevWords.size();
                for (String word : prevWords) {
                    offset++;
                    if (word.equals(IteratorFactory.EMPTY_TOKEN))
                        continue;
                    int index = getIndexFor(word);
                    focusSemantics.add(index, offset);
                }

                // Process the next words.
                offset = 5;
                for (String word : nextWords) {
                    offset--;
                    if (word.equals(IteratorFactory.EMPTY_TOKEN))
                        continue;
                    int index = getIndexFor(word);
                    focusSemantics.add(index, offset);
                }
            }

            prevWords.offer(focusWord);
            if (prevWords.size() > 4)
                prevWords.remove();
        }

        // Add the temporary vectors for each word in this document to the
        // actual semantic fectors.
        for (Map.Entry<String, SparseDoubleVector> e :
                wordDocSemantics.entrySet()) {
            SparseDoubleVector focusSemantics = getSemanticVector(
                    e.getKey());
            // Get the non zero indices before hand so that they are cached
            // during the synchronized section.
            focusSemantics.getNonZeroIndices();
            synchronized (focusSemantics) {
                VectorMath.add(focusSemantics, e.getValue());
            }
        }
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

     * @param word a word
     *
     * @return the {@code SemanticVector} for the provide word.
     */
    private SparseDoubleVector getSemanticVector(String word) {
        SparseDoubleVector v = wordToSemantics.get(word);
        if (v == null) {
            // lock on the word in case multiple threads attempt to add it at
            // once
            synchronized(this) {
                // recheck in case another thread added it while we were waiting
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

    }

    private void add(String relation,
                     SparseDoubleVector vector,
                     Map<String, SparseDoubleVector> map) {
        SparseDoubleVector preference = map.get(relation);
        if (preference == null) {
            map.put(relation,
                    (SparseDoubleVector) Vectors.copyOf(vector));
            return;
        }
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

        int numDims = termToIndex.numDimensions();

        // Iterate through the document's tokens and build the document
        // representation for those terms that have an existing basis in the
        // space
        SparseDoubleVector docVec = new SparseHashDoubleVector(numDims);
        while (docTokens.hasNext()) {
            int dim = termToIndex.getDimension(docTokens.next());
            if (dim >= 0)
                docVec.add(dim, 1d);
        }       
       
        // Transform the vector according to this instance's transform's state,
        // which should normalize the vector as the original vectors were.
        DoubleVector transformed = transform.transform(docVec);

        // Represent the document as a 1-column matrix       
        Matrix queryAsMatrix = new ArrayMatrix(1, numDims);
        for (int nz : docVec.getNonZeroIndices())
            queryAsMatrix.set(0, nz, transformed.get(nz));
       
        // Project the new document vector, d, by using
        //
        //   d * U_k * Sigma_k^-1
 
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

        Queue<String> nextWords = new ArrayDeque<String>();
        for (int i = focusIndex+1;
                 i < Math.min(focusIndex+windowSize+1, tree.length); ++i)
            nextWords.add(getFeature(tree[i], i-focusIndex));

        SparseDoubleVector focusMeaning = new CompactSparseVector();
        addContextTerms(focusMeaning, prevWords, -1 * prevWords.size());
        addContextTerms(focusMeaning, nextWords, 1);
        return focusMeaning;
    }
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

            double averageNumNonZeros = 0;

            if (m instanceof SparseMatrix) {
                SparseMatrix sm = (SparseMatrix) m;
                for (int r = 0; r < m.rows(); ++r) {
                    SparseDoubleVector v = sm.getRowVector(r);
                    int[] nonZeros = v.getNonZeroIndices();
                    numNonZeros[r] += nonZeros.length;
                    averageNumNonZeros += nonZeros.length;
                    for (int column : nonZeros) {
                        nonZeroFeatures.add(column);
                        double value = v.get(column);
                        // Get the max and minimum value for the row.
                        if (value < minValues[column])
                            minValues[column] = value;
                        if (value > maxValues[column])
                            maxValues[column] = value;
View Full Code Here

Examples of edu.ucla.sspace.vector.SparseDoubleVector

                int cols = minValues.length;

                // If the average number of values per row is significantly
                // smaller than the total number of columns then select a subset
                // to be non zero.
                SparseDoubleVector column = new CompactSparseVector(cols);
                int numNonZeros =
                    (int) (random.nextGaussian() * stdevNumValuesPerRow +
                           averageNumValuesPerRow);
                if (numNonZeros == 0)
                    numNonZeros++;

                for (int j = 0; j < numNonZeros; ++j) {
                    // Get the next index to set.
                    int col = getNonZeroColumn();
                    double value = random.nextDouble() *
                            (maxValues[col] - minValues[col]) + minValues[col];
                    column.set(col, value);
                }
                vectors.add(column);
            }
            //builder.finish();

View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.