@Override
 public Label predict(Instance instance) {
   Label l = null;
   if (instance.getLabel() instanceof ClassificationLabel || instance.getLabel() == null) {
     // ----------------- declare variables ------------------
     double lambda = 0.0;
     RealVector x_instance = new ArrayRealVector(matrixX.getColumnDimension(), 0);
     double result = 0.0;
     // -------------------------- initialize xi -------------------------
     for (int idx = 0; idx < matrixX.getColumnDimension(); idx++) {
       x_instance.setEntry(idx, instance.getFeatureVector().get(idx + 1));
     }
     // ------------------ get lambda -----------------------
     for (int j = 0; j < alpha.getDimension(); j++) {
       lambda += alpha.getEntry(j) * kernelFunction(matrixX.getRowVector(j), x_instance);
     }
     // ----------------- make prediction -----------------
     Sigmoid g = new Sigmoid(); // helper function
     result = g.value(lambda);
     l = new ClassificationLabel(result < 0.5 ? 0 : 1);
   } else {
     System.out.println("label type error!");
   }
   return l;
 }
예제 #2
0
  private static Intersection getIntersection(Ray ray, SphereObject obj, Model model) {
    RealMatrix transform = obj.getTransform();
    final RealMatrix transformInverse = obj.getTransformInverse();
    ray = ray.transform(transformInverse);
    Vector3D c = VectorUtils.toVector3D(obj.getCenter());
    Vector3D p0 = VectorUtils.toVector3D(ray.getP0());
    Vector3D p1 = VectorUtils.toVector3D(ray.getP1());
    float a = (float) p1.dotProduct(p1);
    Vector3D p0c = p0.subtract(c);
    float b = (float) p1.dotProduct(p0c) * 2.0f;

    float cc = (float) (p0c.dotProduct(p0c)) - obj.getSize() * obj.getSize();
    Double t = quadraticEquationRoot1(a, b, cc);
    if (t == null || t < EPSILON) {
      return new Intersection(false);
    }
    Intersection result = new Intersection(true);
    result.setObject(obj);
    final Vector3D p = p0.add(p1.scalarMultiply(t));
    RealVector pv = VectorUtils.toRealVector(p);
    pv.setEntry(3, 1.0);
    RealVector pt = transform.preMultiply(pv);
    result.setP(VectorUtils.toVector3D(pt));
    RealVector nv = pv.subtract(obj.getCenter());
    final RealVector nvt = transformInverse.transpose().preMultiply(nv);
    result.setN(VectorUtils.toVector3D(nvt).normalize());
    result.setDistance(t);
    return result;
  }
 @Override
 public void train(List<Instance> instances) {
   // ------------------------ initialize rows and columns ---------------------
   int rows = instances.size();
   int columns = 0;
   // get max columns
   for (Instance i : instances) {
     int localColumns = Collections.max(i.getFeatureVector().getFeatureMap().keySet());
     if (localColumns > columns) columns = localColumns;
   }
   // ------------------------ initialize alpha vector -----------------------
   alpha = new ArrayRealVector(rows, 0);
   // ------------------------ initialize base X and Y for use --------------------------
   double[][] X = new double[rows][columns];
   double[] Y = new double[rows];
   for (int i = 0; i < rows; i++) {
     Y[i] = ((ClassificationLabel) instances.get(i).getLabel()).getLabelValue();
     for (int j = 0; j < columns; j++) {
       X[i][j] = instances.get(i).getFeatureVector().get(j + 1);
     }
   }
   // ---------------------- gram matrix -------------------
   matrixX = new Array2DRowRealMatrix(X);
   RealMatrix gram = new Array2DRowRealMatrix(rows, rows);
   for (int i = 0; i < rows; i++) {
     for (int j = 0; j < rows; j++) {
       gram.setEntry(i, j, kernelFunction(matrixX.getRowVector(i), matrixX.getRowVector(j)));
     }
   }
   // ---------------------- gradient ascent --------------------------
   Sigmoid g = new Sigmoid(); // helper function
   System.out.println("Training start...");
   System.out.println(
       "Learning rate: " + _learning_rate + " Training times: " + _training_iterations);
   for (int idx = 0; idx < _training_iterations; idx++) {
     System.out.println("Training iteration: " + (idx + 1));
     for (int k = 0; k < rows; k++) {
       double gradient_ascent = 0.0;
       RealVector alpha_gram = gram.operate(alpha);
       for (int i = 0; i < rows; i++) {
         double lambda = alpha_gram.getEntry(i);
         double kernel = gram.getEntry(i, k);
         gradient_ascent =
             gradient_ascent
                 + Y[i] * g.value(-lambda) * kernel
                 + (1 - Y[i]) * g.value(lambda) * (-kernel);
       }
       alpha.setEntry(k, alpha.getEntry(k) + _learning_rate * gradient_ascent);
     }
   }
   System.out.println("Training done!");
 }
예제 #4
0
  @Override
  public RealVector computeSyntheticValues(SampleIterator it) {
    it.reset();
    Sample sample = it.next();
    int inputDim = sample.getEncodedInput().getDimension();
    int outputDim = sample.getEncodedOutput().getDimension();

    RealVector v = new ArrayRealVector(outputDim);
    for (int out = 0; out < outputDim; out++) {
      it.reset();
      double eta = generalizedCorrelationRatio(it, inputDim, out);
      if (eta < 0) return null;
      v.setEntry(out, eta);
    }

    return v;
  }