-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmlp_bp.py
More file actions
23 lines (15 loc) · 700 Bytes
/
mlp_bp.py
File metadata and controls
23 lines (15 loc) · 700 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Implement this algorithm using pytorch
import torch
# Assuming we have a neural network model `model` and a loss function `criterion`
# Assuming we have input data `x` and target `y`
# Forward pass to compute output and loss
output = model(x)
loss = criterion(output, y)
# Initialize the gradient table (grad_table) as a list of tensors
grad_table = [None] * len(list(model.parameters()))
# Perform the backward pass to compute gradients
loss.backward()
# Fill the grad_table with the gradients of the loss with respect to each parameter
for i, param in enumerate(model.parameters()):
grad_table[i] = param.grad
# The grad_table now contains the gradients dL/dw for each parameter w(i)