SciRuby / nmatrix

Dense and sparse linear algebra library for Ruby via SciRuby
Other
469 stars 133 forks source link

MRI segmentation fault at unknown location in code #607

Open ghost opened 6 years ago

ghost commented 6 years ago

OS: Ubuntu 17.10 Ruby -v : ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu] NMATRIX (0.2.4)

HI , For some reason i am getting a segmentation fault in my code At first i thought that i waas doing some kind of mistakes but i couldn't find any thing

require "nmatrix"

class NeuralNetwork

  def initialize(inputNodes:0,hiddenNodes:[],outputNodes:0,learningRate: 0.01)
    @nInputs = inputNodes
    @nHidden = hiddenNodes
    @nHiddenLayers = hiddenNodes.length
    @nOutputs = outputNodes
    @learningRate = learningRate
    @hiddenWeights = []
    @hiddenBias = []
    tmp1,tmp2 = @nInputs,@nHidden[0]
    @hiddenWeights[0] = NMatrix.random([tmp2,tmp1],dtype: :float32,scale: (-1..1))
    @hiddenBias[0] = NMatrix.random([tmp2,1],dtype: :float32,scale: (-1..1))

    for i in (1...@nHiddenLayers)
      tmp1,tmp2 = @nHidden[i-1],@nHidden[i]
      @hiddenWeights[i] = NMatrix.random([tmp2,tmp1],dtype: :float32,scale: (-1..1))
      @hiddenBias[i] = NMatrix.random([tmp2,1],dtype: :float32,scale: (-1..1))
    end

    @outputWeights = NMatrix.random([@nOutputs,@nHidden[@nHiddenLayers-1]],dtype: :float32,scale: (-1..1))
    @outputBias =  NMatrix.random([@nOutputs,1],dtype: :float32,scale: (-1..1))

  end

  def train(data,labels)
    target = labels.map{|l| toOneHot(l)}
    data = data.map{|d| d.to_nm([@nInputs,1],dtype: :float32)}
    tloss = 0
    data.each.with_index do |x,ind|
      layers_inputs = [x]
      #feed forward
      for i in(0...@nHiddenLayers)
        x1 = relu(@hiddenWeights[i].dot(layers_inputs[-1])+@hiddenBias[i])
        layers_inputs.push(x1)
      end
      output = softmax(@outputWeights.dot(layers_inputs[-1])+@outputBias)

      #backpropagation
      diff = output - target[ind]
      # puts "#{ind}"
      # tloss += ((diff ** 2).sum[0]/@nOutputs)
      outdelta = softmax_prime(output).dot(diff)
      @outputBias -= outdelta
      @outputWeights -= outdelta.dot(layers_inputs[-1].transpose)
      delta = @outputWeights.transpose.dot(outdelta)
      for i in (@nHiddenLayers-1..0)
        delta = delta*relu_prime(layers_inputs[i])
        @hiddenWeights[i] -= delta.dot(layers_inputs[i])
        @hiddenBias[i] -= delta
        delta = @hiddenWeights[i].transpose.dot(delta)
      end
    end
    # tloss = tloss/data.length
    # return tloss
    nil
  end

  def test(data)
    out = []
    data.each do |x|
      layers_inputs = [x]
      #feed forward
      for i in(0...@nHiddenLayers)
        x1 = relu(@hiddenWeights[i].dot(layers_inputs[-1])+@hiddenBias[i])
        layers_inputs.push(x1)
      end
      out.push(softmax(@outputWeights.dot(layers_inputs[-1])+@outputBias))
    end
    return out.map{|e| e.to_flat_a.index(e.max[0])}
  end

  private
  def relu(x)
    x.map{|e| (e>0)?e:0 }
  end

  def relu_prime(x)
    x.map{|e| (e>0)?1:0 }
  end

  def softmax(x)
    v = x.exp
    return (v/v.sum[0])
  end

  def softmax_prime(y)
    # for output layer only
    phi = y.dot(NMatrix.ones([1,@nOutputs]))
    return phi*(NMatrix.identity(@nOutputs) - phi.transpose)
  end

  def toOneHot(x)
    a = NMatrix.zeros([@nOutputs,1])
    a[x] = 1
    return a
  end
end

