Giter VIP home page Giter VIP logo

Comments (15)

translunar avatar translunar commented on August 26, 2024

When I run your code and try to instantiate the object with NeuralNetwork.new(), I get the following:

TypeError: nil can't be coerced into Integer
from /Users/jwoods/Projects/nmatrix/lib/nmatrix/nmatrix.rb:117:in `*'

I need more information to reproduce this — perhaps the code you used to instantiate this class?

from nmatrix.

 avatar commented on August 26, 2024

@MohawkJohn
main

#!/usr/bin/env ruby
require "./lib/Constants.rb"
require "./lib/Functions.rb"
require "./lib/NeuralNet.rb"
require "nmatrix"

model = NeuralNetwork.new(inputNodes: S_IMAGE,hiddenNodes: [20,15],outputNodes: 10)
for epoch in (0...EPOCHS)
  starting_time = Time.now
  # loss = 0
  for batch_id in (0...N_TRAINING/S_BATCH)
    data,labels = next_batch(batch_id)
    model.train(data,labels)
    puts batch_id
    # loss+=
  end
  puts epoch
  acc = 0.0
  for batch_id in (0...N_TEST/S_BATCH)
    data,labels = next_batch(batch_id,false)
    out = model.test(data)
    out.each.with_index {|x,ind| (x==labels[ind])? acc+=1 : next}
  end
  acc = acc / N_TEST
  end_time = Time.now
  puts "--epoch: #{epoch + 1}/#{EPOCHS} | time: #{end_time - starting_time} s | training loss : #{loss/(N_TRAINING)} | test acc #{acc}"
end   

Constants

# size of traning/test data
N_TRAINING = 60000
N_TEST = 10000

# number of pixels in a single image
S_IMAGE = 784

# bytes offset
O_IMAGES = 16
O_LABELS = 8

# batch size
S_BATCH = 100

EPOCHS = 15

#Learning rate
L_RATE = 0.01

from nmatrix.

translunar avatar translunar commented on August 26, 2024

Hey, sorry, but if you want us to help you debug your code, you need to provide a minimum working example. What's the minimum amount of code that reproduces this crash?

from nmatrix.

 avatar commented on August 26, 2024

hey , thanks for the response
so i noticed that the error does not have a fixed point
sometimes it happens after 100 iteration and sometimes after a 1000
and i also noticed that the error does not occur only at element wise multiplication as in here
`
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710: [BUG] Segmentation fault at 0x00000000beb144
ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]

-- Control frame information -----------------------------------------------
c:0023 p:0014 s:0105 e:000101 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH]
c:0022 p:---- s:0098 e:000097 CFUNC :dense_map_pair
c:0021 p:0011 s:0094 e:000093 LAMBDA /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH]
c:0020 p:---- s:0091 e:000090 CFUNC :+
c:0019 p:0011 s:0087 e:000086 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:512
c:0018 p:0029 s:0083 e:000082 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:244
c:0017 p:0016 s:0080 e:000079 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:121 [FINISH]
c:0016 p:---- s:0077 e:000076 CFUNC :each
c:0015 p:0050 s:0074 e:000073 METHOD /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:120
c:0014 p:0170 s:0069 e:000068 METHOD /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:239
c:0013 p:0016 s:0060 e:000059 METHOD /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:511
c:0012 p:0020 s:0056 e:000054 METHOD /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:87
c:0011 p:0048 s:0050 e:000049 BLOCK /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:39 [FINISH]
c:0010 p:---- s:0039 e:000038 IFUNC
c:0009 p:---- s:0037 e:000036 CFUNC :each
c:0008 p:---- s:0035 e:000034 CFUNC :with_index
c:0007 p:0038 s:0032 e:000031 METHOD /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32
c:0006 p:0039 s:0025 e:000024 BLOCK ./NN-train.rb:13 [FINISH]
c:0005 p:---- s:0022 e:000021 CFUNC :each
c:0004 p:0046 s:0019 e:000018 BLOCK ./NN-train.rb:11 [FINISH]
c:0003 p:---- s:0016 e:000015 CFUNC :each
c:0002 p:0082 s:0013 E:000278 EVAL ./NN-train.rb:8 [FINISH]
c:0001 p:0000 s:0002 E:000af0 (none) [FINISH]

