Exemplo n.º 1
0
  @Override
  public double[][] getKernelMatrix(List<TrainingSample<T>> list) {
    final List<TrainingSample<T>> l = list;
    // init matrix with ones
    double matrix[][] = new double[l.size()][l.size()];
    for (double[] lines : matrix) Arrays.fill(lines, 1.);

    for (final Kernel<T> k : kernels.keySet()) {
      final double w = kernels.get(k);

      // check w
      if (w == 0) continue;

      final double[][] m = k.getKernelMatrix(l);
      // specific factory
      ThreadedMatrixOperator tmo =
          new ThreadedMatrixOperator() {

            @Override
            public void doLine(int index, double[] line) {

              for (int i = line.length - 1; i >= 0; i--) {
                line[i] *= Math.pow(m[index][i], w);
              }
            };
          };

      tmo.getMatrix(matrix);
    }
    return matrix;
  }
Exemplo n.º 2
0
  public IndexedCacheKernel(Kernel<T> k, final Map<S, T> signatures) {
    this.kernel = k;

    matrix = new double[signatures.size()][signatures.size()];

    // adding index
    map = new HashMap<S, Integer>(signatures.size());
    int index = 0;
    for (S s : signatures.keySet()) {
      map.put(s, index);
      index++;
    }

    // computing matrix
    ThreadedMatrixOperator factory =
        new ThreadedMatrixOperator() {
          @Override
          public void doLine(int index, double[] line) {
            // reverse search through mapping S <-> index
            S s1 = null;
            for (S s : map.keySet())
              if (map.get(s) == index) {
                s1 = s;
                break;
              }
            // mapped signature
            T t1 = signatures.get(s1);

            // all mapping S <-> T
            for (S s2 : map.keySet()) {
              // get index of s2
              int j = map.get(s2);
              // get signature of s2
              T t2 = signatures.get(s2);
              // add value of kernel
              line[j] = kernel.valueOf(t1, t2);
            }
          };
        };

    /* do the actuel computing of the matrix */
    matrix = factory.getMatrix(matrix);
  }