### CTC Loss
 
As described above, there is no alignment between utterances and their corresponding phonemes. Thus, Train your network using CTC loss. Decode your predictions, preferably using beam search. Use the list of phonemes provided on the data page to make each prediction into a text string.

Tensor flow has built-in CTC loss function. But for Pytorch, You can use a docker image that has the required CTC-loss library, Wrap-CTC, installed. The docker image name is **`cmudeeplearning11785/machine_learning_gpu`**.  You can read the documentation for wrap-CTC [here][1]. 


**Using Warp CTC in pytorch** 

The module assumes the blank position is at `0`. If there are `n` labels, the model output should be of size `n+1`. The targets passed to the loss function should be from `1` to `n` instead of from `0` to `n-1`. 

The loss function takes four inputs. The first input is a tensor of your model network output. The second input is a tensor of all the labels should be concatenated into one single 1D tensor. The third input is a tensor containing the size of each output sequence from the network. The fourth input is a tensor containing the label of each target.  

Your submission should contain the predicted strings for each item in the test set.

### CTC Decoding

For the Kaggle competition, the provided docker image has a pytorch library that you can use to perform beam search, [here][2] is the link to the library. Tensorflow has it's own implementation of beam search.   

This homework has an additional auto-graded section. The auto-graded section will require you to implement two CTC decoding algorithms. These are **Greedy or best path search** and **Beam search**.  

**Greedy or best path search:** At each time step your model will output a probability distribution of all the symbols. In best path search, you will pick the maximum probable symbol at each time step. 

**Beam Search:** In beam search, you will keep the best scoring beams at each time step. This will be done iteratively and at the final time step, the best scoring beam will be selected. For the complete algorithm, please go through [this][4] reference.

Go to [autolab][3]  and download the handout. Inside the handout, You will find a `hw3_part2_ctc_decoding.py` file. The file has two methods `beam_search_decoder` and `best_path_decoder`. Your task is to implement these methods and submit the file to autolab. 

 - In the handout, You will also find the following sample data files. These files include sample probabilities, sequence lengths and decoded labels. List of labels can be found in the file `phoneme_list.py` in the data section. 

Note: In the provided sample probabilities the probability of the blank symbol is assumed to be at the last index.   

- **Beam_search_decoded_output.txt**: This file has decoded symbols using beam search algorithm. 

- **Beam_search_probs.npy**: A numpy array of sample probabilities of shape ( **batch_size** , **maximum sequence length** , **length of labels + 1** )

- **Beam_search_seqs.npy**: A numpy array of shape ( **batch_size** ), which has the sequence length of each element in the batch.

- **Best_path_search_decoded_output.txt**: This file has decoded symbols using best path search algorithm. 

- **Best_path_search_probs.npy**: A numpy array of sample probabilities of shape (**batch_size**, **maximum sequence length**, **length of labels + 1** )

- **Best_path_search_seqs.npy**: A numpy array of shape (**batch_size**), which has the sequence length of each element in the batch.

**Best path search**

Arguments

 1. The post-soft-max probabilities of the model. It should be of shape (batch_size, maximum sequence length, number of total labels + 1 ).
 2. A list of labels without the blank symbol
 3. The length of the sequence of each element in the batch. It should be of shape (bath_size)
 4. Index of the blank symbol in the probability matrix
 5. A flag if True, consecutive repetitions are allowed. If false, two consecutive time steps will not have the same label. 

Returns list of output label sequences with length (batch_size)

**Beam Search Decoder**

Arguments

 1. The post-soft-max probabilities of the model. It should be of shape (batch_size, maximum sequence length, number of total labels + 1 ).
 2. A list of labels without the blank symbol
 3. The length of the sequence of each element in the batch. It should be of shape (bath_size)
 4. Index of the blank symbol in the probability matrix
 5. Size of the beam

Returns list of output label sequences with length (batch_size) 



  [1]: https://github.com/SeanNaren/warp-ctc
  [2]: https://github.com/parlance/ctcdecode
  [3]: http://autolab.andrew.cmu.edu/courses/11785-f18/assessments/homework3part2
  [4]: http://proceedings.mlr.press/v32/graves14.pdf