-- Ruby level backtrace information ----------------------------------------
./NN-train.rb:8:in <main>' ./NN-train.rb:8:in each'
./NN-train.rb:11:in block in <main>' ./NN-train.rb:11:in each'
./NN-train.rb:13:in block (2 levels) in <main>' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in train'
/home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in with_index' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in each'
/home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:39:in block in train' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:87:in softmax'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:511:in sum' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:239:in inject_rank'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:120:in each_rank' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:120:in each'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:121:in block in each_rank' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/enumerate.rb:244:in block in inject_rank'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:512:in block in sum' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:512:in +'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in block (2 levels) in <class:NMatrix>' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in dense_map_pair'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in block (3 levels) in <class:NMatrix>'
OR HERE
`
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710: [BUG] Segmentation fault at 0x00000000000008
ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]

-- Control frame information -----------------------------------------------
c:0017 p:---- s:0075 e:000074 CFUNC :-
c:0016 p:0014 s:0071 e:000070 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH]
c:0015 p:---- s:0067 e:000066 CFUNC :dense_map_pair
c:0014 p:0011 s:0063 e:000062 LAMBDA /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH]
c:0013 p:---- s:0060 e:000059 CFUNC :-
c:0012 p:0052 s:0056 e:000054 METHOD /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:93
c:0011 p:0093 s:0050 e:000049 BLOCK /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:46 [FINISH]
c:0010 p:---- s:0039 e:000038 IFUNC
c:0009 p:---- s:0037 e:000036 CFUNC :each
c:0008 p:---- s:0035 e:000034 CFUNC :with_index
c:0007 p:0038 s:0032 e:000031 METHOD /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32
c:0006 p:0039 s:0025 e:000024 BLOCK ./NN-train.rb:13 [FINISH]
c:0005 p:---- s:0022 e:000021 CFUNC :each
c:0004 p:0046 s:0019 e:000018 BLOCK ./NN-train.rb:11 [FINISH]
c:0003 p:---- s:0016 e:000015 CFUNC :each
c:0002 p:0082 s:0013 E:000c28 EVAL ./NN-train.rb:8 [FINISH]
c:0001 p:0000 s:0002 E:0014a0 (none) [FINISH]

-- Ruby level backtrace information ----------------------------------------
./NN-train.rb:8:in <main>' ./NN-train.rb:8:in each'
./NN-train.rb:11:in block in <main>' ./NN-train.rb:11:in each'
./NN-train.rb:13:in block (2 levels) in <main>' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in train'
/home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in with_index' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in each'
/home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:46:in block in train' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:93:in softmax_prime'
/home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:93:in -' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in block (2 levels) in class:NMatrix'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in __dense_map_pair__' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in block (3 levels) in class:NMatrix'
/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:in -'

i hope that helps and sorry for the mess

from nmatrix.

translunar avatar translunar commented on August 26, 2024

Even with that information, I can't run a debugger on it without a minimum working example.

from nmatrix.

 avatar commented on August 26, 2024

@MohawkJohn
Is this Good enough ?

require "nmatrix"

class SomeClass
    def self.sigmoid(x)
      ((-x).exp + 1.0)**-1
    end
    def self.someFunction(input,hidden,output,data)
      hiddenWeights = NMatrix.random([hidden,input])
      outputWeights = NMatrix.random([output,hidden])
      data = data.map{|d| d.to_nm([input,1]) }
      data.each do |x|
        x1 = hiddenWeights.dot(x)
        self.sigmoid(outputWeights.dot(x1))
      end
    end
end

100000.times do
    SomeClass.someFunction(2,3,2,[[1,1],[0,0],[0,1],[1,0]])
end

notice that this bug doesn't always appear in this example so you might want to try it few times

from nmatrix.

translunar avatar translunar commented on August 26, 2024

It's not happening for me. But I'm looking through your segfault and seeing this:

/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710: [BUG] Segmentation fault at 0x00000000000008

The fact that you're dealing with a memory address near 0x0 suggests to me that you're running out of memory. That's triggering an exception in Ruby, and it's not properly unwinding through NMatrix code (and is an NMatrix bug). These bugs are typically pretty hard to track down, though you could do it with some careful gdb-fu. But resolving the bug will, at best, transform your segfault into a regular Ruby exception, so you may want to look at trying to run this code on a machine with more memory.

from nmatrix.

 avatar commented on August 26, 2024

@MohawkJohn
hi, when i tried to use other libraries (i am using JBLAS + jruby )
and i dont have a problem so i dont think that i have memory problem
maybe dependencies or something else

from nmatrix.

translunar avatar translunar commented on August 26, 2024

Ohhhh, that's different, and could perhaps explain why I wasn't seeing the same problem, since I'm on MRI and not JRuby. Interesting.

from nmatrix.

 avatar commented on August 26, 2024

you got me wrong , the error wasn't on jruby
it was on MRI i switched to jruby just to use JBLAS
@MohawkJohn

from nmatrix.

ralampay avatar ralampay commented on August 26, 2024

I have a similar experience with this. Does the segmentation fault always occur for every run or is it random? In my case it's random. Using MRI. Coded a neural network and for relatively simple matrix multiplications, I get segmentation faults from time to time. My code can be found here:

https://github.com/ralampay/rbnn

from nmatrix.

translunar avatar translunar commented on August 26, 2024

If someone can produce a reliable minimal test that causes the crash, that would be immensely helpful.

Even if it's random, sometimes you can do something like

10000.times do {
  # test
}

from nmatrix.

zheng-yongping avatar zheng-yongping commented on August 26, 2024

openSUSE Leap 15.1
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
nmatrix (0.2.4)

I get a segmentation fault too, but not sure is the same problem.

>> cat test.rb
require 'nmatrix'
N[1, 2, dtype: :xxx]

>> ruby test.rb
Traceback (most recent call last):
        3: from test.rb:2:in `<main>'
        2: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `[]'
        1: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `new'
