Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

### Task 2 . 2 - Backward propagation In this task, you will start with random weights for ` w 0 ` and ` w

### Task 2.2- Backward propagation
In this task, you will start with random weights for `w0` and `w1`, and iteratively perform forward passes and backward propagation multiple times to converge on a solution.
Submit your values of `w0`,`w1`, and `loss` value onto Coursemology. Your `loss` value should be less than 1.
torch.manual_seed(1) # Set seed to some fixed value
w0= torch.randn(2,2, requires_grad=True)
w1= torch.randn(3,1, requires_grad=True)
learning_rate =1e-3
print('iter', 'loss', '
----','----', sep='\t')
for t in range(1,10001):
# Forward pass: compute predicted y
y_pred = forward_pass(x, w0, w1, torch.relu)
loss = torch.mean(torch.square(y - y_pred))
loss.backward()
if t %1000==0:
print(t, loss.item(), sep='\t')
with torch.no_grad():
# Update weights and then reset the gradients to zero
raise NotImplementedError
print("--- w0---", w0, sep='
')
print("--- w1---", w1, sep='
')
y_pred = forward_pass(x, w0, w1, torch.relu)
plt.plot(x, y, linestyle='solid', label='|x-1|')
plt.plot(x, y_pred.detach().numpy(), linestyle='dashed', label='perceptron')
plt.axis('equal')
plt.title('Fit NN on abs function')
plt.legend()
plt.show()
# Task 5: Submit the values of `w0`,`w1`, and `loss` values after fitting
# Note: An acceptable loss value should be less than 1.0
# You should try adjusting the random seed, learning rate, or
# number of iterations to improve your model.
w0=[[0.0,0.0],[0.0,0.0]] # to be computed
w1=[[0.0],[0.0],[0.0]] # to be computed
loss =0.0 # to be computed
w0= torch.tensor(w0)
w1= torch.tensor(w1)
x = torch.linspace(-10,10,1000).reshape(-1,1)
y = torch.abs(x-1)
#IMPORTANT: Your forward pass above have to be correctly implemented
y_pred = forward_pass(x, w0, w1, torch.relu)
computed_mse_loss = torch.mean(torch.square(y - y_pred)).item()
assert loss <1
assert isclose(computed_mse_loss, loss, atol=1e-5, rtol=1e-2)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions