Skip to content

Error in song genre classifier using neural network from chapter 4 #7

@Kashyapdevesh

Description

@Kashyapdevesh
ValueError                                Traceback (most recent call last)
<ipython-input-50-9bfdddf06ca4> in <module>
----> 1 features, labels = generate_features_and_labels()

<ipython-input-49-e1fbb8357030> in generate_features_and_labels()
     15     label_uniq_ids, label_row_ids = np.unique(all_labels, return_inverse=True)
     16     label_row_ids = label_row_ids.astype(np.int32, copy=False)
---> 17     onehot_labels = to_categorical(label_row_ids, len(label_uniq_ids))
     18     return np.stack(all_features), onehot_labels
     19 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/np_utils.py in to_categorical(y, num_classes, dtype)
     73   y = y.ravel()
     74   if not num_classes:
---> 75     num_classes = np.max(y) + 1
     76   n = y.shape[0]
     77   categorical = np.zeros((n, num_classes), dtype=dtype)

<__array_function__ internals> in amax(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial, where)
   2704     """
   2705     return _wrapreduction(a, np.maximum, 'max', axis, None, out,
-> 2706                           keepdims=keepdims, initial=initial, where=where)
   2707 
   2708 

/opt/conda/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
     85                 return reduction(axis=axis, out=out, **passkwargs)
     86 
---> 87     return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
     88 
     89 

ValueError: zero-size array to reduction operation maximum which has no identity

Hello,I'm facing this ValueError when calling 'generate_features_and_labels()' function ,I tried wrapping this with special case but it didn't work ,please help.

generate_features_and_labels() function:

def generate_features_and_labels():
    all_features = []
    all_labels = []

    genres = ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock']
    for genre in genres:
        sound_files = glob.glob('genres/'+genre+'/*.au')
        print('Processing %d songs in %s genre...' % (len(sound_files), genre))
        for f in sound_files:
            features = extract_features_song(f)
            all_features.append(features)
            all_labels.append(genre)

    # convert labels to one-hot encoding
    label_uniq_ids, label_row_ids = np.unique(all_labels, return_inverse=True)
    label_row_ids = label_row_ids.astype(np.int32, copy=False)
    onehot_labels = to_categorical(label_row_ids, len(label_uniq_ids))
    return np.stack(all_features), onehot_labels

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions