pluskid / Mocha.jl

Deep Learning framework for Julia
Other
1.29k stars 254 forks source link

Bug at hinge-loss.jl #191

Closed freddycct closed 6 years ago

freddycct commented 8 years ago

@pluskid I hope you can help me with this bug. Please look at this piece of code at line 63 or hinge-loss.jl

function forward(backend::CPUBackend, state::HingeLossLayerState, inputs::Vector{Blob})
  pred  = inputs[1]
  label = inputs[2]

  data_type = eltype(pred)
  n = length(pred)
  m = length(label)

  state.loss = zero(data_type)
  for i=1:n
    println("n=$n, m=$m, i=$i")
    loss = one(data_type) - pred.data[i]*label.data[i]

The problem here is that n != m and n and m should be the batch_size of the training_data. So when i reaches i > n, there will be an out of bounds error. This bug however, does not appear when the GPU backend is used.

Infact, n is a constant 500 while m is the batch_size

Similarly, the backward function also has the same problem at line 85 of the same file.

function backward(backend::CPUBackend, state::HingeLossLayerState, inputs::Vector{Blob}, diffs::Vector{Blob})

assuming that i set n=length(label) for both forward and backward function, i get an error at

ERROR: LoadError: InexactError()
 in max_pooling_forward at C:\Users\chuaf\.julia\v0.4\Mocha\src\layers/pooling/julia-impl.jl:34

Sorry if this seems like a lot to understand!

pluskid commented 8 years ago

@freddycct Hinge loss is used in binary classification. In the case the prediction is a real number (for two classes +/- 1). It does not work in multi-class case.

freddycct commented 8 years ago

@pluskid, I know that, my labels are float32(1.0) and float32(-1.0). It's definitely a bug there.

My program works for the GPU backend but fails for CPU backend.

I should add that I am also doing image classification, but only for two classes.

pluskid commented 8 years ago

In the case of binary prediction, m and n should be equal, so the problem you described does not sound like binary prediction. Can you provide a minimum runnable example to reproduce the bug?

freddycct commented 8 years ago

hingeloss_bug.zip

Here's the zip file. It's basically LeNet but using hingeloss layer at the end. If you enable the GPU backend, you can see that it works fine..