jeudi 24 mars 2022

I get different accuracy each time i run my mlpclassifier

I'm using MLP classifier to classify my text data with 4 emotions

['calm', 'happy', 'fearful', 'disgust']. I checked everything I know random state , test split, gridsearchcv. The algorithm works fine and the accuracy is the problem, even though i am using the same dataset each time i run , the change in accuracy is very huge. The following is the code, Can some one tell me where i went wrong...

 def extract_feature(file_name, mfcc, chroma, mel):
    with soundfile.SoundFile(file_name) as sound_file:
        X = sound_file.read(dtype="float32")
        sample_rate = sound_file.samplerate
        if chroma:
            stft = np.abs(librosa.stft(X))
        result = np.array([])
        if mfcc:
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            result = np.hstack((result, mfccs))
        if chroma:
            chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T, axis=0)
            result = np.hstack((result, chroma))
        if mel:
            mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T, axis=0)
            result = np.hstack((result, mel))
    return result

# Define the motions dictionary
emotions = {
    '01': 'neutral',
    '02': 'calm',
    '03': 'happy',
    '04': 'sad',
    '05': 'angry',
    '06': 'fearful',
    '07': 'disgust',
    '08': 'surprised'
}

# Emotions we want to observe
observed_emotions = ['calm', 'happy', 'fearful', 'disgust']


# Load the data and extract features for each sound file
def load_data():
    x, y = [], []
    for folder in glob.glob('dataset/Actor_*'):
        # print(folder)
        for file in glob.glob(folder + '/*.wav'):
            file_name = os.path.basename(file)
            emotion = emotions[file_name.split('-')[2]]
            if emotion not in observed_emotions:
                continue
            feature = extract_feature(file, mfcc=True, chroma=True, mel=True)
            x.append(feature)
            y.append(emotion)
    return train_test_split(np.array(x), y, test_size=0.25)


x_train, x_test, y_train, y_test = load_data()
# Shape of train and test set and Number of features extracted
print((x_train.shape[0], x_test.shape[0]))
print('Features extracted: {x_train.shape[1]}')

'''model = MLPClassifier(alpha=0.001, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(18,), activation='logistic',
                      learning_rate='adaptive',
                      max_iter=500, solver='adam')'''
model = MLPClassifier(alpha=0.0001, batch_size=25, epsilon=1e-3, hidden_layer_sizes=(100,), activation='logistic',
                      learning_rate='constant',
                      max_iter=500, solver='adam', early_stopping=True)
model.fit(x_train, y_train)
# Predict for the test set
y_pred = model.predict(x_test)
# Calculate Accuracy

# clf = GridSearchCV(MLPClassifier(), param_grid, scoring='accuracy')
# clf.fit(x_train, y_train)

# y1_pred = clf.predict(x_test)

accuracy = accuracy_score(y_test, y_pred, normalize=True)

The accuracy keeps changing between 30-74.some times even below 30% gives this warning..

C:\Users\Gowtham nag\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\neural_network_multilayer_perceptron.py:692: ConvergenceWarning: Stochastic Optimizer: Maximum iterations (500) reached and the optimization hasn't converged yet.

warnings.warn(




Aucun commentaire:

Enregistrer un commentaire