ruby-cntk 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,21 @@
1
+ require "rbconfig"
2
+
3
+ #RbConfig::MAKEFILE_CONFIG["CXX"] = "clang++-3.8"
4
+
5
+ require 'mkmf'
6
+
7
+
8
+
9
+
10
+ have_library("c++") or have_library("stdc++")
11
+
12
+ # have_library("cntklibrary-2.0", nil, nil, " -L /cntk/build/cpu/release/lib/ ")
13
+ dir_config("cntklibrary-2.0")
14
+ have_library("cntklibrary-2.0")
15
+
16
+ # rake2.3 compile -- --with-cntklibrary-2.0-lib=/cntk/build/cpu/release/lib/ --with-cntklibrary-2.0-include=/cntk/Source/CNTKv2LibraryDll/API/
17
+ # rake2.3 compile -- --with-cntklibrary-2.0-lib=/cntk/cntk/lib/ --with-cntklibrary-2.0-include=/cntk/Include/
18
+ $CXXFLAGS = ($CXXFLAGS || "") + " -std=c++11 -O2 -DSWIG "
19
+ # $LDFLAGS = ($LDFLAGS || "") + ""
20
+
21
+ create_makefile('cntk/CNTK')
@@ -0,0 +1,68 @@
1
+ module CNTK
2
+ class Function
3
+
4
+ def call(args)
5
+ if args.outputs.length == 1
6
+ return replace_placeholders({placeholders[0] => args.output})
7
+ else
8
+ raise "not implemented"
9
+ end
10
+ end
11
+
12
+ def forward(*args)
13
+ if args.length > 1
14
+ return __forward__(*args)
15
+ elsif args.length == 1
16
+ input = convert_to_value(args[0])
17
+ out = StdUMapVariableValue.new()
18
+ outputs().each{|o|
19
+ v = NDArrayView.new(CNTK::DataType_Double,
20
+ required_output_shape(o),
21
+ required_output_buf(o),
22
+ CNTK::DeviceDescriptor.default_device(),
23
+ true)
24
+ out[o] = Value.new(v)
25
+ }
26
+ b = __forward__(input, out)
27
+ out = remove_dynamic_axes(out)
28
+ return [out, b]
29
+ end
30
+ end
31
+
32
+ def convert_to_value(h)
33
+ input = {}
34
+ h.each_pair{|k,val|
35
+ if val.respond_to?(:row_major?)
36
+ input[k] = Value.create(val)
37
+ else
38
+ input[k] = val
39
+ end
40
+ }
41
+ return input
42
+ end
43
+
44
+ #FIXME
45
+ # we must add dynamic axes?
46
+ def required_output_shape(ov)
47
+ ov.shape().to_a + [1,1]
48
+ end
49
+
50
+ def required_output_buf(ov)
51
+ [1.0] * ov.shape.total_size
52
+ end
53
+
54
+ def remove_dynamic_axes(out)
55
+ out1 = {}
56
+ out.each{|o,ov|
57
+ if ov.shape.rank == o.shape.rank + 2 and ov.shape.to_a[-2..-1] == [1,1]
58
+ out1[o] = ov.reshape( ov.shape.to_a[0..-3] )
59
+ else
60
+ out1[o] = ov
61
+ end
62
+ }
63
+ return out1
64
+ end
65
+
66
+ private :__forward__
67
+ end
68
+ end
@@ -0,0 +1,27 @@
1
+ module CNTK
2
+ class NDArrayView
3
+
4
+ def self.create(a)
5
+ if a.respond_to?(:shape) and a.respond_to?(:row_major?)
6
+ if a.row_major?
7
+ # NDArrayView is column-major.
8
+ # So we must transpose a.
9
+ ta = a.transpose
10
+ end
11
+ return self.new(DataType_Double, a.shape, ta.flatten.to_a,
12
+ CNTK::DeviceDescriptor.default_device(), false)
13
+ else
14
+ raise "not implemented"
15
+ end
16
+ end
17
+
18
+ def to_narray
19
+ ret = Numo::DFloat[*to_vec()]
20
+ # NDArrayView is column-major and NArray is row-major.
21
+ # So we must reverse shape and transpose it.
22
+ ret = ret.reshape(*shape().reverse)
23
+ return ret.transpose
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module CNTK
2
+ class NDShape
3
+ alias :to_ary :dimensions
4
+ alias :to_a :dimensions
5
+
6
+ def reverse
7
+ to_ary.reverse
8
+ end
9
+ end
10
+ end
data/lib/cntk/value.rb ADDED
@@ -0,0 +1,18 @@
1
+ module CNTK
2
+ class Value
3
+
4
+ def self.create(a)
5
+ new(NDArrayView.create(a))
6
+ end
7
+
8
+ def to_narray
9
+ data().to_narray
10
+ end
11
+
12
+ def reshape(a)
13
+ na = to_narray().reshape(*a)
14
+ self.class.create(na)
15
+ end
16
+
17
+ end
18
+ end
data/lib/cntk.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "cntk/CNTK"
2
+ require "cntk/function"
3
+ require "cntk/ndshape"
4
+ require "cntk/ndarrayview"
5
+ require "cntk/value"
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-cntk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre1
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Tamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake-compiler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.5
27
+ description: Ruby bindings for Microsoft Cognitive Toolkit (CNTK), an open source
28
+ deep-learning toolkit
29
+ email: ''
30
+ executables: []
31
+ extensions:
32
+ - ext/cntk/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE
36
+ - ext/cntk/cntk_wrap.cxx
37
+ - ext/cntk/extconf.rb
38
+ - lib/cntk.rb
39
+ - lib/cntk/function.rb
40
+ - lib/cntk/ndarrayview.rb
41
+ - lib/cntk/ndshape.rb
42
+ - lib/cntk/value.rb
43
+ homepage: https://github.com/tamuratak/ruby-cntk
44
+ licenses:
45
+ - MIT
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options:
49
+ - "--exclude=."
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
62
+ requirements:
63
+ - CNTK >= 2.0b11
64
+ rubyforge_project:
65
+ rubygems_version: 2.5.2
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Ruby bindings for Microsoft CNTK, an open source deep-learning toolkit
69
+ test_files: []