Character Prediction Model Python

starter code

In this homework, you will work with character-level language models. These models take as input a sequence of characters and predict the next character. You will first implement functionalities for an abstract language model, then build a new Temporal Convolutional Network (TCN).

Starter code and dataset

You will train your model on Barrack Obama speeches (we tried other presidents, but Obama has the most publicly available transcribed speeches). For this assignment, we use a simplified character set: 26 lowercase characters (a to z), space and period.

A character language model (LanguageModel in models.py) generates text by predicting the 28 value (log-)probability distribution of the next character given a string s (in the function LanguageModel.predict_next). For most models predicting the next log-probability for all characters (LanguageModel.predict_all) is as efficient as predicting the log-probability of the last character only. This is why in this assignment, you will only implement the predict_all function, and compute predict_next from predict_all. The predict_all takes a string s of length n as an input. It predicts the log-probability of the next character for each substring of s[:i] for $i \in {0, \ldots, n}$, including the emtpy string ” and the full string s. The function returns n+1 values.

To get you started, we implemented a simple Bigram model, see here for more information. The starter code further contains an AdjacentLanguageModel that favors characters that are adjacent in the alphabet. Use both models to debug your code.

Finally, utils.py provides some useful functionality. First, it loads the dataset in SpeechDataset, and it implements a one_hot encoding function that converts a string s into a one-hot encoding of size (28, len(s)). You can create a dataset of one-hot encodings by calling SpeechDataset(‘data/train.txt’, transform=one_hot). This might be useful later during training.

You can implement different parts of this homework independently. Feel free to skip parts that seem too hard. However, it might be easiest to follow the order of the assignment.

Log likelihoods of text (10 pts)

We start by implementing a log_likelihood function in language.py. This function takes a string as input and returns the log probability of that string under the current language model. Test your implementation using the Bigram or AdjacentLanguageModel.

python3 -m homework.language -m Bigram

Hint: Remember that the language model can take an empty string as input

Hint: Recall that LanguageModel.predict_all returns the log probabilities of the next characters for all substrings.

Hint: The log-likelihood is the sum of all individual likelihoods, not the average

You can grade your log-likelihood using:

python3 -m grader homework -v

Relevant Operations

  • predict_all
  • one_hot
  • and all previous

Generating text (10 pts)

Next, implement the sample_random function. This function takes a language model and samples from it using random sampling. You can generate a random sample by randomly generating the next character according to its distribution. The sample terminates if max_len characters are produced, or a period . is generated.

Hint: torch.distributions contains many useful sampling functions.

Again, test your implementation using the Bigram and grade:

python3 -m grader homework -v

Here is what the master solution (TCN) produces:

some of a backnown my but or the understand thats why weve hardships not around work since there onethey will be begin with consider daughters some as more a new but jig go atkeeral westedly.yet the world.and when a letter prides.in the step of support information and rall higher capacity training fighting and defered melined an

Relevant Operations

Beam search (20 pts)

Implement the function beam_search to generate the top sentences generated by your language mode. You should generate character-by-character and use beam search to efficiently store the top candidate substrings at each step. At every step of beam search expand all possible characters. Terminate a sentence if a period . is generated or max_length was reached. Beam search returns the top n_results either based on their overall log-likelihood or the average per-character log-likelihood average_log_likelihood=True. The per-character log-likelihood will encourage longer sentences, while the overall log-likelihood ofter terminates after a few words.

Hint: You mind find TopNHeap useful to keep the top beam_size beams or n_results around.

Here is a snipped from the master solution TCN with average_log_likelihood=False

thats.today.in.now.

And here with average_log_likelihood=True

and we will continue to make sure that we will continue to the united states of american.and we will continue to make sure that we will continue to the united states of the united states.and we will continue to make sure that we will continue to the united states of america.and thats why were going to make sure that will continue to the united states of america.

Grade your beam search:

python3 -m grader homework -v

Relevant Operations

  • and all previous

TCN Model (20 pts)

Your TCN model will use a CausalConv1dBlock. This block combines causal 1D convolution with a non-linearity (e.g. ReLU). The main TCN then stacks multiple dilated CausalConv1dBlock’s to build a complete model. Use a 1×1 convolution to produce the output. TCN.predict_all should use TCN.forward to compute the log-probability from a single sentence.

Hint: Make sure TCN.forward uses batches of data.

Hint: Make sure TCN.predict_all returns log-probabilities, not logits.

Hint: Store the distribution of the first character as a parameter of the model torch.nn.Parameter

Hint: Try to keep your model manageable and small. The master solution trains in 15 min on a GPU.

Hint: Try a residual block.

Grade your TCN model:

python3 -m grader homework -v

Relevant Operations

TCN Training (40 pts)

Train your TCN in train.py. You may reuse much of the code from prior homework. Save your model using save_model, and test it:

python3 -m grader homework -v

Hint: SGD might work better to train the model, but you might need a high learning rate (e.g. 0.1).

Grading

You can test your code using

python3 -m grader homework -v

In this homework, it is quite easy to cheat the validation grader. We have a much harder to cheat hidden test grader, that is likely going to catch any attempts at fooling it. The point distributions between validation and test will be the same, but we will use additional test cases.

Second, in this homework, it is a little bit harder to overfit, especially if you keep your model small enough. However, still, keep in mind that we evaluate your model on the test set. The performance on the test grader may vary. Try not to overfit to the validation set too much.

We set the testing log-likelihood threshold such that a Bigram with a log-likelihood of -2.3 gets 0 points and a TCN with log-likelihood -1.3 get the full score. Grading is linear.

Grading

The test grader we provide

python3 -m grader homework -v

will run a subset of test cases we use during the actual testing. The point distributions will be the same, but we will use additional test cases. More importantly, we evaluate your model on the test set. The performance on the test grader may vary. Try not to overfit to the validation set too much.

Submission (ID is 3849)

Once you finished the assignment, create a submission bundle using

python3 bundle.py homework [YOUR ID]

and submit the zip file online. Please note that the maximum file size our grader accepts is 20MB. Please keep your model compact. Please double-check that your zip file was properly created, by grading it again

python3 -m grader [YOUR ID].zip

Online grader

We will use an automated grader online to grade all your submissions. There is a limit of 5 submissions per assignment.

The online grading system will use a slightly modified version of python and the grader:

  • Please do not use the exitor exit command, it will likely lead to a crash in the grader
  • Please do not try to access, read, or write files outside the ones specified in the assignment. This again will lead to a crash. File writing is disabled.
  • Network access is disabled. Please do not try to communicate with the outside world.
  • Forking is not allowed!
  • printor stdout.write statements from your code are ignored and not returned.

Running your assignment on google colab

You might need a GPU to train your models. You can get a free one on google colab. We provide you with an ipython notebook that can get you started on colab for each homework.

If you’ve never used colab before, go through colab notebook (tutorial)
When you’re comfortable with the workflow, feel free to use colab notebook (shortened)

Follow the instructions below to use it.

  • Go to http://colab.research.google.com/.
  • Sign in to your Google account.
  • Select the upload tab then select the .ipynb
  • Follow the instructions on the homework notebook to upload code and data.

 

 

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
Open chat
1
You can contact our live agent via WhatsApp! Via + 1 929 473-0077

Feel free to ask questions, clarifications, or discounts available when placing an order.

Order your essay today and save 20% with the discount code GURUH