Skip to content

Commit

Permalink
Ability to skip random initialization so that starting positions can …
Browse files Browse the repository at this point in the history
…be set via Y
  • Loading branch information
Dominiek Ter Heide committed Mar 17, 2016
1 parent fc0bd63 commit 268ebb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions tsne.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@
using namespace std;

// Perform t-SNE
void TSNE::run(double* X, int N, int D, double* Y, int no_dims, double perplexity, double theta, int rand_seed) {
void TSNE::run(double* X, int N, int D, double* Y, int no_dims, double perplexity, double theta, int rand_seed, bool skip_random_init) {

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

// Determine whether we are using an exact algorithm
Expand Down Expand Up @@ -133,7 +135,9 @@ void TSNE::run(double* X, int N, int D, double* Y, int no_dims, double perplexit
else { for(int i = 0; i < row_P[N]; i++) val_P[i] *= 12.0; }

// Initialize solution (randomly)
for(int i = 0; i < N * no_dims; i++) Y[i] = randn() * .0001;
if (skip_random_init != true) {
for(int i = 0; i < N * no_dims; i++) Y[i] = randn() * .0001;
}

// Perform main training loop
if(exact) printf("Input similarities computed in %4.2f seconds!\nLearning embedding...\n", (float) (end - start) / CLOCKS_PER_SEC);
Expand Down Expand Up @@ -741,7 +745,7 @@ int main() {
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);
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);
Expand Down
2 changes: 1 addition & 1 deletion tsne.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static inline double sign(double x) { return (x == .0 ? .0 : (x < .0 ? -1.0 : 1.
class TSNE
{
public:
void run(double* X, int N, int D, double* Y, int no_dims, double perplexity, double theta, int rand_seed);
void run(double* X, int N, int D, double* Y, int no_dims, double perplexity, double theta, int rand_seed, bool skip_random_init);
bool load_data(double** data, int* n, int* d, int* no_dims, double* theta, double* perplexity, int* rand_seed);
void save_data(double* data, int* landmarks, double* costs, int n, int d);
void symmetrizeMatrix(unsigned int** row_P, unsigned int** col_P, double** val_P, int N); // should be static!
Expand Down

0 comments on commit 268ebb4

Please sign in to comment.