jedld / tensor_stream

A ground-up and standalone reimplementation of TensorFlow for ruby. Comes with a pure ruby and OpenCL opcode evaluator
MIT License
507 stars 17 forks source link

matmul is throwing Error #5

Closed samgooi4189 closed 5 years ago

samgooi4189 commented 5 years ago

I am currently following the steps in the README, and having issue with matmul

Const rank must be greater than 1
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby/math_ops.rb:242:in `block (2 levels) in included'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/base_evaluator.rb:134:in `instance_exec'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/base_evaluator.rb:134:in `invoke'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:274:in `eval_operation'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:52:in `run'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:65:in `run_with_buffer'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:166:in `run_with_session_context'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:140:in `delegate_to_evaluator'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:88:in `block in run'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:85:in `collect'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:85:in `run'
breakpoint.rb:9:in `<main>'
Traceback (most recent call last):
    9: from breakpoint.rb:9:in `<main>'
    8: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:85:in `run'
    7: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:85:in `collect'
    6: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:88:in `block in run'
    5: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:140:in `delegate_to_evaluator'
    4: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/session.rb:166:in `run_with_session_context'
    3: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:65:in `run_with_buffer'
    2: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:52:in `run'
    1: from /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:270:in `eval_operation'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby_evaluator.rb:322:in `rescue in eval_operation': error Const rank must be greater than 1 while evaluating abc :  (TensorStream::Evaluator::EvaluatorExcecutionException)
 Const.matmul(Const_1) defined at /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/graph.rb:152:in `add_op!'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/helpers/op_helper.rb:8:in `_op'
/home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/ops.rb:734:in `matmul'
breakpoint.rb:7:in `<main>'

My code is

require 'tensor_stream'

ts = TensorStream

a = ts.constant([2,2])
b = ts.constant([3,3])
f = ts.matmul(a,b, name: 'abc')

ts.session.run(f)

From the gem code (/lib/tensor_stream/evaluator/ruby/math_ops.rb),

[237, 246] in /home/samgooi4189/.rvm/gems/ruby-2.5.1@tensorstream/gems/tensor_stream-0.9.10/lib/tensor_stream/evaluator/ruby/math_ops.rb
   237: 
   238:         register_op :mat_mul do |_context, tensor, inputs|
   239:           matrix_a, matrix_b = inputs
   240:           rank_a = get_rank(matrix_a)
   241:           rank_b = get_rank(matrix_b)
=> 242:           raise "#{tensor.inputs[0].name} rank must be greater than 1" if rank_a < 2
   243:           raise "#{tensor.inputs[1].name} rank must be greater than 1" if rank_b < 2
   244: 
   245:           matrix_a = matrix_a.transpose if tensor.options[:transpose_a]
   246:           matrix_b = matrix_b.transpose if tensor.options[:transpose_b]
(byebug) rank_a
1
(byebug) rank_b
1

Maybe get_rank(matrix_a, 1) ?

jedld commented 5 years ago

matmul requires at least rank(2) tensors (2 dimensional) and as this is matrix multiplication the shapes must be compatible.

For your example to work, it should be:

require 'tensor_stream'

ts = TensorStream

a = ts.constant([[2,2]])
b = ts.constant([[3],[3]])
f = ts.matmul(a,b, name: 'abc')

ts.session.run(f)