tsne 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0c56199fa52550bd32e634b4a0edb8d6d5b70295ae63a9378eca893797095083
4
+ data.tar.gz: cef96a9b2fee4bf0a156aaf32babbf6876dff0aae9ee1fde3d065601b70dc59c
5
+ SHA512:
6
+ metadata.gz: 403eb9b7c138165f4332c78c3eccd5c09abe91ca0b73b5d6f6aa5ba8adbff267c8e42938d55adf1dad7bbee23249c75d08923b516e3296536398d159ca0672fe
7
+ data.tar.gz: 4a4b1ad00b87dcd10817d5d61400eb6532e7a4d1e2128f1aa7ac163f974a994e3e5b3bea6750cf3d3506c61ff88a200356e1b43038291bcd85d36ed31c96f2f6
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.1.0 (2020-02-11)
2
+
3
+ - First release
data/LICENSE.txt ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020, Andrew Kane
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
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # t-SNE
2
+
3
+ High performance t-SNE for Ruby, powered by [Multicore t-SNE](https://github.com/DmitryUlyanov/tsne)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application’s Gemfile:
8
+
9
+ ```ruby
10
+ gem 'tsne'
11
+ ```
12
+
13
+ ## Getting Started
14
+
15
+ Prep your data
16
+
17
+ ```ruby
18
+ x = Numo::DFloat.cast([[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 1]])
19
+ ```
20
+
21
+ Run
22
+
23
+ ```ruby
24
+ tsne = TSNE.new
25
+ tsne.fit_transform(x)
26
+ ```
27
+
28
+ Get Kullback-Leibler divergence
29
+
30
+ ```ruby
31
+ tsne.kl_divergence
32
+ ```
33
+
34
+ ## Parameters
35
+
36
+ ```ruby
37
+ TSNE.new(
38
+ n_components: 2,
39
+ perplexity: 30.0,
40
+ early_exaggeration: 12,
41
+ learning_rate: 200,
42
+ n_iter: 1000,
43
+ n_iter_early_exag: 250,
44
+ verbose: 0,
45
+ random_state: -1,
46
+ angle: 0.5,
47
+ n_jobs: 1,
48
+ cheat_metric: true
49
+ )
50
+ ```
51
+
52
+ ## History
53
+
54
+ View the [changelog](https://github.com/ankane/tsne/blob/master/CHANGELOG.md)
55
+
56
+ ## Contributing
57
+
58
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
59
+
60
+ - [Report bugs](https://github.com/ankane/tsne/issues)
61
+ - Fix bugs and [submit pull requests](https://github.com/ankane/tsne/pulls)
62
+ - Write, clarify, or fix documentation
63
+ - Suggest or add new features
64
+
65
+ To get started with development:
66
+
67
+ ```sh
68
+ git clone https://github.com/ankane/tsne.git
69
+ cd tsne
70
+ bundle install
71
+ bundle exec rake vendor:all
72
+ bundle exec rake test
73
+ ```
data/lib/tsne/ffi.rb ADDED
@@ -0,0 +1,25 @@
1
+ module TSNE
2
+ module FFI
3
+ extend Fiddle::Importer
4
+
5
+ libs = Array(TSNE.ffi_lib).dup
6
+ begin
7
+ dlload Fiddle.dlopen(libs.shift)
8
+ rescue Fiddle::DLError => e
9
+ retry if libs.any?
10
+ raise e if ENV["TSNE_DEBUG"]
11
+ raise LoadError, "Could not find t-SNE"
12
+ end
13
+
14
+ typealias "bool", "char"
15
+
16
+ extern "void tsne_run_double(
17
+ double* X, int N, int D, double* Y,
18
+ int no_dims, double perplexity, double theta,
19
+ int num_threads, int max_iter, int n_iter_early_exag,
20
+ int random_state, bool init_from_Y, int verbose,
21
+ double early_exaggeration, double learning_rate,
22
+ double *final_error, int distance
23
+ )"
24
+ end
25
+ end
data/lib/tsne/model.rb ADDED
@@ -0,0 +1,48 @@
1
+ module TSNE
2
+ class Model
3
+ attr_reader :kl_divergence, :n_iter
4
+
5
+ def initialize(n_components: 2, perplexity: 30.0,
6
+ early_exaggeration: 12, learning_rate: 200,
7
+ n_iter: 1000, n_iter_early_exag: 250, verbose: 0,
8
+ random_state: -1, angle: 0.5, n_jobs: 1, cheat_metric: true)
9
+
10
+ @n_components = n_components
11
+ @perplexity = perplexity
12
+ @early_exaggeration = early_exaggeration
13
+ @learning_rate = learning_rate
14
+ @n_iter = n_iter
15
+ @n_iter_early_exag = n_iter_early_exag
16
+ @verbose = verbose
17
+ @random_state = random_state
18
+ @angle = angle
19
+ @n_jobs = n_jobs
20
+ @cheat_metric = cheat_metric
21
+ end
22
+
23
+ def fit(x)
24
+ fit_transform(x)
25
+ self
26
+ end
27
+
28
+ def fit_transform(x)
29
+ x = Numo::DFloat.cast(x) unless x.is_a?(Numo::NArray)
30
+ x = x.cast_to(Numo::DFloat) unless x.is_a?(Numo::DFloat)
31
+ n, d = x.shape
32
+
33
+ x = Fiddle::Pointer[x.to_binary]
34
+ y = Fiddle::Pointer.malloc(n * @n_components * Fiddle::SIZEOF_DOUBLE)
35
+ final_error = Fiddle::Pointer.malloc(Fiddle::SIZEOF_DOUBLE)
36
+
37
+ FFI.tsne_run_double(
38
+ x, n, d, y, @n_components, @perplexity, @angle, @n_jobs,
39
+ @n_iter, @n_iter_early_exag, @random_state, 0, @verbose, @early_exaggeration,
40
+ @learning_rate, final_error, @cheat_metric ? 1 : 0
41
+ )
42
+
43
+ @kl_divergence = final_error[0, final_error.size].unpack1("d")
44
+
45
+ Numo::DFloat.from_binary(y[0, y.size]).reshape(n, @n_components)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module TSNE
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tsne.rb ADDED
@@ -0,0 +1,34 @@
1
+ # dependencies
2
+ require "numo/narray"
3
+
4
+ # stdlib
5
+ require "fiddle/import"
6
+
7
+ # modules
8
+ require "tsne/model"
9
+ require "tsne/version"
10
+
11
+ module TSNE
12
+ class Error < StandardError; end
13
+
14
+ class << self
15
+ attr_accessor :ffi_lib
16
+ end
17
+ lib_name =
18
+ if Gem.win_platform?
19
+ "tsne_multicore.dll"
20
+ elsif RbConfig::CONFIG["host_os"] =~ /darwin/i
21
+ "libtsne_multicore.dylib"
22
+ else
23
+ "libtsne_multicore.so"
24
+ end
25
+ vendor_lib = File.expand_path("../vendor/#{lib_name}", __dir__)
26
+ self.ffi_lib = [vendor_lib]
27
+
28
+ # friendlier error message
29
+ autoload :FFI, "tsne/ffi"
30
+
31
+ def self.new(*args, **options)
32
+ Model.new(*args, **options)
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2014, Laurens van der Maaten (Delft University of Technology)
4
+ Copyright (c) 2016, Dmitry Ulyanov
5
+ All rights reserved.
6
+
7
+ Redistribution and use in source and binary forms, with or without
8
+ modification, are permitted provided that the following conditions are met:
9
+
10
+ * Redistributions of source code must retain the above copyright notice, this
11
+ list of conditions and the following disclaimer.
12
+
13
+ * Redistributions in binary form must reproduce the above copyright notice,
14
+ this list of conditions and the following disclaimer in the documentation
15
+ and/or other materials provided with the distribution.
16
+
17
+ * Neither the name of the copyright holder nor the names of its
18
+ contributors may be used to endorse or promote products derived from
19
+ this software without specific prior written permission.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Binary file
Binary file
Binary file
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tsne
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '5'
69
+ description:
70
+ email: andrew@chartkick.com
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - CHANGELOG.md
76
+ - LICENSE.txt
77
+ - README.md
78
+ - lib/tsne.rb
79
+ - lib/tsne/ffi.rb
80
+ - lib/tsne/model.rb
81
+ - lib/tsne/version.rb
82
+ - vendor/LICENSE.txt
83
+ - vendor/libtsne_multicore.dylib
84
+ - vendor/libtsne_multicore.so
85
+ - vendor/tsne_multicore.dll
86
+ homepage: https://github.com/ankane/tsne
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '2.4'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubygems_version: 3.1.2
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: High performance t-SNE for Ruby
109
+ test_files: []