Ejemplo n.º 1
0
  /**
   * 图象压缩
   *
   * @throws IOException
   */
  public static void part2() throws IOException {
    tic();

    logger.info("加载数据...\n");
    String path = COURSE_ML_PATH + "/ex7/bird_small.png";
    MatrixImage mi = ImageLoader.load(path);
    FloatMatrix X = mi.getMatrix();

    logger.info("模型初始化...\n");
    KMeans kMeans = new KMeans(X, 16);

    logger.info("执行训练...\n");
    FloatMatrix centroids = kMeans.run(10);

    logger.info("运行完毕.\n聚类中心如下:\n{}\n", centroids);

    logger.info("图象对比...\n");
    FloatMatrix indices = kMeans.findClosestCentroids();
    int[] index = indices.toIntArray();
    FloatMatrix result = centroids.getRows(index);

    ImShow.show(mi);

    mi.setMatrix(result);
    ImShow.show(mi);
    toc();
  }
Ejemplo n.º 2
0
  @Override
  public Object compute(FloatMatrix params, int flag) {

    x = params.getRange(0, rows * features);
    FloatMatrix theta = params.getRange(rows * features, params.length);

    x = x.reshape(rows, features);
    theta = theta.reshape(columns, features);

    if (flag == 1 || flag == 3) {
      FloatMatrix M = MatrixFunctions.pow(x.mmul(theta.transpose()).sub(y), 2);
      this.cost = M.mul(r).columnSums().rowSums().get(0) / 2;

      if (lambda != 0) {
        float cost1 =
            (lambda / 2)
                * (MatrixFunctions.pow(theta, 2).columnSums().rowSums().get(0)
                    + MatrixFunctions.pow(x, 2).columnSums().rowSums().get(0));
        this.cost += cost1;
      }
    }

    if (flag == 2 || flag == 3) {

      FloatMatrix xGrad = FloatMatrix.zeros(x.rows, x.columns);
      FloatMatrix thetaGrad = FloatMatrix.zeros(theta.rows, theta.columns);

      int[] indices;
      FloatMatrix thetaTemp;
      FloatMatrix xTemp;
      FloatMatrix yTemp;
      for (int i = 0; i < rows; i++) {
        indices = r.getRow(i).eq(1).findIndices();
        if (indices.length == 0) continue;

        thetaTemp = theta.getRows(indices);
        yTemp = y.getRow(i).get(indices);
        xGrad.putRow(i, x.getRow(i).mmul(thetaTemp.transpose()).sub(yTemp).mmul(thetaTemp));
      }
      xGrad = xGrad.add(x.mmul(lambda));

      for (int i = 0; i < columns; i++) {
        indices = r.getColumn(i).eq(1).findIndices();
        if (indices.length == 0) continue;

        xTemp = x.getRows(indices);
        yTemp = y.getColumn(i).get(indices);
        thetaGrad.putRow(
            i, xTemp.mmul(theta.getRow(i).transpose()).sub(yTemp).transpose().mmul(xTemp));
      }
      thetaGrad = thetaGrad.add(theta.mmul(lambda));

      this.gradient = MatrixUtil.merge(xGrad.data, thetaGrad.data);
    }

    return flag == 1 ? cost : gradient;
  }
Ejemplo n.º 3
0
 public void pmmuli(FloatMatrix self, FloatMatrix other, FloatMatrix result) {
   pool.invoke(new MulitplyPartly(self, other, result, 0, self.getRows(), 0, other.getColumns()));
 }