CMSC 510 HW2 – The goal of the homework is

CMSC 510 HW2 –

The goal of the homework is to gain familiarity with PyTorch (to install it, see:
https://pytorch.org/get-started/locally/ ), a machine learning library for python that allows for
defining the machine learning model and performing gradient descent for it in an automated
way.

Complete 4 exercises described below, and submit via Canvas a zip file with four Jupyter
Notebook files, one per each exercise. Each notebook should contain the code, as well as short
reports on the results of experiments.

Exercise 1.

Train a linear classifier for the Iris dataset (a 3-class classification problem, file iris.csv in
Canvas), using Mean Squared Error as loss (see pytorch_linear_Iris_MSE.py file on Canvas).
Perform an analysis of the behavior of training risk and accuracy for different learning rates.  

Detailed steps:
a) Use pandas to load the iris dataset. Create dummy variables for the classes

b) Define pytorch tensors for the dataset using:  
torch.tensor

c) Define pytorch tensors (with gradient) for weights and biases (W & b). W should be
n_features x n_classes, b should be 1 x n_classes. Initialize b to zeros (torch.zeros), and W to
random values sampled from a normal distribution with null mean – try different values for the
standard deviation and observe changes in the training behavior.

d) Define pytorch optimizer over variables W & b
torch.optim.SGD or torch.optim.Adam

e) Create the main loop that goes over the dataset in multiple epochs. In each epoch
e1) clear gradients (using optimizer.zero_grad)
e2) calculate linear predictions: pred=X W + b using
torch.matmul
e3) pass the linear predictions through the unipolar sigmoid: sigmoid(pred)=1/(1+exp(-
pred)). Use these functions:
torch.log, torch.exp
e4) calculate the squared difference between the predictions (after sigmoid) and the
true classes, for all three output neurons. Use:
torch.pow
e5) calculate risk = average the squared difference over the training samples. Use:
torch.mean e6) calculate gradients of risk with respect to W & b (call risk.backwards)
e7) make optimizer step (using optimizer.step)
e8) calculate accuracy

Experiment with different learning rates for the two optimizers and report the behavior of the
training loss and accuracy.

Exercise 2.

Train a linear classifier for the Iris dataset, using CrossEntropy as loss. Perform an analysis of the
behavior of training risk and accuracy for different learning rates.  

Detailed steps – follow Exercise 1, but replace MSE with CrossEntropy:

e3) pass the linear predictions through softmax (i.e., normalize the unipolar sigmoids for classes
i=1,…,3 to sum up to 1 for each sample)

e4) calculate the cross entropy after softmax (sum_{i=1}^3 y_i ln(softmax_i)).  
torch.multiply, torch.log, torch.sum

e5) calculate risk = average the cross entropy over the training samples

Experiment and report results as in Exercise 1.

Exercise 3.

Starting from Exercise 2, add a split of the Iris dataset into a training set and a test set. Also, in
the training loop, go over small batches of samples (e.g. 20 samples) instead of always over the
whole training set. Experiment with batch size and learning rate.

Exercise 4:

Linear classifier for MNIST Digits dataset. Explore the behavior of the code from Exercise 3 on a
larger, more complicated dataset and report the results.  
The number of training samples is 50,000 – analyze training behavior if a random subset of 100,
500, 1000, 2000 samples is used instead. Also, experiment with the learning rate and the batch
size.

For loading the dataset, use: import torchvision.datasets as datasets
full_train_dataset = datasets.MNIST(root=’./data’, train=True, download=True, transform=None)
full_test_dataset = datasets.MNIST(root=’./data’, train=False, download=True, transform=None)
x_train = full_train_dataset.data.numpy().reshape(-1,n_features).astype(dtype=np.float)/255.0;
x_test = full_test_dataset.data.numpy().reshape(-1,n_features).astype(dtype=np.float)/255.0;
y_train_cat = full_train_dataset.targets.numpy()
y_test_cat = full_test_dataset.targets.numpy()
Note that the download of the dataset may take long time. As with Iris, convert categorical
variables for classes into dummy variables (there are 10 classes).  

 

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

