qdtsne
A quick and dirty t-SNE C++ library
Loading...
Searching...
No Matches
initialize.hpp
Go to the documentation of this file.
1/*
2 *
3 * Copyright (c) 2014, Laurens van der Maaten (Delft University of Technology)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the Delft University of Technology.
16 * 4. Neither the name of the Delft University of Technology nor the names of
17 * its contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY LAURENS VAN DER MAATEN ''AS IS'' AND ANY EXPRESS
21 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
23 * EVENT SHALL LAURENS VAN DER MAATEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29 * OF SUCH DAMAGE.
30 *
31 */
32
33#ifndef QDTSNE_INITIALIZE_HPP
34#define QDTSNE_INITIALIZE_HPP
35
36#include "knncolle/knncolle.hpp"
37
38#include <vector>
39#include <stdexcept>
40
41#include "Status.hpp"
42#include "Options.hpp"
43#include "gaussian.hpp"
44#include "symmetrize.hpp"
45
51namespace qdtsne {
52
56namespace internal {
57
58template<int num_dim_, typename Index_, typename Float_>
59Status<num_dim_, Index_, Float_> initialize(NeighborList<Index_, Float_> nn, Float_ perp, const Options& options) {
60 compute_gaussian_perplexity(nn, perp, options.num_threads);
61 symmetrize_matrix(nn);
62 return Status<num_dim_, Index_, Float_>(std::move(nn), options);
63}
64
65}
84template<int num_dim_, typename Index_, typename Float_>
87 if (options.infer_perplexity && neighbors.size()) {
88 perp = static_cast<Float_>(neighbors.front().size())/3;
89 } else {
90 perp = options.perplexity;
91 }
92 return internal::initialize<num_dim_>(std::move(neighbors), perp, options);
93}
94
109template<int num_dim_, typename Dim_, typename Index_, typename Float_>
111 const Index_ K = perplexity_to_k(options.perplexity);
113 if (K >= N) {
114 throw std::runtime_error("number of observations should be greater than 3 * perplexity");
115 }
116
117 auto neighbors = find_nearest_neighbors(prebuilt, K, options.num_threads);
118 return internal::initialize<num_dim_>(std::move(neighbors), options.perplexity, options);
119}
120
138template<int num_dim_, typename Dim_, typename Index_, typename Float_>
149
150}
151
152#endif
Options for the t-SNE algorithm.
Status of the t-SNE iterations.
Status of the t-SNE iterations.
Definition Status.hpp:64
size_t num_observations() const
Definition Status.hpp:118
Quick and dirty t-SNE.
int perplexity_to_k(double perplexity)
Definition utils.hpp:44
Status< num_dim_, Index_, Float_ > initialize(NeighborList< Index_, Float_ > neighbors, const Options &options)
Definition initialize.hpp:85
Options for initialize().
Definition Options.hpp:14