# 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

e) Create the main loop that goes over the dataset in multiple epochs. In each epoch
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
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).

Pages (275 words)
Standard price: \$0.00

### Latest Reviews

Impressed with the sample above? Wait there is more

Related Questions

### 4 DQ 1 An effective leader advocates for their staff

4 DQ 1 An effective leader advocates for their staff whenever the need arises. Leaders must address staff needs, requests, problems, and rights. Nursing leaders

### Pick an ethical or legal issue in nursing leadership. You Nursing Assignment Help

Client #231095 #Pick #ethical #legal #issue #nursing #leadership #Nursing #Assignment

### Assignment Details This assignment requires you to use Excel to answer the questions and create the charts as specified in

Assignment DetailsThis assignment requires you to use Excel to answer the questions and create the charts as specified in the file below. However, when presenting

### CTU Similarities & Differences between AMC & Acute Care Community Hospitals Essay

Deliverable Length: 1–2 pages, not including a title or reference page Healing Hands Hospital is an acute care community hospital that serves a suburban community

### Benchmark – Control PowerPoint Presentation Create a 15-20 slide PowerPoint

Benchmark – Control PowerPoint Presentation Create a 15-20 slide PowerPoint presentation (including speaker notes or audio notes) that illustrates internal controls in your environment

### A client with anxiety, Insomnia, bipolar and depression. As a

A client with anxiety, Insomnia, bipolar and depression. As a psych therapist, discuss on the following topics:  Anxiety: A. Vulnerability (factors in a person’s

### NSTRUCTIONS: Data File and Data Dictionary This assignment must be

NSTRUCTIONS: Data File and Data Dictionary This assignment must be completed individually and should not be discussed with other students or on the Discussion

### ACTIONX is a project designed to enhance your skills as

ACTIONX is a project designed to enhance your skills as a diversity leader through engaging in the Journey of Self-Discovery activities and a

### CSU Health Information Management Discussion

I’m working on a health & medical discussion question and need the explanation and answer to help me learn. Explain the importance of having legal

### Sample Output Original Polyhedra – [Sphere] (2, 2, 2)->Radius: 1

Sample Output Original Polyhedra——————————————————[Sphere] (2, 2, 2)->Radius: 1 Diameter: 2[Cylinder] (2, 2, 2)->Radius: 1 Height: 2[Sphere] (8, 8, 8)->Radius: 4 Diameter: 8[Cylinder] (6, 6, 2)->Radius:

### 4 DQ 1 An advocate is defined as one that

4 DQ 1 An advocate is defined as one that pleads, defends, or supports a cause or interest of another. Nurse leaders can act as

### Project: Scholar-Practitioner Project: Health Care Administration Leadership Theory For your final

Project: Scholar-Practitioner Project: Health Care Administration Leadership Theory For your final written Assignment, you refine and develop your Health Care Administration Leadership Theory based

New questions

### Please compose a power point presentation that will include Nursing Assignment Help

Please compose a power point presentation that will include the following components: the paper is attached  Topic Background Problem Statement Research Design Data Collection What

### ACU Health & Medical Navigating Telehealth Perspectives Nursing Assignment Help

Instructions:  Select three people and interview them about their experiences and perceptions using telehealth services.  One of the interviewees must work in healthcare; however, the

### WU An Evaluation of The Dilemma at Benevento Foods Essay Nursing Assignment Help

AN EVALUATION OF THE DILEMMA AT BENEVENTO FOODS: UNDERSTANDING CAUSE AND EFFECT AND RECOMMENDATIONS FOR IMPROVEMENT For this Assignment, you will continue to evaluate the

### Shortness of Breath with Exertion Discussion Nursing Assignment Help

Please write a 1-2 paragraph response to each of these. Please include one reference for each response CASE STUDY 1 – RESPONSE Mason is a

### Based on Chapter 18, discuss the most common Claims and Nursing Assignment Help

Based on Chapter 18, discuss the most common Claims and Benefits Administration problems, their repercussions, and how to identify and rectify them. Based on Chapter