예제 #1
0
void FFN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Predict(arma::mat& predictors, arma::mat& responses)
{
  deterministic = true;

  arma::mat responsesTemp;
  ResetParameter(network);
  Forward(arma::mat(predictors.colptr(0), predictors.n_rows, 1, false, true),
      network);
  OutputPrediction(responsesTemp, network);

  responses = arma::mat(responsesTemp.n_elem, predictors.n_cols);
  responses.col(0) = responsesTemp.col(0);

  for (size_t i = 1; i < predictors.n_cols; i++)
  {
    Forward(arma::mat(predictors.colptr(i), predictors.n_rows, 1, false, true),
        network);

    responsesTemp = arma::mat(responses.colptr(i), responses.n_rows, 1, false,
        true);
    OutputPrediction(responsesTemp, network);
    responses.col(i) = responsesTemp.col(0);
  }
}
예제 #2
0
void RNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Predict(arma::mat& predictors, arma::mat& responses)
{
  arma::mat responsesTemp;
  SinglePredict(arma::mat(predictors.colptr(0), predictors.n_rows,
      1, false, true), responsesTemp);

  responses = arma::mat(responsesTemp.n_elem, predictors.n_cols);
  responses.col(0) = responsesTemp.col(0);

  for (size_t i = 1; i < predictors.n_cols; i++)
  {
    SinglePredict(arma::mat(predictors.colptr(i), predictors.n_rows,
      1, false, true), responsesTemp);
    responses.col(i) = responsesTemp.col(0);
  }
}
예제 #3
0
void CF<FactorizerType>::InsertNeighbor(const size_t queryIndex,
                                        const size_t pos,
                                        const size_t neighbor,
                                        const double value,
                                        arma::Mat<size_t>& recommendations,
                                        arma::mat& values) const
{
  // We only memmove() if there is actually a need to shift something.
  if (pos < (recommendations.n_rows - 1))
  {
    const int len = (values.n_rows - 1) - pos;
    memmove(values.colptr(queryIndex) + (pos + 1),
        values.colptr(queryIndex) + pos,
        sizeof(double) * len);
    memmove(recommendations.colptr(queryIndex) + (pos + 1),
        recommendations.colptr(queryIndex) + pos,
        sizeof(size_t) * len);
  }

  // Now put the new information in the right index.
  values(pos, queryIndex) = value;
  recommendations(pos, queryIndex) = neighbor;
}
예제 #4
0
void LSHSearch<SortPolicy>::InsertNeighbor(arma::mat& distances,
                                           arma::Mat<size_t>& neighbors,
                                           const size_t queryIndex,
                                           const size_t pos,
                                           const size_t neighbor,
                                           const double distance)
{
  // We only memmove() if there is actually a need to shift something.
  if (pos < (distances.n_rows - 1))
  {
    const size_t len = (distances.n_rows - 1) - pos;
    memmove(distances.colptr(queryIndex) + (pos + 1),
        distances.colptr(queryIndex) + pos,
        sizeof(double) * len);
    memmove(neighbors.colptr(queryIndex) + (pos + 1),
        neighbors.colptr(queryIndex) + pos,
        sizeof(size_t) * len);
  }

  // Now put the new information in the right index.
  distances(pos, queryIndex) = distance;
  neighbors(pos, queryIndex) = neighbor;
}
예제 #5
0
void FastMKS<KernelType, TreeType>::InsertNeighbor(arma::Mat<size_t>& indices,
                                                   arma::mat& products,
                                                   const size_t queryIndex,
                                                   const size_t pos,
                                                   const size_t neighbor,
                                                   const double distance)
{
  // We only memmove() if there is actually a need to shift something.
  if (pos < (products.n_rows - 1))
  {
    int len = (products.n_rows - 1) - pos;
    memmove(products.colptr(queryIndex) + (pos + 1),
        products.colptr(queryIndex) + pos,
        sizeof(double) * len);
    memmove(indices.colptr(queryIndex) + (pos + 1),
        indices.colptr(queryIndex) + pos,
        sizeof(size_t) * len);
  }

  // Now put the new information in the right index.
  products(pos, queryIndex) = distance;
  indices(pos, queryIndex) = neighbor;
}
예제 #6
0
파일: cnn_impl.hpp 프로젝트: GYGit/mlpack
void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Predict(arma::cube& predictors, arma::mat& responses)
{
  deterministic = true;

  arma::mat responsesTemp;
  ResetParameter(network);
  Forward(predictors.slices(0, sampleSize - 1), network);
  OutputPrediction(responsesTemp, network);

  responses = arma::mat(responsesTemp.n_elem, predictors.n_slices);
  responses.col(0) = responsesTemp.col(0);

  for (size_t i = 1; i < (predictors.n_slices / sampleSize); i++)
  {
    Forward(predictors.slices(i, (i + 1) * sampleSize - 1), network);

    responsesTemp = arma::mat(responses.colptr(i), responses.n_rows, 1, false,
        true);
    OutputPrediction(responsesTemp, network);
    responses.col(i) = responsesTemp.col(0);
  }
}
예제 #7
0
void Distribution::generate_distribution3D(arma::mat& dist,
        int n_p,
        double bin_edge,
        int N,
        bool rerun) {

    int x_i, y_i, z_i, r_i, n, n_tot;
    double x, y, z, r, dr, dr_R, stretch, mean_r;

    int dim = 3;
    
    using namespace arma;

    ucube distribution(N, N, N);
    uvec radial_dist = zeros<uvec > (N);
    mat tot_dist;

    vec R = arma::sqrt(sum(dist % dist, 1));

    n = dist.n_rows;

#ifdef MPI_ON
    ivec n_list = zeros<ivec > (n_nodes);

    MPI_Allgather(&n, 1, MPI_INT, n_list.memptr(), 1, MPI_INT, MPI_COMM_WORLD);

    n_tot = accu(n_list);
#else
    n_tot = n;
#endif

    detect_deadlock(dist, n_p, dim, n);

    //On fly calculation during QMC initialized by a binedge 0.
    if (bin_edge == 0) {
        stretch = 3;

        //calculate the bin edge and size based on the mean radius
        if (!locked) {
            mean_r = ErrorEstimator::combine_mean(mean(R), n, n_tot);
        } else {

            mean_r = 0;
            int k = 0;

            for (int i = 0; i < n; i++) {
                if (is_deadlocked(dist, dim, i)) continue;

                mean_r += R(i);
                k++;
            }

            mean_r = ErrorEstimator::combine_mean(mean_r / k, n, n_tot);

        }
        bin_edge = stretch*mean_r;
        //        if (node==0) cout << "pre " << mean_r << endl;
    }

    dr = 2 * bin_edge / (N - 1);

    for (int ni = 0; ni < n; ni++) {

        if (is_deadlocked(dist, dim, ni)) continue;

        x = dist(ni, 0);
        y = dist(ni, 1);
        z = dist(ni, 2);

        x_i = (N / 2 + int(x / dr))*(x > 0) + (N / 2 + int(x / dr) - 1)*(x <= 0);
        y_i = (N / 2 + int(y / dr))*(y > 0) + (N / 2 + int(y / dr) - 1)*(y <= 0);
        z_i = (N / 2 + int(z / dr))*(z > 0) + (N / 2 + int(z / dr) - 1)*(z <= 0);

        if (x_i < 0 || x_i >= N) {
            continue;
        } else if (y_i < 0 || y_i >= N) {
            continue;
        } else if (z_i < 0 || z_i >= N) {
            continue;
        }

        distribution(x_i, y_i, z_i)++;

    }

    dr_R = dr / 2;
    for (int ni = 0; ni < n; ni++) {

        if (is_deadlocked(dist, dim, ni)) continue;

        r = R(ni);

        r_i = r / dr_R;

        if (r_i >= N) {
            continue;
        }

        radial_dist(r_i)++;

    }




#ifdef MPI_ON
    if (node == 0) {

        MPI_Reduce(MPI_IN_PLACE, distribution.memptr(), N * N*N, MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD);
        MPI_Reduce(MPI_IN_PLACE, radial_dist.memptr(), N, MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD);

        if (!rerun) {

            ivec displs = zeros<ivec > (n_nodes);

            double displ_sum = 0;
            for (int j = 0; j < n_nodes; j++) {
                displs(j) = displ_sum;
                displ_sum += n_list(j);
            }

            tot_dist = zeros<mat > (n_tot, dim);

            for (int i = 0; i < dim; i++) {
                MPI_Gatherv(dist.colptr(i), n, MPI_DOUBLE,
                        tot_dist.colptr(i), n_list.memptr(), displs.memptr(),
                        MPI_DOUBLE, 0, MPI_COMM_WORLD);
            }

            //            cout << as_scalar(mean(arma::sqrt(sum(tot_dist % tot_dist, 1)))) << endl;

            displs.reset();

        }

    } else {
        MPI_Reduce(distribution.memptr(), NULL, N * N*N, MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD);
        MPI_Reduce(radial_dist.memptr(), NULL, N, MPI_UNSIGNED, MPI_SUM, 0, MPI_COMM_WORLD);

        if (!rerun) {
            for (int i = 0; i < dim; i++) {
                MPI_Gatherv(dist.colptr(i), n, MPI_DOUBLE,
                        NULL, NULL, NULL,
                        NULL, 0, MPI_COMM_WORLD);
            }
        }

        radial_dist.reset();
        distribution.reset();
    }
#else
    tot_dist(dist.memptr(), n, dim, false, true);
#endif

    n_list.reset();
    R.reset();

    if (node == 0) {

        if (!silent) cout << "3D Distribution calculated using " << n_tot << " samples." << endl;

        cube normalized_dist = conv_to< cube>::from(distribution);
        vec normalized_radd = conv_to< vec>::from(radial_dist);

        vec radial_axis = linspace(0, bin_edge, N);

        normalized_dist *= n_p / (accu(normalized_dist) * dr * dr * dr);


        //project out a symmetric axis and normalize (skip singularity)
        //        normalized_radd(span(1, N - 1)) /= radial_axis(span(1, N - 1));
        //
        //        normalized_radd(span(1, N - 1)) /= radial_axis(span(1, N - 1));
        //
        //        normalized_radd(0) = normalized_radd(1);

        //        cout << normalized_radd.max()/(2*datum::pi*accu(normalized_radd)*dr_R) << endl;
        normalized_radd *= n_p / (accu(normalized_radd) * dr_R);


        s << path << "walker_positions/dist_out_" << name + suffix << "_edge" << bin_edge << ".arma3D";
        normalized_dist.save(s.str());
        normalized_dist.reset();
        s.str(std::string());

        if (!rerun) {
            s << path << "walker_positions/dist_rawdata_" << name + suffix << ".arma";
            tot_dist.save(s.str());
            tot_dist.reset();
            s.str(std::string());
        }

        s << path << "walker_positions/radial_out_" << name + suffix << "_edge" << bin_edge << ".arma";
        normalized_radd.save(s.str());
        normalized_radd.reset();
        s.str(std::string());

        radial_axis.reset();
        radial_dist.reset();
        distribution.reset();

    }

}