我正在训练两个GMM分类器,每个分类器对应一个标签,使用MFCC值。 我连接了一个类的所有MFCC值,并将其拟合到一个分类器中。 对于每个分类器,我求出其标签概率的总和。在
def createGMMClassifiers():
label_samples = {}
for label, sample in training.iteritems():
labelstack = np.empty((50,13))
for feature in sample:
#debugger.set_trace()
labelstack = np.concatenate((labelstack,feature))
label_samples[label]=labelstack
for label in label_samples:
#debugger.set_trace()
classifiers[label] = mixture.GMM(n_components = n_classes)
classifiers[label].fit(label_samples[label])
for sample in testing['happy']:
classify(sample)
def classify(testMFCC):
probability = {'happy':0,'sad':0}
for name, classifier in classifiers.iteritems():
prediction = classifier.predict_proba(testMFCC)
for probforlabel in prediction:
probability[name]+=probforlabel[0]
print 'happy ',probability['happy'],'sad ',probability['sad']
if(probability['happy']>probability['sad']):
print 'happy'
else:
print 'sad'
但我的结果似乎并不一致,我发现很难相信这是因为RandomSeed=None状态,因为对于所有测试数据,所有的预测通常都是同一个标签,但每次运行都会给出完全相反的结果(见输出1和输出2)。在
所以我的问题是,在训练分类器的时候,我是不是做了一些明显的错误?在
输出1:
^{2}$输出2:
^{2}$早些时候我问了一个相关的问题,得到了正确的答案。我提供下面的链接。在
Having different results every run with GMM Classifier
编辑: 增加了收集数据并分为培训和测试的主要功能
def main():
happyDir = dir+'happy/'
sadDir = dir+'sad/'
training["sad"]=[]
training["happy"]=[]
testing["happy"]=[]
#TestSet
for wavFile in os.listdir(happyDir)[::-1][:10]:
#print wavFile
fullPath = happyDir+wavFile
testing["happy"].append(sf.getFeatures(fullPath))
#TrainSet
for wavFile in os.listdir(happyDir)[::-1][10:]:
#print wavFile
fullPath = happyDir+wavFile
training["happy"].append(sf.getFeatures(fullPath))
for wavFile in os.listdir(sadDir)[::-1][10:]:
fullPath = sadDir+wavFile
training["sad"].append(sf.getFeatures(fullPath))
#Ensure the number of files in set
print "Test(Happy): ", len(testing['happy'])
print "Train(Happy): ", len(training['happy'])
createGMMClassifiers()
编辑2: 根据答案改变了密码。仍然有类似的不一致的结果。在
你的代码没有什么意义,你为每个新的训练样本重新创建分类器。在
正确的培训代码方案应考虑以下内容:
你的解码码没问题。在
对于分类任务来说,调整给定给分类器的参数是很重要的,而且大量的分类算法遵循选择理论,这意味着如果你简单地改变模型的某些参数,可能会得到一些巨大的不同结果。另外,重要的是要使用不同的算法,而不是只对所有分类任务使用一种算法
对于这个问题,您可以尝试不同的分类算法来测试您的数据是否良好,并尝试为每个分类器使用不同值的不同参数,这样就可以确定问题出在哪里了。在
另一种方法是使用网格搜索来探索和优化特定分类器的最佳参数,请阅读以下内容:http://scikit-learn.org/stable/modules/grid_search.html
相关问题 更多 >
编程相关推荐