tensor_stream 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rake_tasks~ +0 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +5 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +123 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/lib/tensor_stream.rb +138 -0
  14. data/lib/tensor_stream/control_flow.rb +23 -0
  15. data/lib/tensor_stream/evaluator/evaluator.rb +7 -0
  16. data/lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb +32 -0
  17. data/lib/tensor_stream/evaluator/ruby_evaluator.rb +749 -0
  18. data/lib/tensor_stream/graph.rb +98 -0
  19. data/lib/tensor_stream/graph_keys.rb +5 -0
  20. data/lib/tensor_stream/helpers/op_helper.rb +58 -0
  21. data/lib/tensor_stream/math_gradients.rb +161 -0
  22. data/lib/tensor_stream/monkey_patches/integer.rb +0 -0
  23. data/lib/tensor_stream/nn/nn_ops.rb +17 -0
  24. data/lib/tensor_stream/operation.rb +195 -0
  25. data/lib/tensor_stream/ops.rb +225 -0
  26. data/lib/tensor_stream/placeholder.rb +21 -0
  27. data/lib/tensor_stream/session.rb +66 -0
  28. data/lib/tensor_stream/tensor.rb +317 -0
  29. data/lib/tensor_stream/tensor_shape.rb +25 -0
  30. data/lib/tensor_stream/train/gradient_descent_optimizer.rb +23 -0
  31. data/lib/tensor_stream/train/saver.rb +61 -0
  32. data/lib/tensor_stream/trainer.rb +7 -0
  33. data/lib/tensor_stream/types.rb +17 -0
  34. data/lib/tensor_stream/variable.rb +52 -0
  35. data/lib/tensor_stream/version.rb +7 -0
  36. data/samples/iris.data +150 -0
  37. data/samples/iris.rb +117 -0
  38. data/samples/linear_regression.rb +55 -0
  39. data/samples/raw_neural_net_sample.rb +54 -0
  40. data/tensor_stream.gemspec +40 -0
  41. metadata +185 -0