/home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `initialize': invalid data type symbol (:#<Symbol:0x0000000000cf110c>) specified (ArgumentError)
test.rb: [BUG] Segmentation fault at 0x00007f22fd392830
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]
>> irb
irb(main):001:0> require 'nmatrix'
=> true
irb(main):002:0> N[1, 2, dtype: :xxx]
Traceback (most recent call last):
        7: from /home/me/.rbenv/versions/2.6.3/bin/irb:23:in `<main>'
        6: from /home/me/.rbenv/versions/2.6.3/bin/irb:23:in `load'
        5: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):2
        3: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `[]'
        2: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `new'
        1: from /home/me/.rbenv/versions/2.6.3/lib64/ruby/gems/2.6.0/gems/nmatrix-0.2.4/lib/nmatrix/shortcuts.rb:203:in `initialize'
ArgumentError (invalid data type symbol (:#<Symbol:0x000000000163610c>) specified)
irb(main):003:0> Ctrl-D
/home/me/.rbenv/versions/2.6.3/bin/irb: [BUG] Segmentation fault at 0x00007ffc6e67e7b0
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-linux]

from nmatrix.

kojix2 avatar kojix2 commented on August 26, 2024

@zheng-yongping
If you are looking for a fast matrix computation library like NumPy, try Numo:: Narrray.
https://github.com/ruby-numo/numo-narray
NMatrix is not in active development these years.
There is a long history between NMatrix and NArray, but for now I personally recommend NArray to all users.

If you are skilled developer and want to improve NMatrix, there is an ongoing project.
https://github.com/SciRuby/numruby

from nmatrix.

zheng-yongping avatar zheng-yongping commented on August 26, 2024

@kojix2
Ok, thanks.

from nmatrix.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.