and here is the error `/var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710: [BUG] Segmentation fault at 0x00000000000038 ruby 2.3.3p222 (2016-11-21) [x86_64-linux-gnu]

-- Control frame information ----------------------------------------------- c:0016 p:0014 s:0073 e:000069 BLOCK /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH] c:0015 p:---- s:0066 e:000065 CFUNC :dense_map_pair c:0014 p:0011 s:0062 e:000061 LAMBDA /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710 [FINISH] c:0013 p:---- s:0059 e:000058 CFUNC :* c:0012 p:0055 s:0055 e:000054 METHOD /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:92 c:0011 p:0074 s:0050 e:000049 BLOCK /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:45 [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:0077 s:0013 E:001bb8 EVAL ./NN-train.rb:8 [FINISH] c:0001 p:0000 s:0002 E:001fd0 (none) [FINISH]

-- Ruby level backtrace information ---------------------------------------- ./NN-train.rb:8:in <main>' ./NN-train.rb:8:ineach' ./NN-train.rb:11:in block in <main>' ./NN-train.rb:11:ineach' ./NN-train.rb:13:in block (2 levels) in <main>' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:intrain' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in with_index' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:ineach' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:45:in block in train' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:92:insoftmax_prime' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:92:in *' /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix/math.rb:710:inblock (2 levels) in ' /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:inblock (3 levels) in '

-- Machine register context ------------------------------------------------ RIP: 0x00007f75ca68a289 RBP: 0x000056073621d598 RSP: 0x00007ffcf2147b40 RAX: 0x0000000000000000 RBX: 0x0000560735c2b710 RCX: 0x0000000000001c1f RDX: 0x000056073621d5e8 RDI: 0x0000560735af2a70 RSI: 0x0000560735daa950 R8: 0x00007f75cab98c10 R9: 0x00007f75cab98c10 R10: 0x0000560735c2b6f0 R11: 0x0000000000000000 R12: 0x0000560735daa950 R13: 0x0000560735af2a70 R14: 0x0000560735c2b760 R15: 0x00007f75cab98c10 EFL: 0x0000000000010246

-- C level backtrace information ------------------------------------------- /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca6a1405] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca6a163c] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca579f14] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca62c55e] /lib/x86_64-linux-gnu/libc.so.6 [0x7f75ca16c140] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68a289] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca691258] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca697ee9] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_yield_values+0x10b) [0x7f75ca69bc7b] /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so(nm_dense_map_pair+0x18e) [0x7f75c80e7aee] storage/dense/dense.cpp:396 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68e227] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69cd0f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69dd53] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca691345] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca697b3a] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca697da5] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca6992fe] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69a066] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_funcall+0xbd) [0x7f75ca69a6dd] /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so(elementwise_op+0x4c6) [0x7f75c80b8646] ruby_nmatrix.c:2390 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68e227] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca6947f4] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca697ee9] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_yield_values+0x10b) [0x7f75ca69bc7b] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68cc31] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_yield+0x253) [0x7f75ca69c163] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_ary_each+0x3c) [0x7f75ca53b79c] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca699272] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69a066] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68eb8a] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_block_call+0x2b) [0x7f75ca68ed3b] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca577988] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68e227] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca691345] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_yield+0x360) [0x7f75ca69c270] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca5f9638] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68e227] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69cd0f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69dd53] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca691345] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(rb_yield+0x360) [0x7f75ca69c270] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca5f9638] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca68e227] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69cd0f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69dd53] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca691345] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca69731f] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3 [0x7f75ca57d64a] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(ruby_exec_node+0x1d) [0x7f75ca57efbd] /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3(ruby_run_node+0x1e) [0x7f75ca5811be] ruby [0x5607339f78cb] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf1) [0x7f75ca1561c1] ../csu/libc-start.c:308 ruby(_start+0x2a) [0x5607339f78fa]

-- Other runtime information -----------------------------------------------

5607339f7000-5607339f8000 r-xp 00000000 08:05 14688881 /usr/bin/ruby2.3 560733bf7000-560733bf8000 r--p 00000000 08:05 14688881 /usr/bin/ruby2.3 560733bf8000-560733bf9000 rw-p 00001000 08:05 14688881 /usr/bin/ruby2.3 56073590e000-5607363db000 rw-p 00000000 00:00 0 [heap] 7f75c676a000-7f75c7618000 r--s 00000000 08:05 14682313 /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.26.so 7f75c7618000-7f75c77f7000 r--s 00000000 08:05 3674553 /lib/x86_64-linux-gnu/libc-2.26.so 7f75c77f7000-7f75c7a64000 r--s 00000000 08:05 14688764 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3.0 7f75c7a64000-7f75c7a7a000 r-xp 00000000 08:05 3674591 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f75c7a7a000-7f75c7c79000 ---p 00016000 08:05 3674591 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f75c7c79000-7f75c7c7a000 r--p 00015000 08:05 3674591 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f75c7c7a000-7f75c7c7b000 rw-p 00016000 08:05 3674591 /lib/x86_64-linux-gnu/libgcc_s.so.1 7f75c7c7b000-7f75c7df3000 r-xp 00000000 08:05 14689026 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24 7f75c7df3000-7f75c7ff2000 ---p 00178000 08:05 14689026 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24 7f75c7ff2000-7f75c7ffc000 r--p 00177000 08:05 14689026 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24 7f75c7ffc000-7f75c7ffe000 rw-p 00181000 08:05 14689026 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.24 7f75c7ffe000-7f75c8001000 rw-p 00000000 00:00 0 7f75c8001000-7f75c8321000 r-xp 00000000 08:05 20197513 /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so 7f75c8321000-7f75c8521000 ---p 00320000 08:05 20197513 /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so 7f75c8521000-7f75c8528000 r--p 00320000 08:05 20197513 /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so 7f75c8528000-7f75c8529000 rw-p 00327000 08:05 20197513 /var/lib/gems/2.3.0/gems/nmatrix-0.2.4/lib/nmatrix.so 7f75c8529000-7f75c852a000 rw-p 00000000 00:00 0 7f75c852a000-7f75c8531000 r-xp 00000000 08:05 14948721 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/stringio.so 7f75c8531000-7f75c8730000 ---p 00007000 08:05 14948721 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/stringio.so 7f75c8730000-7f75c8731000 r--p 00006000 08:05 14948721 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/stringio.so 7f75c8731000-7f75c8732000 rw-p 00007000 08:05 14948721 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/stringio.so 7f75c8732000-7f75c8734000 r-xp 00000000 08:05 14948679 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/trans/transdb.so 7f75c8734000-7f75c8934000 ---p 00002000 08:05 14948679 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/trans/transdb.so 7f75c8934000-7f75c8935000 r--p 00002000 08:05 14948679 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/trans/transdb.so 7f75c8935000-7f75c8936000 rw-p 00003000 08:05 14948679 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/trans/transdb.so 7f75c8936000-7f75c8938000 r-xp 00000000 08:05 14948636 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/encdb.so 7f75c8938000-7f75c8b37000 ---p 00002000 08:05 14948636 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/encdb.so 7f75c8b37000-7f75c8b38000 r--p 00001000 08:05 14948636 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/encdb.so 7f75c8b38000-7f75c8b39000 rw-p 00002000 08:05 14948636 /usr/lib/x86_64-linux-gnu/ruby/2.3.0/enc/encdb.so 7f75c8b39000-7f75c9504000 r--p 00000000 08:05 14686901 /usr/lib/locale/locale-archive 7f75c9504000-7f75c9659000 r-xp 00000000 08:05 3674619 /lib/x86_64-linux-gnu/libm-2.26.so 7f75c9659000-7f75c9858000 ---p 00155000 08:05 3674619 /lib/x86_64-linux-gnu/libm-2.26.so 7f75c9858000-7f75c9859000 r--p 00154000 08:05 3674619 /lib/x86_64-linux-gnu/libm-2.26.so 7f75c9859000-7f75c985a000 rw-p 00155000 08:05 3674619 /lib/x86_64-linux-gnu/libm-2.26.so 7f75c985a000-7f75c9863000 r-xp 00000000 08:05 3674563 /lib/x86_64-linux-gnu/libcrypt-2.26.so 7f75c9863000-7f75c9a62000 ---p 00009000 08:05 3674563 /lib/x86_64-linux-gnu/libcrypt-2.26.so 7f75c9a62000-7f75c9a63000 r--p 00008000 08:05 3674563 /lib/x86_64-linux-gnu/libcrypt-2.26.so 7f75c9a63000-7f75c9a64000 rw-p 00009000 08:05 3674563 /lib/x86_64-linux-gnu/libcrypt-2.26.so 7f75c9a64000-7f75c9a92000 rw-p 00000000 00:00 0 7f75c9a92000-7f75c9a95000 r-xp 00000000 08:05 3674577 /lib/x86_64-linux-gnu/libdl-2.26.so 7f75c9a95000-7f75c9c94000 ---p 00003000 08:05 3674577 /lib/x86_64-linux-gnu/libdl-2.26.so 7f75c9c94000-7f75c9c95000 r--p 00002000 08:05 3674577 /lib/x86_64-linux-gnu/libdl-2.26.so 7f75c9c95000-7f75c9c96000 rw-p 00003000 08:05 3674577 /lib/x86_64-linux-gnu/libdl-2.26.so 7f75c9c96000-7f75c9d14000 r-xp 00000000 08:05 14688447 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2 7f75c9d14000-7f75c9f14000 ---p 0007e000 08:05 14688447 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2 7f75c9f14000-7f75c9f15000 r--p 0007e000 08:05 14688447 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2 7f75c9f15000-7f75c9f16000 rw-p 0007f000 08:05 14688447 /usr/lib/x86_64-linux-gnu/libgmp.so.10.3.2 7f75c9f16000-7f75c9f30000 r-xp 00000000 08:05 3674692 /lib/x86_64-linux-gnu/libpthread-2.26.so 7f75c9f30000-7f75ca12f000 ---p 0001a000 08:05 3674692 /lib/x86_64-linux-gnu/libpthread-2.26.so 7f75ca12f000-7f75ca130000 r--p 00019000 08:05 3674692 /lib/x86_64-linux-gnu/libpthread-2.26.so 7f75ca130000-7f75ca131000 rw-p 0001a000 08:05 3674692 /lib/x86_64-linux-gnu/libpthread-2.26.so 7f75ca131000-7f75ca135000 rw-p 00000000 00:00 0 7f75ca135000-7f75ca30b000 r-xp 00000000 08:05 3674553 /lib/x86_64-linux-gnu/libc-2.26.so 7f75ca30b000-7f75ca50b000 ---p 001d6000 08:05 3674553 /lib/x86_64-linux-gnu/libc-2.26.so 7f75ca50b000-7f75ca50f000 r--p 001d6000 08:05 3674553 /lib/x86_64-linux-gnu/libc-2.26.so 7f75ca50f000-7f75ca511000 rw-p 001da000 08:05 3674553 /lib/x86_64-linux-gnu/libc-2.26.so 7f75ca511000-7f75ca515000 rw-p 00000000 00:00 0 7f75ca515000-7f75ca77b000 r-xp 00000000 08:05 14688764 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3.0 7f75ca77b000-7f75ca97a000 ---p 00266000 08:05 14688764 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3.0 7f75ca97a000-7f75ca980000 r--p 00265000 08:05 14688764 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3.0 7f75ca980000-7f75ca981000 rw-p 0026b000 08:05 14688764 /usr/lib/x86_64-linux-gnu/libruby-2.3.so.2.3.0 7f75ca981000-7f75ca991000 rw-p 00000000 00:00 0 7f75ca991000-7f75ca9b8000 r-xp 00000000 08:05 3674525 /lib/x86_64-linux-gnu/ld-2.26.so 7f75caa99000-7f75cab9f000 rw-p 00000000 00:00 0 7f75cabaf000-7f75cabb1000 r--s 00000000 08:05 14688881 /usr/bin/ruby2.3 7f75cabb1000-7f75cabb2000 ---p 00000000 00:00 0 7f75cabb2000-7f75cabb8000 rw-p 00000000 00:00 0 7f75cabb8000-7f75cabb9000 r--p 00027000 08:05 3674525 /lib/x86_64-linux-gnu/ld-2.26.so 7f75cabb9000-7f75cabba000 rw-p 00028000 08:05 3674525 /lib/x86_64-linux-gnu/ld-2.26.so 7f75cabba000-7f75cabbb000 rw-p 00000000 00:00 0 7ffcf194f000-7ffcf214e000 rw-p 00000000 00:00 0 [stack] 7ffcf2150000-7ffcf2153000 r--p 00000000 00:00 0 [vvar] 7ffcf2153000-7ffcf2155000 r-xp 00000000 00:00 0 [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]

[NOTE] You may have encountered a bug in the Ruby interpreter or extension libraries. Bug reports are welcome. For details: http://www.ruby-lang.org/bugreport.html

Aborted (core dumped) `

