Package types

Examples of types.SparseVector


        SparseVector[] x = new SparseVector[words.size()];
        Object[] y = new Object[tags.size()];
        for (int t = 0; t < x.length; t++) {
          y[t] = tags.get(t);
          String word = "^" + words.get(t) + "$";
          x[t] = new SparseVector();
          x[t].add(xAlphabet.lookupObject(word), 1);
          for (int i = 0; i < word.length() - 3; i++) {
            x[t].add(xAlphabet.lookupObject(word
                .substring(i, i + 3)), 1);
          }
View Full Code Here


    ArrayList<ClassificationInstance> result = new ArrayList<ClassificationInstance>();
    BufferedReader reader = new BufferedReader(new FileReader(dataLoc));
    String ln = reader.readLine();
    while (ln != null) {
      String[] feats = ln.split(",");
      SparseVector x = new SparseVector();
      Object y = feats[feats.length - 1];
      for (int i = 3; i < feats.length - 1; i++) {
        if (feats[i].equals("1"))
          x.add(xAlphabet.lookupObject(i), 1.0);
      }
      result.add(new ClassificationInstance(xAlphabet, yAlphabet, x, y));
      ln = reader.readLine();
    }
    return result;
View Full Code Here

          for (String w : ln.split("\\s\\s*")) {
            words.add(w);
          }
        }
        reader.close();
        SparseVector x = new SparseVector();
        for (String w : words)
          x.add(xAlphabet.lookupObject(w), 1);
        result.add(new ClassificationInstance(xAlphabet, yAlphabet, x,
            label));
      }
    }
    return result;
View Full Code Here

    numY = yAlphabet.size();
    defalutFeatureIndex = xAlphabet.size();
  }

  public SparseVector apply(SparseVector x, int y) {
    SparseVector res = new SparseVector();
    for (int i = 0; i < x.numEntries(); i++) {
      res.add(y * (defalutFeatureIndex + 1) + x.getIndexAt(i), x
          .getValueAt(i));
    }
    res.add(y * (defalutFeatureIndex + 1) + defalutFeatureIndex, 1);
    return res;
  }
View Full Code Here

    this.numInputs = xAlphabet.size() + 1;
  }

  // see SequenceFeatureFunction for java doc
  public SparseVector apply(SparseVector[] x, int[] y) {
    SparseVector result = this.apply(x, 0, y[0], 0);
    for (int t = 1; t < x.length; t++) {
      SparseVector fsubt = this.apply(x, y[t - 1], y[t], t);
      StaticUtils.plusEquals(result, fsubt);
    }
    return result;
  }
View Full Code Here

        this.yAsize * this.numInputs;
  }

  // see SequenceFeatureFunction for java doc
  public SparseVector apply(SparseVector[] xseq, int ytm1, int yt, int t) {
    SparseVector result = new SparseVector();
    for (int i = 0; i < xseq[t].numEntries(); i++) {
      int x = xseq[t].getIndexAt(i);
      // initial yt
      if (t == 0) {
        result.add(this.initialIndex(yt, x), 1);
        // ymt1 times yt
      } else {
        result.add(this.labelXlabel(ytm1, yt, x), 1);
      }
      if (t == xseq.length - 1) {
        result.add(this.finalIndex(yt, x), 1);
      }
      // yt times x
      result.add(this.labelXinput(yt, x), 1);
    }
    int x = this.xAsize;
    // initial yt
    if (t == 0) {
      result.add(this.initialIndex(yt, x), 1);
      // ymt1 times yt
    } else {
      result.add(this.labelXlabel(ytm1, yt, x), 1);
    }
    if (t == xseq.length - 1) {
      result.add(this.finalIndex(yt, x), 1);
    }
    // yt times x
    result.add(this.labelXinput(yt, x), 1);

    return result;
  }
View Full Code Here

    this.yAsize = yAlphabet.size();
  }

  // see SequenceFeatureFunction for java doc
  public SparseVector apply(SparseVector[] x, int[] y) {
    SparseVector result = apply(x, 0, y[0], 0);
    for (int t = 1; t < x.length; t++) {
      SparseVector fsubt = apply(x, y[t - 1], y[t], t);
      StaticUtils.plusEquals(result, fsubt);
    }
    return result;
  }
View Full Code Here

        yAsize * xAsize;
  }

  // see SequenceFeatureFunction for java doc
  public SparseVector apply(SparseVector[] x, int ytm1, int yt, int t) {
    SparseVector result = new SparseVector();
    // initial yt
    if (t == 0)
      result.add(initialIndex(yt), 1);
    // ymt1 times yt
    else
      result.add(labelXlabel(ytm1, yt), 1);
    if (t == x.length - 1)
      result.add(finalIndex(yt), 1);
    // yt times x
    for (int i = 0; i < x[t].numEntries(); i++) {
      result.add(labelXinput(yt, x[t].getIndexAt(i)), 1);
    }
    return result;
  }
View Full Code Here

    double wrongUpdate = Math.exp(alpha);
    double correctUpdate = Math.exp(-alpha);
    for (int instInd = 0; instInd < trainingData.size(); instInd++) {
      ClassificationInstance inst = trainingData.get(instInd);
      for (int y = 0; y < yAlphabet.size(); y++) {
        SparseVector fv = fxy.apply(inst.x, y);
        for (int i = 0; i < fv.numEntries(); i++) {
          if (fv.getIndexAt(i) == bestFeature) {
            if (y == inst.y)
              w[instInd] *= correctUpdate;
            else
              w[instInd] *= wrongUpdate;
          }
View Full Code Here

    }
    for (int instInd = 0; instInd < trainingData.size(); instInd++) {
      ClassificationInstance inst = trainingData.get(instInd);
      total += w[instInd];
      for (int y = 0; y < yAlphabet.size(); y++) {
        SparseVector fv = fxy.apply(inst.x, y);
        if (y == inst.y) {
          for (int i = 0; i < fv.numEntries(); i++) {
            correct[fv.getIndexAt(i)] += w[instInd];
          }
        } else {
          for (int i = 0; i < fv.numEntries(); i++) {
            wrongs[fv.getIndexAt(i)] += w[instInd];
          }
        }
      }
    }
    for (int i = 0; i < correct.length; i++) {
View Full Code Here

TOP

Related Classes of types.SparseVector

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.