如何训练具有可变长度MFCC数据的RNN?

时间:2017-01-01 05:35:44

标签: python-2.7 numpy keras

我是Python和Keras的新手。

我正在尝试为具有可变长度功能的每个音频文件训练具有MFCC数据的循环网络。意思是第一个MFCC文件的MFCC矩阵为(1454,13),第二个MFCC文件的MFCC矩阵为(2512,13)。

我编写了以下脚本来加载培训所需的数据。

import glob
import os
import numpy as np

class advertisements:
ad_list = []
result_list = []

def __init__(self, ad_file_path, result_file_path):

    for x in range(0, glob.glob(os.path.join(ad_file_path, "*.mfcc")).__len__()):
        self.ad_list.append([])


    for file_ in glob.glob(os.path.join(ad_file_path, "*.mfcc")):

        index_ = file_.split('\\')[1].split('-')[0]
        # print "Index : " + index_
        file_ = open(file_)

        for feat_ in file_:
            self.ad_list[int(index_)-1].append(feat_)
        file_.close()

    file_ = open(result_file_path)

    for result in file_:
        self.result_list.append(result.split(',')[1])
    file_.close()

    file_index_ = 0
    for ad_ in self.ad_list:
        # print "Number of Features in Ad : = " + str(row.__len__())
        feature_index_ = 0
        for feat_ in ad_:
            contents_ = feat_.split(' ')
            # 14 th line is a blank line therefore we should remove it
            del contents_[-1]
            self.ad_list[file_index_][feature_index_] = np.array(contents_, dtype='float32')
            feature_index_ += 1
        file_index_ += 1

def load_data(self, train_data_percent=75):
    x_train = []
    y_train = []
    x_test = []
    y_test = []
    # print "Ad List Length at load data : " + str(self.ad_list.__len__())
    if 0 < train_data_percent < 100 :
        last_index = self.ad_list.__len__()*(train_data_percent)/100

        for i in range(0, last_index):

            x_train.append(self.ad_list[i])
            y_train.append(self.result_list[i])

        for i in range(last_index, self.ad_list.__len__()):

            x_test.append(self.ad_list[i])
            y_test.append(self.result_list[i])
    else:
        print ("Invalid Training Data percentage")
        return -1

    return (x_train, y_train), (x_test, y_test)

我已将所有MFCC数据加载到3D列表中,因为我找不到创建3D numpy数组的方法。

以下脚本用于构建RNN并训练它。

from keras.models import Sequential
from keras.layers import Highway
from keras.layers import LSTM
from keras.layers import Dense
from keras.utils import np_utils
import numpy as np

import advertisements


batch_size = 75
nb_epochs = 10

a = advertisements.advertisements("MFCC", "results.csv")
(x_train, y_train), (x_test, y_test) = a.load_data(75)

# Created the model

model = Sequential()

# Adding Input Layer
model.add(Dense(4, batch_input_shape=(100,None,13)))

# Adding LSTM Layer (Recurrent Layer)

model.add(LSTM(100))

# Adding output layer

model.add(Dense(1))

# Compile the model

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fit the model

print(model.summary())

x_train = np.array(x_train)
model.fit(x_train, y_train, nb_epoch=nb_epochs, batch_size=batch_size)

# Evaluate the model with test data set

scores = model.evaluate(x_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

# write the weights to a file

我运行训练RNN的脚本。我收到以下错误:

 Traceback (most recent call last):
 File "C:/Users/Nicum/PycharmProjects/ResearchSamples/testScript.py",   line 79, in <module>
 model.fit(x_train, y_train, nb_epoch=nb_epochs, batch_size=batch_size)
 File "H:\Users\Nicum\Anaconda2\lib\site-packages\keras\models.py", line 671, in fit
initial_epoch=initial_epoch)
File "H:\Users\Nicum\Anaconda2\lib\site-packages\keras\engine\training.py", line 1069, in fit
batch_size=batch_size)
File "H:\Users\Nicum\Anaconda2\lib\site-packages\keras\engine\training.py", line 982, in _standardize_user_data
exception_prefix='model input')
File "H:\Users\Nicum\Anaconda2\lib\site-packages\keras\engine\training.py", line 101, in standardize_input_data
str(array.shape))
ValueError: Error when checking model input: expected dense_input_1 to  have 3 dimensions, but got array with shape (75L, 1L)

我假设这是将数据加载到x_train的问题。有人可以帮我解决这个问题。

先谢谢你了:)

0 个答案:

没有答案