tensor_stream 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +10 -0
  3. data/CHANGELOG.md +8 -0
  4. data/README.md +40 -1
  5. data/benchmark/benchmark.rb +4 -1
  6. data/lib/tensor_stream.rb +5 -0
  7. data/lib/tensor_stream/debugging/debugging.rb +4 -2
  8. data/lib/tensor_stream/device.rb +2 -1
  9. data/lib/tensor_stream/evaluator/base_evaluator.rb +43 -32
  10. data/lib/tensor_stream/evaluator/evaluator.rb +0 -1
  11. data/lib/tensor_stream/evaluator/opencl/kernels/acos.cl +8 -0
  12. data/lib/tensor_stream/evaluator/opencl/kernels/apply_gradient.cl +9 -0
  13. data/lib/tensor_stream/evaluator/opencl/kernels/asin.cl +9 -0
  14. data/lib/tensor_stream/evaluator/opencl/kernels/floor_mod.cl +3 -0
  15. data/lib/tensor_stream/evaluator/opencl/kernels/log_softmax.cl +26 -0
  16. data/lib/tensor_stream/evaluator/opencl/kernels/max.cl +5 -5
  17. data/lib/tensor_stream/evaluator/opencl/kernels/min.cl +46 -0
  18. data/lib/tensor_stream/evaluator/opencl/kernels/real_div.cl +3 -0
  19. data/lib/tensor_stream/evaluator/opencl/kernels/softmax_cross.cl +27 -0
  20. data/lib/tensor_stream/evaluator/opencl/kernels/softmax_cross_grad.cl +28 -0
  21. data/lib/tensor_stream/evaluator/opencl/opencl_buffer.rb +5 -6
  22. data/lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb +200 -265
  23. data/lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb +4 -8
  24. data/lib/tensor_stream/evaluator/ruby_evaluator.rb +193 -122
  25. data/lib/tensor_stream/exceptions.rb +6 -0
  26. data/lib/tensor_stream/graph.rb +21 -6
  27. data/lib/tensor_stream/graph_builder.rb +67 -0
  28. data/lib/tensor_stream/graph_deserializers/protobuf.rb +271 -0
  29. data/lib/tensor_stream/graph_keys.rb +1 -0
  30. data/lib/tensor_stream/graph_serializers/pbtext.rb +11 -10
  31. data/lib/tensor_stream/helpers/op_helper.rb +7 -33
  32. data/lib/tensor_stream/helpers/string_helper.rb +16 -0
  33. data/lib/tensor_stream/math_gradients.rb +67 -44
  34. data/lib/tensor_stream/nn/nn_ops.rb +7 -1
  35. data/lib/tensor_stream/operation.rb +14 -27
  36. data/lib/tensor_stream/ops.rb +82 -29
  37. data/lib/tensor_stream/session.rb +4 -0
  38. data/lib/tensor_stream/tensor.rb +30 -12
  39. data/lib/tensor_stream/tensor_shape.rb +1 -1
  40. data/lib/tensor_stream/train/gradient_descent_optimizer.rb +37 -4
  41. data/lib/tensor_stream/train/saver.rb +46 -0
  42. data/lib/tensor_stream/train/utils.rb +37 -0
  43. data/lib/tensor_stream/trainer.rb +2 -0
  44. data/lib/tensor_stream/utils.rb +24 -14
  45. data/lib/tensor_stream/variable.rb +5 -11
  46. data/lib/tensor_stream/variable_scope.rb +15 -0
  47. data/lib/tensor_stream/version.rb +1 -1
  48. data/samples/iris.rb +8 -4
  49. data/samples/linear_regression.rb +1 -1
  50. data/samples/multigpu.rb +73 -0
  51. data/samples/nearest_neighbor.rb +3 -3
  52. data/tensor_stream.gemspec +1 -1
  53. data/test_samples/raw_neural_net_sample.rb +4 -1
  54. metadata +21 -6
data/samples/iris.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "bundler/setup"
2
2
  require 'tensor_stream'
3
- require 'tensor_stream/evaluator/opencl/opencl_evaluator'
3
+ # require 'tensor_stream/evaluator/opencl/opencl_evaluator'
4
4
 
5
5
  # This neural network will predict the species of an iris based on sepal and petal size
6
6
  # Dataset: http://en.wikipedia.org/wiki/Iris_flower_data_set
@@ -75,7 +75,7 @@ w_2 = init_weights([h_size, y_size])
75
75
 
