tensor_stream 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rake_tasks~ +0 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +123 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tensor_stream.rb +138 -0
- data/lib/tensor_stream/control_flow.rb +23 -0
- data/lib/tensor_stream/evaluator/evaluator.rb +7 -0
- data/lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb +32 -0
- data/lib/tensor_stream/evaluator/ruby_evaluator.rb +749 -0
- data/lib/tensor_stream/graph.rb +98 -0
- data/lib/tensor_stream/graph_keys.rb +5 -0
- data/lib/tensor_stream/helpers/op_helper.rb +58 -0
- data/lib/tensor_stream/math_gradients.rb +161 -0
- data/lib/tensor_stream/monkey_patches/integer.rb +0 -0
- data/lib/tensor_stream/nn/nn_ops.rb +17 -0
- data/lib/tensor_stream/operation.rb +195 -0
- data/lib/tensor_stream/ops.rb +225 -0
- data/lib/tensor_stream/placeholder.rb +21 -0
- data/lib/tensor_stream/session.rb +66 -0
- data/lib/tensor_stream/tensor.rb +317 -0
- data/lib/tensor_stream/tensor_shape.rb +25 -0
- data/lib/tensor_stream/train/gradient_descent_optimizer.rb +23 -0
- data/lib/tensor_stream/train/saver.rb +61 -0
- data/lib/tensor_stream/trainer.rb +7 -0
- data/lib/tensor_stream/types.rb +17 -0
- data/lib/tensor_stream/variable.rb +52 -0
- data/lib/tensor_stream/version.rb +7 -0
- data/samples/iris.data +150 -0
- data/samples/iris.rb +117 -0
- data/samples/linear_regression.rb +55 -0
- data/samples/raw_neural_net_sample.rb +54 -0
- data/tensor_stream.gemspec +40 -0
- metadata +185 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5adc4dd6bb2c50782e1f0463723d18e395cc0d72480fc79e80df1e0b1e0f1a52
|
4
|
+
data.tar.gz: 64ebe439640a9cb4281b73da9b9c0830b7a8ef722fd7c50582179cab84f1c7e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 04b240ea9ea146fc3db61f1ebbe75c63f1cc1c4c425b27b8cce239a4d900d74466c81c92f0282ca099c2061078096f00a80c2bf1019bcadc80c7da7e3b7edfe1
|
7
|
+
data.tar.gz: e2e8fa75f95c2e4a442d66c95f1e17dcea881c4dc52d7c5a43fdf2a720bea2b1ccc6d3dbc3c359492ba6959fbcb727e91974d79f25a86255f6edff1ccf04b429
|
data/.gitignore
ADDED
data/.rake_tasks~
ADDED
File without changes
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at joseph.dayo@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Joseph Emmanuel Dayo
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
# TensorStream
|
2
|
+
|
3
|
+
A reimplementation of TensorFlow for ruby. This is a ground up implementation with no dependency on TensorFlow. Effort has been made to make the programming style as near to TensorFlow as possible, comes with a pure ruby evaluator by default as well with support for an opencl evaluator.
|
4
|
+
|
5
|
+
The goal of this gem is to have a high performance machine learning and compute solution for ruby with support for a wide range of hardware and software configuration.
|
6
|
+
|
7
|
+
## Features
|
8
|
+
|
9
|
+
- Replicates most of the commonly used low-level tensorflow ops
|
10
|
+
- Supports auto-differentiation via tf.gradients (mostly)
|
11
|
+
- Provision to use your own opcode evaluator (opencl, sciruby and tensorflow backends planned)
|
12
|
+
- Goal is to be as close to TensorFlow in behavior but with some freedom to add ruby specific enhancements (with lots of test cases)
|
13
|
+
|
14
|
+
Since this is a pure ruby implementation for now, performance is not there yet. However it should be a good enough environment to learn about tensorflow and experiment with some models.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Installation is easy, no need to mess with docker, python, clang or other shennanigans, works with both mri and jruby out of the box.
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'tensor_stream'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install tensor_stream
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Usage is similar to how you would use TensorFlow except with ruby syntax
|
37
|
+
|
38
|
+
Linear regression sample:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'tensor_stream'
|
42
|
+
|
43
|
+
tf = TensorStream
|
44
|
+
|
45
|
+
learning_rate = 0.01
|
46
|
+
training_epochs = 1000
|
47
|
+
display_step = 50
|
48
|
+
|
49
|
+
train_X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
|
50
|
+
7.042,10.791,5.313,7.997,5.654,9.27,3.1]
|
51
|
+
train_Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
|
52
|
+
2.827,3.465,1.65,2.904,2.42,2.94,1.3]
|
53
|
+
|
54
|
+
n_samples = train_X.size
|
55
|
+
|
56
|
+
X = tf.placeholder("float")
|
57
|
+
Y = tf.placeholder("float")
|
58
|
+
|
59
|
+
# Set model weights
|
60
|
+
W = tf.Variable(rand, name: "weight")
|
61
|
+
b = tf.Variable(rand, name: "bias")
|
62
|
+
|
63
|
+
# Construct a linear model
|
64
|
+
pred = X * W + b
|
65
|
+
|
66
|
+
# Mean squared error
|
67
|
+
cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / ( 2 * n_samples)
|
68
|
+
|
69
|
+
optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).minimize(cost)
|
70
|
+
|
71
|
+
# Initialize the variables (i.e. assign their default value)
|
72
|
+
init = tf.global_variables_initializer()
|
73
|
+
|
74
|
+
tf.Session do |sess|
|
75
|
+
start_time = Time.now
|
76
|
+
sess.run(init)
|
77
|
+
(0..training_epochs).each do |epoch|
|
78
|
+
train_X.zip(train_Y).each do |x,y|
|
79
|
+
sess.run(optimizer, feed_dict: {X => x, Y => y})
|
80
|
+
end
|
81
|
+
|
82
|
+
if (epoch+1) % display_step == 0
|
83
|
+
c = sess.run(cost, feed_dict: { X => train_X, Y => train_Y })
|
84
|
+
puts("Epoch:", '%04d' % (epoch+1), "cost=", c, \
|
85
|
+
"W=", sess.run(W), "b=", sess.run(b))
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
puts("Optimization Finished!")
|
90
|
+
training_cost = sess.run(cost, feed_dict: { X => train_X, Y => train_Y})
|
91
|
+
puts("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
|
92
|
+
puts("time elapsed ", Time.now.to_i - start_time.to_i)
|
93
|
+
end
|
94
|
+
```
|
95
|
+
|
96
|
+
## Roadmap
|
97
|
+
|
98
|
+
- Docs
|
99
|
+
- Complete low-level op support
|
100
|
+
- SciRuby evaluator
|
101
|
+
- Opencl evaluator
|
102
|
+
- TensorFlow savemodel compatibility
|
103
|
+
|
104
|
+
## Issues
|
105
|
+
|
106
|
+
- This is an early preview release and many things still don't work
|
107
|
+
- Performance is not great, at least until the opencl and/or sciruby backends are complete
|
108
|
+
|
109
|
+
## Development
|
110
|
+
|
111
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
112
|
+
|
113
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cl-brains. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
118
|
+
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
123
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tensor_stream"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
require "tensor_stream/version"
|
2
|
+
require 'deep_merge'
|
3
|
+
require 'matrix'
|
4
|
+
require 'concurrent'
|
5
|
+
require 'tensor_stream/helpers/op_helper'
|
6
|
+
require 'tensor_stream/graph_keys'
|
7
|
+
require 'tensor_stream/types'
|
8
|
+
require 'tensor_stream/graph'
|
9
|
+
require 'tensor_stream/session'
|
10
|
+
require 'tensor_stream/tensor_shape'
|
11
|
+
require 'tensor_stream/tensor'
|
12
|
+
require 'tensor_stream/variable'
|
13
|
+
require 'tensor_stream/operation'
|
14
|
+
require 'tensor_stream/placeholder'
|
15
|
+
require 'tensor_stream/control_flow'
|
16
|
+
require 'tensor_stream/trainer'
|
17
|
+
require 'tensor_stream/nn/nn_ops'
|
18
|
+
require 'tensor_stream/evaluator/evaluator'
|
19
|
+
# require 'tensor_stream/libraries/layers'
|
20
|
+
require "tensor_stream/monkey_patches/integer"
|
21
|
+
require 'tensor_stream/ops'
|
22
|
+
|
23
|
+
module TensorStream
|
24
|
+
extend TensorStream::OpHelper
|
25
|
+
extend TensorStream::Ops
|
26
|
+
|
27
|
+
def self.float32
|
28
|
+
Types.float32
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.get_default_graph
|
32
|
+
TensorStream::Graph.get_default_graph
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.reset_default_graph
|
36
|
+
TensorStream::Graph.get_default_graph.reset
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.enable_eager_execution
|
40
|
+
TensorStream::Graph.get_default_graph.enable_eager_execution
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.disable_eager_execution
|
44
|
+
TensorStream::Graph.get_default_graph.disable_eager_execution
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.executing_eagerly?
|
48
|
+
TensorStream::Graph.get_default_graph.executing_eagerly?
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.Variable(value, options = {})
|
52
|
+
common_options= {
|
53
|
+
initializer: Operation.new(:assign, nil, value),
|
54
|
+
name: options[:name]
|
55
|
+
}
|
56
|
+
if value.is_a?(String)
|
57
|
+
TensorStream::Variable.new(options[:dtype] || :string, 0, [], common_options)
|
58
|
+
elsif value.is_a?(Integer)
|
59
|
+
TensorStream::Variable.new(options[:dtype] || :int32, 0, [], common_options)
|
60
|
+
elsif value.is_a?(Float)
|
61
|
+
TensorStream::Variable.new(options[:dtype] || :float32, 0, [], common_options)
|
62
|
+
else
|
63
|
+
TensorStream::Variable.new(options[:dtype] || :float32, 0, nil, common_options)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.Session(evaluator = :ruby_evaluator, thread_pool_class: Concurrent::ImmediateExecutor)
|
68
|
+
session = TensorStream::Session.new(evaluator, thread_pool_class: thread_pool_class)
|
69
|
+
if block_given?
|
70
|
+
yield session
|
71
|
+
end
|
72
|
+
session
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.program(&block)
|
76
|
+
block.(self)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.layers
|
80
|
+
TensorStream::Layers
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.constant(value, options = {})
|
84
|
+
shared_options = { const: true, value: value, name: options[:name] }
|
85
|
+
if value.is_a?(Float)
|
86
|
+
TensorStream::Tensor.new(options[:dtype] || :float32, 0, options[:shape] || [], shared_options)
|
87
|
+
elsif value.is_a?(Integer)
|
88
|
+
TensorStream::Tensor.new(options[:dtype] || :int32, 0, options[:shape] || [], shared_options)
|
89
|
+
elsif value.is_a?(String)
|
90
|
+
TensorStream::Tensor.new(options[:dtype] || :string, 0, options[:shape] || [], shared_options)
|
91
|
+
elsif value.is_a?(Array)
|
92
|
+
dtype = nil
|
93
|
+
rank = 1
|
94
|
+
dimensions = []
|
95
|
+
value_ptr = value
|
96
|
+
|
97
|
+
begin
|
98
|
+
dtype, rank, value_ptr, d = dtype_eval(dtype, rank, value_ptr)
|
99
|
+
dimensions << d
|
100
|
+
end while dtype == :array
|
101
|
+
|
102
|
+
TensorStream::Tensor.new(dtype, rank, options[:shape] || dimensions, shared_options)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.group(inputs)
|
107
|
+
TensorStream::ControlFlow.new(:group, inputs)
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.get_variable(name, options = {})
|
111
|
+
TensorStream::Variable.new(options[:dtype] || :float32, nil, options[:shape], name: name, initializer: options[:initializer])
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.get_collection(name, options = {})
|
115
|
+
Graph.get_default_graph.get_collection(name, options)
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.placeholder(dtype, options = {})
|
119
|
+
TensorStream::Placeholder.new(dtype, nil, options[:shape])
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.global_variables_initializer
|
123
|
+
TensorStream::Variable.global_variables_initializer
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.train
|
127
|
+
TensorStream::Trainer
|
128
|
+
end
|
129
|
+
|
130
|
+
private
|
131
|
+
|
132
|
+
def self.check_allowed_types(t, types)
|
133
|
+
return t unless t.is_a?(Tensor)
|
134
|
+
return t if t.data_type.nil?
|
135
|
+
|
136
|
+
fail "Parameter data type #{t.data_type} passed not in #{types.join(',')}" if !types.map(&:to_sym).include?(t.data_type)
|
137
|
+
end
|
138
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module TensorStream
|
2
|
+
class ControlFlow < Operation
|
3
|
+
attr_accessor :ops
|
4
|
+
|
5
|
+
def initialize(flow_type, items, ops = nil, options = {})
|
6
|
+
@operation = :"flow_#{flow_type}"
|
7
|
+
@items = items
|
8
|
+
@name = set_name
|
9
|
+
@ops = ops
|
10
|
+
@source = set_source(caller_locations)
|
11
|
+
@graph = options[:graph] || TensorStream.get_default_graph
|
12
|
+
@graph.add_node(self)
|
13
|
+
end
|
14
|
+
|
15
|
+
def set_data_type(passed_data_type)
|
16
|
+
:unknown
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
eval
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|