Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package markov;
- import java.util.*;
- import java.math.*;
- import java.lang.reflect.*;
- /**
- * Class with main method for presenting the results of the lab. At the moment this just reads in the sequence data
- * and the corresponding state labels.
- */
- public class Answer {
- public static void main(String[] args)
- {
- String mfccDataDirectory = "data/yesno_uncut/mfcc/";
- String labelDirectory = "data/yesno_uncut/labels/";
- // Read in the MFCC data and state labels from each class
- DataWithLabels dataClass1 = new DataWithLabels (mfccDataDirectory+"yes",labelDirectory+"yes");
- DataWithLabels dataClass2 = new DataWithLabels (mfccDataDirectory+"no",labelDirectory+"no");
- // Task 1
- // 3 = number of states (silence, yes, silence)
- System.out.println("------");
- System.out.println("Task 1");
- System.out.println("------");
- System.out.println("Yes:");
- HiddenMarkovModel markovYes = new HiddenMarkovModel(3, dataClass1.getMfcc(), dataClass1.getLabels());
- // iterate through each state, getting probability for each transition
- for (int i = 0; i <= markovYes.getNoStates(); i++)
- for (int j = 0; j <= markovYes.getNoStates(); j++)
- System.out.println(markovYes.getTransitionProbability(i, j));
- System.out.println("--- End of Yes ---");
- // 3 = number of states (silence, no, silence)
- System.out.println("No:");
- HiddenMarkovModel markovNo = new HiddenMarkovModel(3, dataClass2.getMfcc(), dataClass2.getLabels());
- // iterate through each state, getting probability for each transition
- for (int i = 0; i <= markovNo.getNoStates(); i++)
- for (int j = 0; j <= markovYes.getNoStates(); j++)
- System.out.println(markovNo.getTransitionProbability(i, j));
- System.out.println("--- End of No ---");
- // Task 2
- // Our hidden markov's with number of yes/no
- Classifier classifier = new Classifier(markovYes, markovNo, 82 / 165.0);
- int numberOfErrors = 0;
- // If less than 0.5 then we do not classify as 'yes'
- for (int index = 0; index < dataClass1.getNumberExamples(); index++)
- {
- double answer = classifier.classify(dataClass1.getMfcc(index));
- if (answer < 0.5)
- numberOfErrors++;
- } // for
- // If greater than 0.5 then we do not classify as 'no'
- for (int index = 0; index < dataClass2.getNumberExamples(); index++)
- {
- double answer = classifier.classify(dataClass2.getMfcc(index));
- if (answer > 0.5)
- numberOfErrors++;
- }
- // Finding total percentage of errors = noOfErrors / totalData * 100
- double percentageErrors = numberOfErrors / 165.0 * 100.0;
- System.out.println("------");
- System.out.println("Task 2");
- System.out.println("------");
- System.out.println("Total error percentage is " + percentageErrors + "%");
- // Task 3
- // Creating a new Markov Model by combining our current two
- // This time we have 4 states - silence, yes, no, silence (and then stop)
- HiddenMarkovModel markovYesNo = new HiddenMarkovModel(4);
- // Need to represent silent to yes (states 0 -> 1 in our old model, and the same in the new)
- markovYesNo.setTransitionProbability(markovYes.getTransitionProbability(0, 1), 0, 1);
- // Need to represent silent to no (states 0 -> 1 in our old model, 0 -> 2 in the new)
- markovYesNo.setTransitionProbability(markovNo.getTransitionProbability(0, 1), 0, 2);
- // Need to represent silent to silent [initial] (states 0 -> 0, 0 -> 0 in our old model, 0 -> 0, 0 -> 0 in the new, so we must take an average)
- markovYesNo.setTransitionProbability((markovYes.getTransitionProbability(0, 0)
- + markovNo.getTransitionProbability(0, 0)) / 2, 0, 0);
- // Need to represent yes to yes (states 1 -> 1 in our old model, 1 -> 1 in the new)
- markovYesNo.setTransitionProbability(markovYes.getTransitionProbability(1, 1), 1, 1);
- // Need to represent no to no (states 1 -> 1 in our old model, 2 -> 2 in the new)
- markovYesNo.setTransitionProbability(markovNo.getTransitionProbability(1, 1), 2, 2);
- // Need to represent yes to silent (states 1 -> 2 in our old model, 1 -> 3 in the new)
- markovYesNo.setTransitionProbability(markovYes.getTransitionProbability(1, 2), 1, 3);
- // Need to represent no to silent (states 1 -> 2 in our old model, 2 -> 3 in the new)
- markovYesNo.setTransitionProbability(markovNo.getTransitionProbability(1, 2), 2, 3);
- // Need to represent silent to silent [last] (states 2 -> 2, 2 -> 2 in our old model, 3 -> 3, 3 -> 3 in the new, so we must take an average)
- markovYesNo.setTransitionProbability((markovYes.getTransitionProbability(2, 2)
- + markovNo.getTransitionProbability(2,2)) / 2, 3, 3);
- // Need to represent stop to start (states 3 -> 0, 3 -> 0 in our old model, 4 -> 0, 4 -> 0 in the new, so we must take an average)
- markovYesNo.setTransitionProbability((markovYes.getTransitionProbability(3, 0)
- + markovNo.getTransitionProbability(3,0)) / 2, 4, 0);
- // Need to represent silence [initial] to stop (states 0 -> 3, 0 -> 3 in our old model, 0 -> 4, 0 -> 4 in the new, so we must take an average)
- markovYesNo.setTransitionProbability((markovYes.getTransitionProbability(0, 3)
- + markovNo.getTransitionProbability(0,3)) / 2, 0, 4);
- // Need to represent yes to stop (states 1 -> 3 in our old model, 1 -> 4 in the new)
- markovYesNo.setTransitionProbability(markovYes.getTransitionProbability(1, 3), 1, 4);
- // Need to represent no to stop (states 1 -> 3 in our old model, 2 -> 4 in the new)
- markovYesNo.setTransitionProbability(markovNo.getTransitionProbability(1, 3), 2, 4);
- // Need to represent silence to stop [last] (states 2 -> 3, 2 -> 3 in our old model, 3 -> 4, 3 -> 4 in the new, so we must take an average)
- markovYesNo.setTransitionProbability((markovYes.getTransitionProbability(2, 3)
- + markovNo.getTransitionProbability(2,3)) / 2, 3, 4);
- // Here we are printing our results for each state
- // Should be the result of combining both yes and no classifiers
- System.out.println("------");
- System.out.println("Task 3");
- System.out.println("------");
- for(int i = 0; i < 5; i++)
- {
- for(int j = 0; j < 5; j++)
- System.out.println(markovYesNo.getTransitionProbability(i, j));
- }
- // Combining emission densities
- for (int i = 0; i < 13; i++)
- {
- // "Silence" needs to be combined
- markovYesNo.setEmissionDensity(markovYes.getEmissionDensity(i, 0).combine(markovNo.getEmissionDensity(i, 0)), i, 0);
- // Separate "yes" and "no"
- markovYesNo.setEmissionDensity(markovYes.getEmissionDensity(i, 1), i, 1);
- markovYesNo.setEmissionDensity(markovNo.getEmissionDensity(i, 1), i, 2);
- // And finally another "silence" at the end
- markovYesNo.setEmissionDensity(markovYes.getEmissionDensity(i, 2).combine(markovNo.getEmissionDensity(i, 2)), i, 3);
- }
- // Task 4
- // Finding path errors for yes and no classifiers respectively
- // Using viterbi method to find optimal path
- double yesNoError = 0;
- int[] yesViterbi;
- int[] noViterbi;
- // Going through our 'yes's
- for(int i = 0; i < dataClass1.getNumberExamples(); i++)
- {
- yesViterbi = markovYesNo.viterbi(dataClass1.getMfcc(i));
- for(int j = 0; j < yesViterbi.length; j++)
- {
- // State 2 is our 'no' and therefore cannot possibly be optimal
- if (yesViterbi[j] == 2)
- {
- // But only count it once so we do not get a silly return e.g. 2,2,2,2 = 1 error not 4
- yesNoError++;
- break;
- }
- }
- }
- // Going through our 'no's
- for(int i = 0; i < dataClass2.getNumberExamples(); i++)
- {
- noViterbi = markovYesNo.viterbi(dataClass2.getMfcc(i));
- for(int j= 0; j < noViterbi.length; j++)
- {
- // State 1 is our 'yes' and therefore cannot possibly be optimal
- if (noViterbi[j] == 1)
- {
- // But only count it once so we do not get a silly return e.g. 1,1,1,1 = 1 error not 4
- yesNoError++;
- break;
- }
- }
- }
- // Calculate percentage
- double yesNoViterbiTotalError = (yesNoError * 100)
- / (dataClass1.getNumberExamples() + dataClass2.getNumberExamples());
- System.out.println("------");
- System.out.println("Task 4");
- System.out.println("------");
- // Combined classifier error percentage
- System.out.println("Total error percentage is " + yesNoViterbiTotalError + "%");
- } // main
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement