Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ python migrate_kernel.py --hw1-dir <hw1 path> --hw2-dir <hw2 path>

Implement automatic differentiation. We have provided the derivative operations for internal Python operators in `minitorch.Function.backward` call. Your task is to write the two core functions needed for automatic differentiation: `topological_sort` and `backpropagate`. This will allow us to traverse the computation graph and compute the gradients along the way.

Complete the following functions in `minitorch/autodiff.py`. The places where you need to fill in your code are highlighted with `BEGIN ASSIGN2_1` and `END ASSIGN2_1`
Complete the following functions in `minitorch/autodiff.py`. The places where you need to fill in your code are highlighted with `BEGIN HW2_1` and `END HW2_1`

**Note**: Be sure to checkout the functions in `class Variable(Protocol)`!

Expand Down Expand Up @@ -134,7 +134,7 @@ python -m pytest -l -v -k "autodiff"

## Problem 2: Neural Network Architecture (30 points)

In this section, you will implement the neural network architecture. Complete the following functions in `run_sentiment.py` under the project folder. The places where you need to fill in your code are highlighted with `BEGIN ASSIGN2_2` and `END ASSIGN2_2`.
In this section, you will implement the neural network architecture. Complete the following functions in `run_sentiment.py` under the project folder. The places where you need to fill in your code are highlighted with `BEGIN HW2_2` and `END HW2_2`.

### 1. Implement Linear layer

Expand Down Expand Up @@ -197,7 +197,7 @@ python -m pytest -l -v -k "network"

## Problem 3: Training and Evaluation (30 points)

In this section, you will implement codes for training and perform training on a simple MLP for the sentence sentiment classification task. The places where you need to fill in your code are highlighted with `BEGIN ASSIGN2_3` and `END ASSIGN2_3`.
In this section, you will implement codes for training and perform training on a simple MLP for the sentence sentiment classification task. The places where you need to fill in your code are highlighted with `BEGIN HW2_3` and `END HW2_3`.

### 1. Implement cross entropy loss function

Expand Down
8 changes: 4 additions & 4 deletions minitorch/autodiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ def topological_sort(variable: Variable) -> Iterable[Variable]:
2. When the children nodes of the current node are visited, add the current node
at the front of the result order list.
"""
# BEGIN ASSIGN2_1
# BEGIN HW2_1
# TODO

raise NotImplementedError("Task Autodiff Not Implemented Yet")
# END ASSIGN2_1
# END HW2_1


def backpropagate(variable: Variable, deriv: Any) -> None:
Expand All @@ -128,11 +128,11 @@ def backpropagate(variable: Variable, deriv: Any) -> None:
2. If the node is a leaf, the derivative should be accumulated
3. Otherwise, the derivative should be propagated via chain rule
"""
# BEGIN ASSIGN2_1
# BEGIN HW2_1
# TODO

raise NotImplementedError("Task Autodiff Not Implemented Yet")
# END ASSIGN2_1
# END HW2_1


@dataclass
Expand Down
28 changes: 14 additions & 14 deletions project/run_sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def binary_cross_entropy_loss(out, y):
y: true labels [0, 1] (batch_size,)
"""

# BEGIN ASSIGN2_3
# BEGIN HW2_3
# TODO
# 1. Create ones tensor with same shape as y
# 2. Compute log softmax of out and (ones - out)
Expand All @@ -41,13 +41,13 @@ def binary_cross_entropy_loss(out, y):

raise NotImplementedError("cross_entropy_loss not implemented")

# END ASSIGN2_3
# END HW2_3

class Linear(minitorch.Module):
def __init__(self, in_size, out_size):
super().__init__()

# BEGIN ASSIGN2_2
# BEGIN HW2_2
# TODO
# 1. Initialize self.weights to be a random parameter of (in_size, out_size).
# 2. Initialize self.bias to be a random parameter of (out_size)
Expand All @@ -56,13 +56,13 @@ def __init__(self, in_size, out_size):

raise NotImplementedError("Linear not implemented")

# END ASSIGN2_2
# END HW2_2

def forward(self, x):

batch, in_size = x.shape

# BEGIN ASSIGN2_2
# BEGIN HW2_2
# TODO
# 1. Reshape the input x to be of size (batch, in_size)
# 2. Reshape self.weights to be of size (in_size, self.out_size)
Expand All @@ -72,7 +72,7 @@ def forward(self, x):

raise NotImplementedError("Linear forward not implemented")

# END ASSIGN2_2
# END HW2_2



Expand All @@ -99,14 +99,14 @@ def __init__(
self.embedding_dim = embedding_dim
self.dropout_prob = dropout_prob

# BEGIN ASSIGN2_2
# BEGIN HW2_2
# TODO
# 1. Construct two linear layers: the first one is embedding_dim * hidden_dim
# the second one is hidden_dim * 1

raise NotImplementedError("Network not implemented")

# END ASSIGN2_2
# END HW2_2



Expand All @@ -115,7 +115,7 @@ def forward(self, embeddings):
embeddings tensor: [batch x sentence length x embedding dim]
"""

# BEGIN ASSIGN2_2
# BEGIN HW2_2
# TODO
# 1. Average the embeddings on the sentence length dimension to obtain a tensor of (batch, embedding_dim)
# 2. Apply the first linear layer
Expand All @@ -126,7 +126,7 @@ def forward(self, embeddings):

raise NotImplementedError("Network forward not implemented")

# END ASSIGN2_2
# END HW2_2


# Evaluation helper methods
Expand Down Expand Up @@ -209,7 +209,7 @@ def train(
):
out=None

# BEGIN ASSIGN2_3
# BEGIN HW2_3
# TODO
# 1. Create x and y using minitorch.tensor function
# 2. Get the model output (as out)
Expand All @@ -219,7 +219,7 @@ def train(

raise NotImplementedError("SentenceSentimentTrain train not implemented")

# END ASSIGN2_3
# END HW2_3


# Save training results
Expand All @@ -233,7 +233,7 @@ def train(
(X_val, y_val) = data_val
model.eval()

# BEGIN ASSIGN2_3
# BEGIN HW2_3
# TODO
# 1. Create x and y using minitorch.tensor function
# 2. Get the output of the model
Expand All @@ -242,7 +242,7 @@ def train(

raise NotImplementedError("SentenceSentimentTrain train not implemented")

# END ASSIGN2_3
# END HW2_3

model.train()

Expand Down