76
76
  # Forward propagation
77
77
  yhat = forwardprop(X, w_1, w_2)
78
- predict = tf.argmax(yhat, axis=1)
78
+ predict = tf.argmax(yhat, 1)
79
79
 
80
80
  # Backward propagation
81
81
  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels: y, logits: yhat))
@@ -89,13 +89,17 @@ sess.run(init)
89
89
  loss = sess.run(cost, feed_dict: { X => x_train, y => y_train })
90
90
  puts "Testing the untrained network..."
91
91
  puts loss
92
+ start_time = Time.now
92
93
  (0..100).each do |epoch|
93
94
  x_train.size.times do |i|
94
95
  sess.run(updates, feed_dict: {X => [x_train[i]], y => [y_train[i]]})
95
- loss = sess.run(cost, feed_dict: { X => [x_train[i]], y => [y_train[i]] })
96
96
  end
97
+ loss = sess.run(cost, feed_dict: { X => x_train, y => y_train })
97
98
  puts "epoch: #{epoch}, loss #{loss}"
98
99
  end
99
100
 
100
101
  loss = sess.run(cost, feed_dict: { X => x_train, y => y_train })
101
- puts "loss after training #{loss}"
102
+ puts "loss after training #{loss}"
103
+ loss = sess.run(cost, feed_dict: { X => x_test, y => y_test })
104
+ puts "loss test data set #{loss}"
105
+ puts("time elapsed ", Time.now.to_i - start_time.to_i)
@@ -26,7 +26,7 @@ b = tf.variable(rand, name: "bias")
26
26
  pred = X * W + b
27
27
 
28
28
  # Mean squared error
29
- cost = tf.reduce_sum(tf.pow(pred - Y, 2)) / ( 2 * n_samples)
29
+ cost = ((pred - Y) ** 2).reduce(:+) / ( 2 * n_samples)
30
30
 
31
31
  optimizer = TensorStream::Train::GradientDescentOptimizer.new(learning_rate).minimize(cost)
32
32
 
@@ -0,0 +1,73 @@
1
+ require "bundler/setup"
2
+ require 'tensor_stream'
3
+ require 'tensor_stream/evaluator/opencl/opencl_evaluator'
4
+ require 'pry-byebug'
5
+
6
+ ts = TensorStream
7
+
8
+ n = 10
9
+ DIMEN = 1024
10
+
11
+ A = ts.random_uniform([DIMEN, DIMEN]).eval
12
+ B = ts.random_uniform([DIMEN, DIMEN]).eval
13
+
14
+
15
+ # Create a graph to store results
16
+ c1 = []
17
+ c2 = []
18
+ a = nil
19
+ b = nil
20
+
21
+ def matpow(m, n)
22
+ return m if n < 1
23
+ TensorStream.matmul(m, matpow(m, n-1))
24
+ end
25
+
26
+ ts.device('/device:GPU:0') do
27
+ a = ts.placeholder(:float32, shape: [DIMEN, DIMEN])
28
+ b = ts.placeholder(:float32, shape: [DIMEN, DIMEN])
29
+ # Compute A^n and B^n and store results in c1
30
+ c1 << matpow(a, n)
31
+ c1 << matpow(b, n)
32
+ end
33
+
34
+ sum = ts.device('/device:GPU:0') do
35
+ ts.add_n(c1)
36
+ end
37
+
38
+ t1_1 = Time.now.to_i
39
+ t2_1 = nil
40
+
41
+ ts.session(log_device_placement: true) do |sess|
42
+ sess.run(sum, feed_dict: { a => A, b => B})
43
+ t2_1 = Time.now.to_i
44
+ end
45
+
46
+ # Multi GPU computing
47
+ # GPU:0 computes A^n
48
+ ts.device('/device:GPU:1') do
49
+ a = ts.placeholder(:float32, shape: [DIMEN, DIMEN])
50
+ c2 << matpow(a, n)
51
+ end
52
+
53
+ # GPU:1 computes B^n
54
+ ts.device('/device:GPU:1') do
55
+ b = ts.placeholder(:float32, shape: [DIMEN, DIMEN])
56
+ c2 << matpow(b, n)
57
+ end
58
+
59
+ ts.device('/device:GPU:1') do
60
+ sum = ts.add_n(c2) #Addition of all elements in c2, i.e. A^n + B^n
61
+ end
62
+
63
+ t1_2 = Time.now.to_i
64
+ t2_2 = nil
65
+ ts.session(log_device_placement:true) do |sess|
66
+ # Run the op.
67
+ sess.run(sum, feed_dict: {a => A, b => B})
68
+ t2_2 = Time.now.to_i
69
+ end
70
+
71
+
72
+ print("Single GPU computation time: " + (t2_1-t1_1).to_s)
73
+ print("Multi GPU computation time: " + (t2_2-t1_2).to_s)
@@ -36,14 +36,14 @@ accuracy = 0.0
36
36
  init = tf.global_variables_initializer()
37
37
 
38
38
  # Start training
39
- tf.session(:opencl_evaluator) do |sess|
39
+ tf.session do |sess|
40
40
  # Run the initializer
41
41
  sess.run(init)
42
42
  Xte.size.times do |i|
43
43
  # Get nearest neighbor
44
44
  nn_index = sess.run(pred, feed_dict: {xtr => Xtr, xte => Xte[i]})
45
- print("Test", i, "Prediction:",Ytr[nn_index].max, \
46
- "True Class:", Yte[i].max)
45
+ print("Test ", i, "Prediction: ",Ytr[nn_index].max, \
46
+ "True Class: ", Yte[i].max, "\n")
47
47
  if Ytr[nn_index].max == Yte[i].max
48
48
  accuracy += 1.0/ Xte.size
49
49
  end
@@ -41,7 +41,7 @@ Gem::Specification.new do |spec|
41
41
  spec.add_development_dependency "rspec_junit_formatter"
42
42
  spec.add_development_dependency "mnist-learn"
43
43
  spec.add_development_dependency "opencl_ruby_ffi"
44
+ spec.add_development_dependency "simplecov"
44
45
  spec.add_dependency "deep_merge"
45
46
  spec.add_dependency "concurrent-ruby"
46
- spec.add_dependency "sciruby"
47
47
  end
@@ -86,8 +86,11 @@ tf.session do |sess|
86
86
  # Run the initializer
87
87
  sess.run(init)
88
88
 
89
+ print("Testing Accuracy:", \
90
+ sess.run(accuracy, feed_dict: { X => mnist.test.images,
91
+ Y => mnist.test.labels}))
92
+
89
93
  (1..num_steps+1).each do |step|
90
-
91
94
  batch_x, batch_y = mnist.train.next_batch(batch_size)
92
95
  # Run optimization op (backprop)
93
96
  puts "."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tensor_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-07-21 00:00:00.000000000 Z
11
+ date: 2018-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -165,13 +165,13 @@ dependencies:
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  - !ruby/object:Gem::Dependency
168
- name: deep_merge
168
+ name: simplecov
169
169
  requirement: !ruby/object:Gem::Requirement
170
170
  requirements:
171
171
  - - ">="
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
- type: :runtime
174
+ type: :development
175
175
  prerelease: false
176
176
  version_requirements: !ruby/object:Gem::Requirement
177
177
  requirements:
@@ -179,7 +179,7 @@ dependencies:
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
181
  - !ruby/object:Gem::Dependency
182
- name: concurrent-ruby
182
+ name: deep_merge
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
185
  - - ">="
@@ -193,7 +193,7 @@ dependencies:
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
195
  - !ruby/object:Gem::Dependency
196
- name: sciruby
196
+ name: concurrent-ruby
197
197
  requirement: !ruby/object:Gem::Requirement
198
198
  requirements:
199
199
  - - ">="
@@ -246,9 +246,12 @@ files:
246
246
  - lib/tensor_stream/evaluator/opencl/kernels/_bool_operand.cl
247
247
  - lib/tensor_stream/evaluator/opencl/kernels/_operand.cl
248
248
  - lib/tensor_stream/evaluator/opencl/kernels/abs.cl
249
+ - lib/tensor_stream/evaluator/opencl/kernels/acos.cl
249
250
  - lib/tensor_stream/evaluator/opencl/kernels/add.cl
251
+ - lib/tensor_stream/evaluator/opencl/kernels/apply_gradient.cl
250
252
  - lib/tensor_stream/evaluator/opencl/kernels/argmax.cl
251
253
  - lib/tensor_stream/evaluator/opencl/kernels/argmin.cl
254
+ - lib/tensor_stream/evaluator/opencl/kernels/asin.cl
252
255
  - lib/tensor_stream/evaluator/opencl/kernels/cast.cl
253
256
  - lib/tensor_stream/evaluator/opencl/kernels/ceil.cl
254
257
  - lib/tensor_stream/evaluator/opencl/kernels/cond.cl.erb
@@ -257,14 +260,18 @@ files:
257
260
  - lib/tensor_stream/evaluator/opencl/kernels/exp.cl
258
261
  - lib/tensor_stream/evaluator/opencl/kernels/floor.cl
259
262
  - lib/tensor_stream/evaluator/opencl/kernels/floor_div.cl
263
+ - lib/tensor_stream/evaluator/opencl/kernels/floor_mod.cl
260
264
  - lib/tensor_stream/evaluator/opencl/kernels/gemm.cl
261
265
  - lib/tensor_stream/evaluator/opencl/kernels/log.cl
262
266
  - lib/tensor_stream/evaluator/opencl/kernels/log1p.cl
267
+ - lib/tensor_stream/evaluator/opencl/kernels/log_softmax.cl
263
268
  - lib/tensor_stream/evaluator/opencl/kernels/max.cl
269
+ - lib/tensor_stream/evaluator/opencl/kernels/min.cl
264
270
  - lib/tensor_stream/evaluator/opencl/kernels/mod.cl
265
271
  - lib/tensor_stream/evaluator/opencl/kernels/mul.cl
266
272
  - lib/tensor_stream/evaluator/opencl/kernels/negate.cl
267
273
  - lib/tensor_stream/evaluator/opencl/kernels/pow.cl
274
+ - lib/tensor_stream/evaluator/opencl/kernels/real_div.cl
268
275
  - lib/tensor_stream/evaluator/opencl/kernels/reciprocal.cl
269
276
  - lib/tensor_stream/evaluator/opencl/kernels/round.cl
270
277
  - lib/tensor_stream/evaluator/opencl/kernels/sigmoid.cl
@@ -272,6 +279,8 @@ files:
272
279
  - lib/tensor_stream/evaluator/opencl/kernels/sign.cl
273
280
  - lib/tensor_stream/evaluator/opencl/kernels/sin.cl
274
281
  - lib/tensor_stream/evaluator/opencl/kernels/softmax.cl
282
+ - lib/tensor_stream/evaluator/opencl/kernels/softmax_cross.cl
283
+ - lib/tensor_stream/evaluator/opencl/kernels/softmax_cross_grad.cl
275
284
  - lib/tensor_stream/evaluator/opencl/kernels/softmax_grad.cl
276
285
  - lib/tensor_stream/evaluator/opencl/kernels/sqrt.cl
277
286
  - lib/tensor_stream/evaluator/opencl/kernels/square.cl
@@ -289,7 +298,10 @@ files:
289
298
  - lib/tensor_stream/evaluator/operation_helpers/math_helper.rb
290
299
  - lib/tensor_stream/evaluator/operation_helpers/random_gaussian.rb
291
300
  - lib/tensor_stream/evaluator/ruby_evaluator.rb
301
+ - lib/tensor_stream/exceptions.rb
292
302
  - lib/tensor_stream/graph.rb
303
+ - lib/tensor_stream/graph_builder.rb
304
+ - lib/tensor_stream/graph_deserializers/protobuf.rb
293
305
  - lib/tensor_stream/graph_keys.rb
294
306
  - lib/tensor_stream/graph_serializers/graphml.rb
295
307
  - lib/tensor_stream/graph_serializers/pbtext.rb
@@ -308,10 +320,12 @@ files:
308
320
  - lib/tensor_stream/tensor_shape.rb
309
321
  - lib/tensor_stream/train/gradient_descent_optimizer.rb
310
322
  - lib/tensor_stream/train/saver.rb
323
+ - lib/tensor_stream/train/utils.rb
311
324
  - lib/tensor_stream/trainer.rb
312
325
  - lib/tensor_stream/types.rb
313
326
  - lib/tensor_stream/utils.rb
314
327
  - lib/tensor_stream/variable.rb
328
+ - lib/tensor_stream/variable_scope.rb
315
329
  - lib/tensor_stream/version.rb
316
330
  - samples/error.graphml
317
331
  - samples/gradient_sample.graphml
@@ -319,6 +333,7 @@ files:
319
333
  - samples/iris.rb
320
334
  - samples/linear_regression.rb
321
335
  - samples/logistic_regression.rb
336
+ - samples/multigpu.rb
322
337
  - samples/nearest_neighbor.rb
323
338
  - tensor_stream.gemspec
324
339
  - test_samples/error.graphml