This assignment evaluates your knowledge and skills in the following

   This assignment evaluates your knowledge and skills in the following areas: · Using the South Carolina standard – https://www.ed.sc.gov/instruction/standards-learning/ to write learning objectives · Use 2nd Lesson  Plan” template  · Aligning the rigor of the state standard to the rigor of the learning objective · Developing a performance measure

Discussion 3.1 If you were to create a perceptual map

Discussion 3.1 If you were to create a perceptual map for the product category of watches, what attributes should you include to illustrate both the similarities and differences among the brands? Discussion 3.2 Consider one of these purchases: health care, a car, a time-share condo. What elements are tangible vs.

The nine broad areas of competency are: Scientific Foundations Leadership

The nine broad areas of competency are: Scientific Foundations Leadership Quality Practice Inquiry Technology and Information Literacy Policy Health Delivery System Ethics Independent Practice To Prepare: Review this week’s Learning Resources, focusing on the NONPF Core Competencies Content. The Assignment For each of the nine NONPF competencies, write one paragraph

Select an issue that’s important in America today and choose

Select an issue that’s important in America today and choose an interest group that has taken on that issue. What tactics and methods do they use to influence policy? Make sure to include a news article from the last eight weeks of this group in action. Do not get sidetracked

After looking at your resources and examining your topic in

 After looking at your resources and examining your topic in light of your research, post an outline of your biblical, historical and theological review. What are the main points that you need to make to support your thesis? Include sub-points as well. You may use the Literature Map provided in

Real World Case Study: Community Hospital Suppose there are three

    Real World Case Study: Community Hospital Suppose there are three hospitals in your town. You work for Community Hospital and within 25 mile radius there are also Lincoln  Hospital and University Hospital. You are serving on a Quality Task Force at your hospital and the task force is

Locate and share a ToolKit from the CDC, WHO, United

Locate and share a ToolKit from the CDC, WHO, United States Preventative Services Taskforce, Million Hearts, or Healthy People 2030. Detail how a masters prepared nurse can  utilize the tool kit for a specific population  that is affected by health care inequities. 1 page 2 peer reviewed articles within the last

U23423: Construction Law, Contracts and Dispute Resolution

Task Outline: The task is to prepare a professional report not exceeding 4,000 words in length, referenced appropriately, relating to the contractual remedies and practical solutions which may be available to the project team in the event of a substantial delay caused by an unexpected issue on site as described

What is a job analysis? What data collection methods do

  What is a job analysis? What data collection methods do I-O professionals use to conduct job analyses? Why are job analyses important? What are three best practices for conducting job analyses effectively? How do I-O professionals use job analysis data to help organizations and their clients to improve important

Reply to the two post with two references on each

Reply to the two post with two references on each  Various databases provide reliable and valid information to the researcher depending on their topic of interest. I logged into the Walden University library, clicked on databases, changed the subject to nursing, and on searching, I accessed 15 databases. Out of

Scenario: Abuse • Mr. CO is a 75, year old

  Scenario: Abuse• Mr. CO is a 75, year old male. He was admitted to your unit with Pulmonary Fibrosis. As you are walking towards his room to administer medications you hear his son say to him in a nasty tone of voice: “Stop being so stubborn. I need you

Poverty in the classroom

A four-unit research paper in APA formatting centered around the effects of poverty in education. The first unit needs to be about the effects poverty has on students in the classroom and needs to be 8-10 pages. Second unit is on the effects of substance abuse on students in the

Treatment Planning for Couples and Families Experiencing Pervasive

Identify the pervasive mental illness or health concern present in the couple/family. •Conceptualize the couple’s/family’s problem through your chosen theoretical orientation. •Design a treatment plan including short- and long-term goals. •Explain two evidence-based interventions you would use to address the couple’s/family’s issues and how you would use them. (Note: The

Household debts

Read the two sentences below, from an analysis of trends in household debt published at the end of 2017. “Debt lets households smooth shocks and invest in high-return assets such as housing or education, raising average consumption over their lifetimes. However, high household debt can make the economy more vulnerable

Describe the rationalization the sex offender is stating. Explain what

  Describe the rationalization the sex offender is stating. Explain what you would want to know about the personal and criminal history of the sex offender and why. Explain how you would address these rationalizations and defenses. Explain what you would want to hear from the sex offender. As the