gpt4 book ai didi

python - Librosa Keras 音乐分析神经网络 Python : Input Value Error

转载 作者:行者123 更新时间:2023-11-30 09:16:41 25 4
gpt4 key购买 nike

我最近尝试进行一项实验,使用 Keras 在 Python IDE IDLE 中编写的神经网络来分析 GTZAN 歌曲数据集。我正在尝试改变层以查看是否对性能有任何影响。我的实验基于一篇详细介绍该项目基础的特定文章:

https://medium.com/@navdeepsingh_2336/identifying-the-genre-of-a-song-with-neural-networks-851db89c42f0

本文中显示的代码共同构成了该程序:

import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.utils.np_utils import to_categorical

def display_mfcc(song):
y, _ = librosa.load(song)
mfcc = librosa.feature.mfcc(y)

plt.figure(figsize=(10, 4))
librosa.display.specshow(mfcc, x_axis='time', y_axis='mel')
plt.colorbar()
plt.title(song)
plt.tight_layout()
plt.show()


def extract_features_song(f):
y, _ = librosa.load(f)

mfcc = librosa.feature.mfcc(y)
mfcc /= np.amax(np.absolute(mfcc))

return np.ndarray.flatten(mfcc)[:25000]

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)

label_uniq_ids, label_row_ids = np.unique(all_labels,
(len(sound_files), genre))
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


features, labels = generate_features_and_labels()

print(np.shape(features))
print(np.shape(labels))

training_split = 0.8

alldata = np.column_stack((features, labels))

np.random.shuffle(alldata)
splitidx = int(len(alldata) * training_split)
train, test = alldata[:splitidx,:], alldata[splitidx:,:]

print(np.shape(train))
print(np.shape(test))

train_input = test[:,:-10]
train_labels = train[:,-10:]

test_input = test[:,:-10]
test_labels = test[:,-10:]

print(np.shape(train_input))
print(np.shape(train_labels))

model = Sequential([
Dense(100, input_dim=np.shape(train_input)[1]),
Activation('relu'),
Dense(10),
Activation('softmax'),
])


model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
print(model.summary())

model.fit(train_input, train_labels, epochs=10, batch_size=32,
validation_split=0.2)
loss, acc = model.evaluate(test_input, test_labels, batch_size=32)

print('Done!')
print('Loss: %.4f, accuracy: %.4f' % (loss, acc))

然后我收到了预期的输出:

Using TensorFlow backend.
Processing 100 songs in blues genre...
Processing 100 songs in classical genre...
Processing 100 songs in country genre...
Processing 100 songs in disco genre...
Processing 100 songs in hiphop genre...
Processing 100 songs in jazz genre...
Processing 100 songs in metal genre...
Processing 100 songs in pop genre...
Processing 100 songs in reggae genre...
Processing 100 songs in rock genre...
(1000, 25000)
(1000, 10)
(800, 25010)
(200, 25010)
(200, 25000)
(800, 10)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_1 (Dense) (None, 100) 2500100
_________________________________________________________________
activation_1 (Activation) (None, 100) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 1010
_________________________________________________________________
activation_2 (Activation) (None, 10) 0
=================================================================
Total params: 2,501,110
Trainable params: 2,501,110
Non-trainable params: 0
<小时/>
None

之后我收到此错误消息:

   Traceback (most recent call last):
File "/Users/surengrigorian/Documents/Stage1.py", line 88, in <module>
validation_split=0.2)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
batch_size=batch_size)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 804, in _standardize_user_data
check_array_length_consistency(x, y, sample_weights)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 237, in check_array_length_consistency
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 200 input samples and 800 target samples.

文章对此部分进行了这样的描述:

总的来说,您有大约 250 万个参数或权重。接下来,运行拟合。它接受训练输入和训练标签,并接受您想要的时期数。您需要 10 个,因此训练输入重复 10 次。它需要一个批量大小来告诉您在更新权重之前要遍历的歌曲数(在本例中为歌曲数); validation_split 为 0.2 表示取 20% 的训练输入,将其分开,实际上并不对其进行训练,而是用它来评估每个时期后的表现。它实际上从未在验证拆分上进行训练,但验证拆分可让您实时查看进度。

感谢您提供的任何帮助。

最佳答案

更好地使用 scikit 中的 split 方法

from sklearn.model_selection import train_test_split, StratifiedShuffleSplit, StratifiedKFold

# apply Scikit stratified sampling
sss = StratifiedShuffleSplit(n_splits=1, test_size=0.20, random_state=random_state)
for train_index, test_index in sss.split(X, y):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]

关于python - Librosa Keras 音乐分析神经网络 Python : Input Value Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com