Ejemplo n.º 1
0
// Function that runs the Barnes-Hut implementation of t-SNE
int main() {

    // Define some variables
	int origN, N, D, no_dims, *landmarks;
	double perc_landmarks;
	double perplexity, theta, *data;
    int rand_seed = -1;
    TSNE* tsne = new TSNE();

    // Read the parameters and the dataset
	if(tsne->load_data(&data, &origN, &D, &no_dims, &theta, &perplexity, &rand_seed)) {

		// Make dummy landmarks
        N = origN;
        int* landmarks = (int*) malloc(N * sizeof(int));
        if(landmarks == NULL) { printf("Memory allocation failed!\n"); exit(1); }
        for(int n = 0; n < N; n++) landmarks[n] = n;

		// Now fire up the SNE implementation
		double* Y = (double*) malloc(N * no_dims * sizeof(double));
		double* costs = (double*) calloc(N, sizeof(double));
        if(Y == NULL || costs == NULL) { printf("Memory allocation failed!\n"); exit(1); }
		tsne->run(data, N, D, Y, no_dims, perplexity, theta, rand_seed, false);

		// Save the results
		tsne->save_data(Y, landmarks, costs, N, no_dims);

        // Clean up the memory
		free(data); data = NULL;
		free(Y); Y = NULL;
		free(costs); costs = NULL;
		free(landmarks); landmarks = NULL;
    }
    delete(tsne);
}
Ejemplo n.º 2
0
// Function that runs the Barnes-Hut implementation of t-SNE
int main(int argc, char** argv) {
  int metrics = 0;
  if (argc > 1) {
    if (!strcmp(argv[1], "1")) metrics = 1;
  }

  // Define some variables
  int origN, N, D, no_dims = 2, *landmarks;
  double perc_landmarks;
  double perplexity, theta, *data;
  TSNE* tsne = new TSNE();
  
  // Read the parameters and the dataset
  if(tsne->load_data(&data, &origN, &D, &theta, &perplexity)) {
    
    // Make dummy landmarks
    N = origN;
    int* landmarks = (int*) malloc(N * sizeof(int));
    if(landmarks == NULL) { printf("Memory allocation failed!\n"); exit(1); }
    for(int n = 0; n < N; n++) landmarks[n] = n;
    
    // Now fire up the SNE implementation
    double* Y = (double*) malloc(N * no_dims * sizeof(double));
    double* costs = (double*) calloc(N, sizeof(double));
    if(Y == NULL || costs == NULL) { printf("Memory allocation failed!\n"); exit(1); }
    double (*metric_func) (const DataPoint&, const DataPoint&);
    switch (metrics) {
    case 1: metric_func = log_cosine_distance; printf("Using %s As Distance Metrics.\n", "Log-Cosine-Similarity"); break;
    default: metric_func = euclidean_distance; printf("Using %s As Distance Metrics.\n", "Euclidean-Distance"); break;
    }
    tsne->run(data, N, D, Y, no_dims, perplexity, theta, metric_func);
    
    // Save the results
    tsne->save_data(Y, landmarks, costs, N, no_dims);
    
    // Clean up the memory
    free(data); data = NULL;
    free(Y); Y = NULL;
    free(costs); costs = NULL;
    free(landmarks); landmarks = NULL;
  }
  delete(tsne);
}
Ejemplo n.º 3
0
// Function that runs the Barnes-Hut implementation of t-SNE
int main() {

	// Define some variables
	int origN, N, D, no_dims, iterations, *landmarks;
	double perc_landmarks;
	double perplexity, theta, eta, *data;
	float gpu_mem;
	int verbose;
	int rand_seed = -1;
	int seed = 0;
	TSNE* tsne = new TSNE();

	time_t start = clock();
	// Read the parameters and the dataset
	if (tsne->load_data(&data, &origN, &D, &no_dims, &theta, &perplexity, &eta, &iterations, &seed, &gpu_mem, &verbose, &rand_seed)) {

		// Set random seed
		if (rand_seed >= 0) {
			if (verbose > 0) printf("Using random seed: %d\n", rand_seed);
			srand((unsigned int)rand_seed);
		}
		else {
			if (verbose > 0) printf("Using current time as random seed...\n");
			srand(time(NULL));
		}

		// Make dummy landmarks
		N = origN;
		int* landmarks = (int*)malloc(N * sizeof(int));
		if (landmarks == NULL) { printf("Memory allocation failed!\n"); exit(1); }
		for (int n = 0; n < N; n++) landmarks[n] = n;

		// Now fire up the SNE implementation
		double* Y = (double*)malloc(N * no_dims * sizeof(double));
		double* costs = (double*)calloc(N, sizeof(double));
		if (Y == NULL || costs == NULL) { printf("Memory allocation failed!\n"); exit(1); }

		tsne->zeroMean(data, N, D);
		tsne->normalize(data, N, D);
		if (seed == 0){
			seed = origN;
		}
		tsne->runWithCenterOfMass(data, Y, origN, seed, D, no_dims, perplexity, theta, eta, iterations, gpu_mem, verbose);

		// Save the results
		tsne->save_data(Y, landmarks, costs, N, no_dims, verbose);

		// Clean up the memory
		free(data); data = NULL;
		free(Y); Y = NULL;
		free(costs); costs = NULL;
		free(landmarks); landmarks = NULL;
	}
	delete(tsne);
	time_t end = clock();
	if (verbose > 0) printf("T-sne required %f seconds (%f minutes) to run\n", float(end - start) / CLOCKS_PER_SEC, float(end - start) / (60 * CLOCKS_PER_SEC));

}
Ejemplo n.º 4
0
void TsneProjector::Project(vector<vector<float>>& values, vector<vector<float>>& proj_values) {
    int N = values[0].size();
    int D = values.size();
    int no_dims = 2;
    double perplexity = 30;
    if (perplexity > values[0].size() / 5) perplexity = values[0].size() / 5;
    double theta = 0.5;
    int rand_seed = 12345;
    bool skip_random_init = false;

    proj_values.resize(no_dims);
    for (int i = 0; i < no_dims; ++i)
        proj_values[i].resize(N);

    vector<double> X, Y;
    X.resize(N * D);
    Y.resize(no_dims * N);

    int accu_count = 0;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < D; ++j) {
            X[accu_count] = values[j][i];
            accu_count++;
        }
    }

    TSNE sne;
    sne.run(X.data(), N, D, Y.data(), no_dims, perplexity, theta, rand_seed, skip_random_init);

    accu_count = 0;
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < no_dims; ++j) {
            proj_values[j][i] = Y[accu_count] * 1000;
            accu_count++;
        }
    }
}
Ejemplo n.º 5
0
// Function that runs the Barnes-Hut implementation of t-SNE
// [[Rcpp::export]]
Rcpp::List Rtsne_cpp(NumericMatrix X, int no_dims_in, double perplexity_in, double theta_in, bool verbose, int max_iter, bool distance_precomputed, NumericMatrix Y_in, bool init) {

  int origN, N, D, no_dims = no_dims_in;

	double  *data;
  TSNE* tsne = new TSNE();
  double perplexity = perplexity_in;
  double theta = theta_in;

  origN = X.nrow();
  D = X.ncol();
    
	data = (double*) calloc(D * origN, sizeof(double));
    if(data == NULL) { Rcpp::stop("Memory allocation failed!\n"); }
    for (int i = 0; i < origN; i++){
        for (int j = 0; j < D; j++){
            data[i*D+j] = X(i,j);
        }
    }
    
    // Make dummy landmarks
    N = origN;
    if (verbose) Rprintf("Read the %i x %i data matrix successfully!\n", N, D);
    int* landmarks = (int*) malloc(N * sizeof(int));
    if(landmarks == NULL) { Rcpp::stop("Memory allocation failed!\n"); }
    for(int n = 0; n < N; n++) landmarks[n] = n;

		double* Y = (double*) malloc(N * no_dims * sizeof(double));
		double* costs = (double*) calloc(N, sizeof(double));
		double* itercosts = (double*) calloc((int)(ceil(max_iter/50.0)), sizeof(double));
    if(Y == NULL || costs == NULL) { Rcpp::stop("Memory allocation failed!\n"); }
    
    // Initialize solution (randomly)
    if (init) {
      for (int i = 0; i < N; i++){
        for (int j = 0; j < no_dims; j++){
          Y[i*no_dims+j] = Y_in(i,j);
        }
      }
      if (verbose) Rprintf("Using user supplied starting positions\n");
    }
    
    // Run tsne
		tsne->run(data, N, D, Y, no_dims, perplexity, theta, verbose, max_iter, costs, distance_precomputed, itercosts, init);

  	// Save the results
    Rcpp::NumericMatrix Yr(N, no_dims);
    for (int i = 0; i < N; i++){
        for (int j = 0; j < no_dims; j++){
            Yr(i,j) = Y[i*no_dims+j];
        }
    }
    
    Rcpp::NumericVector costsr(N);
    for (int i = 0; i < N; i++){
      costsr(i) = costs[i];
    }
    Rcpp::NumericVector itercostsr((int)(ceil(max_iter/50.0)));
    for (int i = 0; i < (int)(ceil(max_iter/50.0)); i++) {
      itercostsr(i) = itercosts[i];
    }
    
    free(data); data = NULL;
  	free(Y); Y = NULL;
		free(costs); costs = NULL;
		free(landmarks); landmarks = NULL;
    delete(tsne);
    
    Rcpp::List output = Rcpp::List::create(Rcpp::_["theta"]=theta, Rcpp::_["perplexity"]=perplexity, Rcpp::_["N"]=N,Rcpp::_["origD"]=D,Rcpp::_["Y"]=Yr, Rcpp::_["costs"]=costsr, Rcpp::_["itercosts"]=itercostsr);
    return output; 
}