Package edu.ucla.sspace.vector

Examples of edu.ucla.sspace.vector.SparseDoubleVector


        for (String word : words) {
            dos.writeUTF(word);
            Vector vector = sspace.getVector(word);
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    dos.writeInt(nz.length);
                    for (int i : nz) {
                        dos.writeInt(i);
                        dos.writeDouble(sdv.get(i));
                    }
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();
View Full Code Here


            // Ignore any focus words that are unaccepted by Wordsi.
            if (!acceptWord(focusNode, contextTokens[1], wordsi))
                return;

            // Create a new context vector and send it to the Wordsi model.
            SparseDoubleVector focusMeaning = generator.generateContext(
                    nodes, focusIndex);
            wordsi.handleContextVector(secondarykey, focusWord, focusMeaning);

            // Close up the document.
            document.close();
View Full Code Here

            if (it.hasNext())
                addNextToken(it.next(), nextWords, nextRealWord);

            // Represent the word if wordsi is willing to process it.
            if (!replacementWord.equals(EMPTY)) {
                SparseDoubleVector contextVector = generator.generateContext(
                        prevWords, nextWords);
                wordsi.handleContextVector(
                        focusWord, replacementWord, contextVector);
            }
View Full Code Here

        // Extract the set of words to consider after the focus word.
        while (it.hasNext() && nextWords.size() < windowSize)
            nextWords.offer(it.next().intern());

        // Create the context vector and have wordsi handle it.
        SparseDoubleVector contextVector = generator.generateContext(
                prevWords, nextWords);
        wordsi.handleContextVector(focusWord, instanceId, contextVector);
    }
View Full Code Here

            pw.print(word + "|");
            // For each vector, write all the non-zero elements and their indices
            StringBuilder sb = null;
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(0).append(",").append(sdv.get(0));
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(i).append(",").append(sdv.get(i));
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();                   
                    sb = new StringBuilder(nz.length * 4);
                    // special case the first
                    sb.append(0).append(",")
                        .append(sv.getValue(0).doubleValue());
                    for (int i = 1; i < nz.length; ++i)
                        sb.append(",").append(i).append(",").
                            append(sv.getValue(i).doubleValue());
                }
            }
           
            else {
                boolean first = true;
                sb = new StringBuilder(vectorLength / 2);
                for (int i = 0; i < vector.length(); ++i) {
                    double d = vector.getValue(i).doubleValue();
                    if (d != 0d) {
                        if (first) {
                            sb.append(i).append(",").append(d);
                            first = false;
                        }
                        else {
                            sb.append(",").append(i).append(",").append(d);
                        }
                    }
                }
            }
            pw.println(sb.toString());
            pw.flush();
            break;
        }
           


        case TEXT: {
            PrintWriter pw = new PrintWriter(writer);
            pw.println(word + "|" + VectorIO.toString(vector));
            pw.flush();
            break;
        }

        case BINARY: {
            writer.writeUTF(word);
            for (int i = 0; i < vector.length(); ++i) {
                writer.writeDouble(vector.getValue(i).doubleValue());
            }           
            break;
        }

        case SPARSE_BINARY: {
            writer.writeUTF(word);
            if (vector instanceof SparseVector) {
                if (vector instanceof DoubleVector) {
                    SparseDoubleVector sdv = (SparseDoubleVector)vector;
                    int[] nz = sdv.getNonZeroIndices();
                    writer.writeInt(nz.length);
                    for (int i : nz) {
                        writer.writeInt(i);
                        writer.writeDouble(sdv.get(i));
                    }
                }
                else {
                    SparseVector sv = (SparseVector)vector;
                    int[] nz = sv.getNonZeroIndices();
View Full Code Here

                                                          DoubleVector v) {
        DoubleVector newV = new DenseVector(matrix.columns());
        if (matrix instanceof SparseMatrix) {
            SparseMatrix smatrix = (SparseMatrix) matrix;
            for (int r = 0; r < smatrix.rows(); ++r) {
                SparseDoubleVector row = smatrix.getRowVector(r);
                int[] nonZeros = row.getNonZeroIndices();
                for (int c : nonZeros)
                    newV.add(c, row.get(c) * v.get(r));
            }
        } else {
            for (int r = 0; r < matrix.rows(); ++r)
                for (int c = 0; c < matrix.columns(); ++c)
                    newV.add(c, matrix.get(r, c) * v.get(r));
 
View Full Code Here

        // Special case for sparse matrices.
        if (matrix instanceof SparseMatrix) {
            SparseMatrix smatrix = (SparseMatrix) matrix;
            for (int r = 0; r < smatrix.rows(); ++r) {
                double vValue = 0;
                SparseDoubleVector row = smatrix.getRowVector(r);
                int[] nonZeros = row.getNonZeroIndices();
                for (int c : nonZeros)
                    vValue += row.get(c) * newV.get(c);
                v.set(r, vValue);
            }
        } else {
            // Handle dense matrices.
            for (int r = 0; r < matrix.rows(); ++r) {
View Full Code Here

        File f = getSparseBinarySVDLIBCFile();
        Iterator<SparseDoubleVector> it =
            new SvdlibcSparseBinaryFileRowIterator(f);

        // Check row 1.
        SparseDoubleVector vector = it.next();
        assertEquals(2, vector.getNonZeroIndices().length);
        assertEquals(2.3, vector.get(0), .0001);
        assertEquals(3.8, vector.get(2), .0001);

        // Check row 2.
        vector = it.next();
        assertEquals(1, vector.getNonZeroIndices().length);
        assertEquals(1.3, vector.get(1), .0001);

        // Check row 3.
        vector = it.next();
        assertEquals(3, vector.getNonZeroIndices().length);
        assertEquals(4.2, vector.get(0), .0001);
        assertEquals(2.2, vector.get(1), .0001);
        assertEquals(0.5, vector.get(2), .0001);

        // Check that there are no more rows.
        assertFalse(it.hasNext());
    }
View Full Code Here

     */
    public SparseDoubleVector next() {
        if (next == null)
            throw new NoSuchElementException("No futher entries");

        SparseDoubleVector curCol = next;
        try {
            advance();
        } catch (IOException ioe) {
            throw new IOError(ioe);
        }
View Full Code Here

        // the correspond row of A against this difference matrix.  Instead of
        // computing the inner product of A' and (AX-Y), we update each k,m
        // value in G by iterating through the columns of A.
        for (int r = 0; r < Y.rows(); ++r) {
            // Get the row vector of Y.
            SparseDoubleVector v = Y.getRowVector(r);
            double[] vDiff = new double[v.length()];
            // Compute the difference between the row vector of AX and the row
            // vector of Y.  This is the straightforward dot product between A_i
            // and X'_i.
            for (int c = 0; c < X.columns(); c++) {
                double sum = 0;
                for (int k = 0; k < A.columns(); ++k)
                    sum += A.get(r, k) * X.get(k, c);
                vDiff[c] = sum - v.get(c);
            }

            // Now get the row vector A_i and for each column k, multiply it
            // against each column c in vDiff to get the difference in the
            // gradient G_{k, c}.
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.vector.SparseDoubleVector

Copyright © 2018 www.massapicom. 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.