コード例 #1
0
ファイル: Tsne.java プロジェクト: n0mer/deeplearning4j
  /**
   * @param X
   * @param nDims
   * @param perplexity
   */
  public INDArray calculate(INDArray X, int nDims, double perplexity) {
    if (usePca) X = PCA.pca(X, Math.min(50, X.columns()), normalize);
    // normalization (don't normalize again after pca)
    if (normalize) {
      X.subi(X.min(Integer.MAX_VALUE));
      X = X.divi(X.max(Integer.MAX_VALUE));
      X = X.subiRowVector(X.mean(0));
    }

    if (nDims > X.columns()) nDims = X.columns();

    INDArray sumX = pow(X, 2).sum(1);

    INDArray D = X.mmul(X.transpose()).muli(-2).addRowVector(sumX).transpose().addRowVector(sumX);

    // output
    if (y == null) y = randn(X.rows(), nDims, Nd4j.getRandom()).muli(1e-3f);

    INDArray p = computeGaussianPerplexity(D, perplexity);

    // lie for better local minima
    p.muli(4);

    // init adagrad where needed
    if (useAdaGrad) {
      if (adaGrad == null) {
        adaGrad = new AdaGrad(y.shape());
        adaGrad.setMasterStepSize(learningRate);
      }
    }

    for (int i = 0; i < maxIter; i++) {
      step(p, i);

      if (i == switchMomentumIteration) momentum = finalMomentum;
      if (i == stopLyingIteration) p.divi(4);

      if (iterationListener != null) iterationListener.iterationDone(null, i);
    }

    return y;
  }