dfalbel / speech-keras

Code for the blog post "Simple Audio Classification with Keras"
https://blogs.rstudio.com/tensorflow/posts/2018-06-06-simple-audio-classification-keras/
11 stars 14 forks source link

Incompatible shapes due to output layer? #1

Closed ck37 closed 5 years ago

ck37 commented 5 years ago

Hello,

Really appreciate the speech recognition blog post and detailed code chunks. While trying to execute the fit_generator() section today I did run into one small error:

Error in py_call_impl(callable, dots$args, dots$keywords) : 
  InvalidArgumentError: Incompatible shapes: [32,29] vs. [32,30]
     [[{{node loss_3/dense_6_loss/mul}} = Mul[T=DT_FLOAT, _class=["loc:@training_3/Adadelta/gradients/loss_3/dense_6_loss/mul_grad/Reshape_1"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_dense_6_target_0_1, loss_3/dense_6_loss/Log)]]

Detailed traceback: 
  File "/Users/ck37/miniconda3/envs/r-tensorflow/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "/Users/ck37/miniconda3/envs/r-tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1418, in fit_generator
    initial_epoch=initial_epoch)
  File "/Users/ck37/miniconda3/envs/r-tensorflow/lib/python3.6/site-packages/keras/engine/training_generator.py", line 217, in fit_generator
    class_weight=class_weight)
  File "/Users/ck37/miniconda3/envs/r-tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1217, in train_on_batch
    outputs = self.train_fu

I resolved it by changing the final softmax output layer to have 29 units rather than 30. My hunch was that this might be due to the background noise directory being excluded earlier in the script.

Thanks, Chris

ck37 commented 5 years ago

Actually reviewing length(unique(df$class)) I do have 30 unique values so I'm not sure why changing to 29 was needed or even worked.

But this was the updated line I needed for training to begin: layer_dense(units = 29, activation = 'softmax')

Update: in data_generator() there is a response <- tf$one_hot(obs$class_id, 29L) line that seems to limit the unique class values to 29.

dfalbel commented 5 years ago

Hello @ck37! This seems to be related to this issue https://github.com/rstudio/keras/issues/586#issuecomment-437785552

The blog post has been updated now. Thanks very much for reporting it!

ck37 commented 5 years ago

Awesome, thanks - switching to 30 solves the issue.