@@ -0,0 +1,55 @@
1
+ require "bundler/setup"
2
+ require 'tensor_stream'
3
+ require 'benchmark'
4
+
5
+ tf = TensorStream
6
+
7
+ learning_rate = 0.01
8
+ training_epochs = 1000
9
+ display_step = 50
10
+
11
+ train_X = [3.3,4.4,5.5,6.71,6.93,4.168,9.779,6.182,7.59,2.167,
12
+ 7.042,10.791,5.313,7.997,5.654,9.27,3.1]
13
+ train_Y = [1.7,2.76,2.09,3.19,1.694,1.573,3.366,2.596,2.53,1.221,
14
+ 2.827,3.465,1.65,2.904,2.42,2.94,1.3]
15
+
16
+ n_samples = train_X.size
17
+
18
+ X = tf.placeholder("float")
19
+ Y = tf.placeholder("float")
20
+
21
+ # Set model weights
22
+ W = tf.Variable(rand, name: "weight")
23
+ b = tf.Variable(rand, name: "bias")
24
+
25
+ # Construct a linear model
26
+ pred = X * W + b
27
+
28
+ # Mean squared error
29
+ cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / ( 2 * n_samples)
30
+
31
+ optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).minimize(cost)
32
+
33
+ # Initialize the variables (i.e. assign their default value)
34
+ init = tf.global_variables_initializer()
35
+
36
+ tf.Session do |sess|
37
+ start_time = Time.now
38
+ sess.run(init)
39
+ (0..training_epochs).each do |epoch|
40
+ train_X.zip(train_Y).each do |x,y|
41
+ sess.run(optimizer, feed_dict: {X => x, Y => y})
42
+ end
43
+
44
+ if (epoch+1) % display_step == 0
45
+ c = sess.run(cost, feed_dict: {X => train_X, Y => train_Y})
46
+ puts("Epoch:", '%04d' % (epoch+1), "cost=", c, \
47
+ "W=", sess.run(W), "b=", sess.run(b))
48
+ end
49
+ end
50
+
51
+ puts("Optimization Finished!")
52
+ training_cost = sess.run(cost, feed_dict: { X => train_X, Y => train_Y})
53
+ puts("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')
54
+ puts("time elapsed ", Time.now.to_i - start_time.to_i)
55
+ end
@@ -0,0 +1,54 @@
1
+ require "bundler/setup"
2
+ require 'tensor_stream'
3
+ require 'pry-byebug'
4
+
5
+ learning_rate = 0.1
6
+ num_steps = 500
7
+ batch_size = 128
8
+ display_step = 100
9
+
10
+ # Network Parameters
11
+ n_hidden_1 = 256 # 1st layer number of neurons
12
+ n_hidden_2 = 256 # 2nd layer number of neurons
13
+ num_input = 784 # MNIST data input (img shape: 28*28)
14
+ num_classes = 10 # MNIST total classes (0-9 digits)
15
+
16
+ tf = TensorStream
17
+
18
+ # tf Graph input
19
+ X = tf.placeholder("float", shape: [nil, num_input])
20
+ Y = tf.placeholder("float", shape: [nil, num_classes])
21
+
22
+ # Store layers weight & bias
23
+ @weights = {
24
+ h1: tf.Variable(tf.random_normal([num_input, n_hidden_1])),
25
+ h2: tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
26
+ out: tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
27
+ }
28
+
29
+ @biases = {
30
+ b1: tf.Variable(tf.random_normal([n_hidden_1])),
31
+ b2: tf.Variable(tf.random_normal([n_hidden_2])),
32
+ out: tf.Variable(tf.random_normal([num_classes]))
33
+ }
34
+
35
+
36
+ # Create model
37
+ def neural_net(x)
38
+ # Hidden fully connected layer with 256 neurons
39
+ layer_1 = TensorStream.add(TensorStream.matmul(x, @weights[:h1]), @biases[:b1])
40
+ # Hidden fully connected layer with 256 neurons
41
+ layer_2 = TensorStream.add(TensorStream.matmul(layer_1, @weights[:h2]), @biases[:b2])
42
+ # Output fully connected layer with a neuron for each class
43
+ TensorStream.matmul(layer_2, @weights[:out]) + @biases[:out]
44
+ end
45
+
46
+ def softmax(logits)
47
+ TensorStream.exp(logits) / TensorStream.reduce_sum(TensorStream.exp(logits))
48
+ end
49
+
50
+ # Construct model
51
+ logits = neural_net(X)
52
+ prediction = softmax(logits)
53
+
54
+ puts prediction.to_math
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tensor_stream/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tensor_stream"
8
+ spec.version = TensorStream::VERSION
9
+ spec.authors = ["Joseph Emmanuel Dayo"]
10
+ spec.email = ["joseph.dayo@gmail.com"]
11
+
12
+ spec.summary = %q{A Pure ruby tensorflow implementation}
13
+ spec.description = %q{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.}
14
+ spec.homepage = "http://www.github.com/jedld/tensor_stream"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ spec.add_development_dependency "awesome_print"
37
+ spec.add_development_dependency "rubocop"
38
+ spec.add_dependency "deep_merge"
39
+ spec.add_dependency "concurrent-ruby"
40
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tensor_stream
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Emmanuel Dayo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '1.14'
19
+ name: bundler
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '10.0'
33
+ name: rake
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ name: rspec
48
+ prerelease: false
49
+ type: :development
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: awesome_print
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ name: rubocop
76
+ prerelease: false
77
+ type: :development
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ name: deep_merge
90
+ prerelease: false
91
+ type: :runtime
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ requirement: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ name: concurrent-ruby
104
+ prerelease: false
105
+ type: :runtime
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A reimplementation of TensorFlow for ruby. This is a ground up implementation
112
+ with no dependency on TensorFlow. Effort has been made to make the programming style
113
+ as near to TensorFlow as possible, comes with a pure ruby evaluator by default as
114
+ well with support for an opencl evaluator.
115
+ email:
116
+ - joseph.dayo@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rake_tasks~"
123
+ - ".rspec"
124
+ - ".travis.yml"
125
+ - CODE_OF_CONDUCT.md
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - lib/tensor_stream.rb
133
+ - lib/tensor_stream/control_flow.rb
134
+ - lib/tensor_stream/evaluator/evaluator.rb
135
+ - lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
136
+ - lib/tensor_stream/evaluator/ruby_evaluator.rb
137
+ - lib/tensor_stream/graph.rb
138
+ - lib/tensor_stream/graph_keys.rb
139
+ - lib/tensor_stream/helpers/op_helper.rb
140
+ - lib/tensor_stream/math_gradients.rb
141
+ - lib/tensor_stream/monkey_patches/integer.rb
142
+ - lib/tensor_stream/nn/nn_ops.rb
143
+ - lib/tensor_stream/operation.rb
144
+ - lib/tensor_stream/ops.rb
145
+ - lib/tensor_stream/placeholder.rb
146
+ - lib/tensor_stream/session.rb
147
+ - lib/tensor_stream/tensor.rb
148
+ - lib/tensor_stream/tensor_shape.rb
149
+ - lib/tensor_stream/train/gradient_descent_optimizer.rb
150
+ - lib/tensor_stream/train/saver.rb
151
+ - lib/tensor_stream/trainer.rb
152
+ - lib/tensor_stream/types.rb
153
+ - lib/tensor_stream/variable.rb
154
+ - lib/tensor_stream/version.rb
155
+ - samples/iris.data
156
+ - samples/iris.rb
157
+ - samples/linear_regression.rb
158
+ - samples/raw_neural_net_sample.rb
159
+ - tensor_stream.gemspec
160
+ homepage: http://www.github.com/jedld/tensor_stream
161
+ licenses:
162
+ - MIT
163
+ metadata:
164
+ allowed_push_host: https://rubygems.org
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.6.13
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: A Pure ruby tensorflow implementation
185
+ test_files: []