and thank you

translunar commented 6 years ago

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?

ghost commented 6 years ago

@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
translunar commented 6 years ago

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?

ghost commented 6 years ago

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:ineach' ./NN-train.rb:11:in block in <main>' ./NN-train.rb:11:ineach' ./NN-train.rb:13:in block (2 levels) in <main>' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:intrain' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in with_index' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:ineach' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:39:in block in train' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:87:insoftmax' /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:ininject_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:ineach' /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:inblock 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:indense_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:ineach' ./NN-train.rb:11:in block in <main>' ./NN-train.rb:11:ineach' ./NN-train.rb:13:in block (2 levels) in <main>' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:intrain' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:in with_index' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:32:ineach' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:46:in block in train' /home/zaki/Desktop/ruby/MNIST/8/lib/NeuralNet.rb:93:insoftmax_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:inblock (2 levels) in ' /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:inblock (3 levels) in ' /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

translunar commented 6 years ago

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

ghost commented 6 years ago

@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

translunar commented 6 years ago

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.

ghost commented 6 years ago

@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

translunar commented 6 years ago

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.

ghost commented 6 years ago

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

ralampay commented 6 years ago

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

translunar commented 6 years ago

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
}
zheng-yongping commented 5 years ago

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]
kojix2 commented 5 years ago

@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

zheng-yongping commented 4 years ago

@kojix2 Ok, thanks.