# Homework 2

In this homework you will learn how to train your first “deep” network in pytorch. In addition we are going to collect some training data, for the next few assignments.

installation instructions and starter code

## Data Collection

All the training data for the next few weeks will be based on SuperTux.
As the first part of this assignment you should go and play a few levels of supertux.
Each student will get 5 levels assigned (follow the `level-assignment.py`

script to see your assignment).
Play these levels either until completion or until you died 10 times.
Please keep in mind Tux does not commit Harakiri, only serious tries count.
Once you have the traces of the 5 levels, zip them up and submit them on canvas.
Instructions on how to use supertux are here.

## PyTorch assignment: Is a point inside a circle?

Recall from last week that we computed \(\pi \) by testing what fraction of points samples from \([0,1]^2 \) lie within a circle:

^{credit: www.stealthcopter.com}

In this assignment you’ll train a classifier to test if a point is inside a circle. You’ll train both linear and non-linear classifiers for this.

### Linear classifier

First you’ll code up your linear classifier. Write your logic in the `__init__`

and `forward()`

functions of class `MainLinear`

in the `main.py`

file. Note that `__init__`

should declare all the model parameters/layers you need, while forward uses them to compute the regression. Then, train your network by first completing the `train_linear()`

function in the `train.py`

file; once you are done, train and save your model using `python -m homework.train linear`

.

### Deep classifier

At the end of this assignment we will see what the effect of adding more and more layers in the network are. Write your logic in the `__init__`

and `forward()`

functions of class `MainDeep`

in the `main.py`

file. Note that `__init__`

should declare all the model parameters/layers you need, while forward uses them to compute the regression. Then, train your network by first completing the `train_deep()`

function in the `train.py`

file; once you are done, train and save your model using `python -m homework.train deep`

. Don’t worry if you don’t have all the background of how to train deeper networks, just try it and watch what happens. You can get creative with your network structure. Try changing any of the following:

- Number of layers
- Number of units in each layer
- Activation function
- etc...

### Desired output

Your model should take as input a \(k\times2\) tensor and is expected to output a \(k\times2\) tensor that will later be fed into a softmax function. (i.e. no nonlinearity at the last layer). Your training labels should also have the form of \(k\times 2\).

Once you trained your models, you can visualize their outputs using `python3 -m homework.test`

.

For the linear classifer, you should be able to achieve \(95\% \) accuracy. We will grade your model linearly with respect to your accuracy from \(90\%\) to \( 95\%\), e.g. you will get full credit if your model achieves \(\geq 95\% \) accuracy, and half credit if your model achieves \(92\%-93\%\) accuracy.

For the deep classifer, you should be able to achieve \(98\% \) accuracy. We will grade your model linearly with respect to your accuracy from \(95\%\) to \(98\%\), e.g. you will get full credit if your model achieves \(\geq 98\% \) accuracy, and 1/4 credit if your model achieves \(95\%-96\%\) accuracy.

### Relevant operations

### Explicitly prohibited operations

You are not allowed to use methods introduced in hw1 as decision rule

- asin
- acos
- atan
- mean
- sqrt
- sum
- constants
- comparisons (e.g. smaller than, greater than)

Note that you are allowed to use constants and above operations during training (e.g. learning rate), however you should not use them in your model logic.

### Submission

Step 1: Write all your logic in the `forward()`

and `__init__`

functions of the `homework/main.py`

, and `train_linear`

and `train_deep`

functions of the `homework/train.py`

file provided in the starter code.

Step 2: Train your model.

```
python3 -m homework.train
```

Step 3: Visualize the outputs of your model

```
python3 -m homework.test
```

This step requires `matplotlib`

. Install it using `conda`

or `pip3`

.

Step 4: Run the grader to test your model.

```
python3 -m grader homework
```

Step 5: Create a submission file

```
python3 -m homework.bundle
```

Note that the grader you have access to is a weaker version of our final grader. Test your program extensively before submission.