Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Error when checking target: expected activation_35 to have shape (2,) but got array with shape (1,) #4

Open
ropinheiro opened this issue Sep 3, 2019 · 2 comments

Comments

@ropinheiro
Copy link

I installed everything in a Windows 10 machine:

  • Anaconda
  • Spyder
  • Configured an Environment using Python 3.7
  • Created a folder "data" with 3 folders "on", "off", "problem".
  • Each folder containing 5 wav files for each class
  • Then I ran this code:
from pyaudioclassification import feature_extraction, train #, predict
features, labels = feature_extraction('.\data')
model = train(features, labels)

First and second lines run without error. The features and labels are correctly loaded. I can even see them and validate they have shape of 15 (3 folders x 5 files each). Each feature is an array of 193 numbers.

But the third line gives this error:

model = train(features, labels)
Traceback (most recent call last):
  File "<ipython-input-40-41e12a9c6c61>", line 1, in <module>
    model = train(features, labels)
  File "(...)\.conda\envs\MyEnv\lib\site-packages\pyaudioclassification\__init__.py", line 48, in train
    model.fit(x, y, batch_size=64, epochs=epochs, verbose=verbose)
  File "(...)\.conda\envs\MyEnv\lib\site-packages\keras\engine\training.py", line 1089, in fit
    batch_size=batch_size)
  File "(...)\.conda\envs\MyEnv\lib\site-packages\keras\engine\training.py", line 795, in _standardize_user_data
    exception_prefix='target')
  File "(...)\.conda\envs\MyEnv\lib\site-packages\keras\engine\training_utils.py", line 141, in standardize_input_data
    str(data_shape))
**ValueError: Error when checking target: expected activation_5 to have shape (2,) but got array with shape (1,)**

So as you can see, it seems to be a bug somewhere that is causing an array to be of shape 1 when it should be 2. The "activation_xx" name that appears (where xx seems to jump from 5 in 5 numbers each repeated execution: _5, _10, _15...) makes me think in some bug in an activation function executed at some point in the weight calculation algorithms.

@ropinheiro
Copy link
Author

I think the issue is somewhat related with the fact that the feature size (after reading the wav files with the feature_extract method) is always 193. Probably it should be some multiple of 2 due to the way the inner things work inside the Keras neural network calculations. E.g. if I use Keras directly instead of the feature_extraction:

model.fit( features, labels, epochs=10, batch_size=32 )

I get this error:

ValueError: Error when checking target: expected activation_42 to have shape (32,) but got array with shape (1,)

That 32 rang me a bell, as 32 x 6 = 192 (one less than 193, that is the feature size). I will do some tests to validate this hypothesis that the problem is with the 193-size array.

@ropinheiro
Copy link
Author

I hacked the 193 number of bytes in the pyAudioClassification files to have the arrays being returned as 192, with no better result. The problem continued, so the 193 was not the cause.

Meanwhile, I was playing with the model.compile() parameters and found that I got no error when using 'sparse_categorical_crossentropy' as the loss function.

So it seems that a workaround is to use a different loss function. I checked the train() code and noticed that there is a parameter loss_type that replaces the %s in loss='%s_crossentropy', so I tried to call it this way:

model = train(features, labels, loss_type='sparse_categorical')

... as it will append the "_crossentropy" part to the end.

A new error occurred:

InvalidArgumentError: Received a label value of -1 which is outside the valid range of [0, 2). Label values: 0 0 0 -1 -1 -1 -1 1 -1 -1 1 1 -1 0 -1 1 1 1 0 -1 0 0 -1 1 1 1 1 1 0 -1 1 1 1 0 1 -1 -1 0 1 -1 -1 -1 -1 -1 0 0 1 1 -1 0 1 0 0 0 -1 0 1

Err, I gave up. This library seems nice to collect the features from audio files, but not to model the neural network. I will use Keras directly for this purpose.

CapPow added a commit to CapPow/pyAudioClassification that referenced this issue Oct 31, 2019
Proposed are a few changes I implemented to solve issues I ran into. These are not tested on binary data conditions!
- Line 30 of the proposed changes addresses micah5#4
- Line 74 of the proposed changes addresses an undocumented issue I had.

Additionally, the workaround tested by @ropinheiro in micah5#4, shows that user loss_types may be categorical which are not necessary == 'categorical'. A future improvement may be to modify line 41 similar to: 
```if  'categorical' in loss_type:```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant