Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The train ( . . . ) function will implement batch qradient descent to train the model. Use PyTorch autograd to estimate gradients and update

The train(...) function will implement batch qradient descent to train the model.
Use PyTorch autograd to estimate gradients and update parameters during gradient descent.
def train(model,x,y, num_epochs=20, learning_rate=0.1, seed=1, log=False):
cost=[]
torch.manual_seed(seed)
for ii in range(num_epochs):
## Forward Propagation and loss ##
### BEGIN SOLUTION ###
=
### END SOLUTION ###
## Compute gradient and update parameters ##
## The grad(.) function gets the gradients of the loss w.r.t parameters in a list
## The output gradients is a list of corresponding parameter gradients
gradients = grad(105s, list(model.parameters.values()))
## Whenever a parameter changes during an operation,
## a graph is maintained to estimate the gradient
## PyTorch does not allow to modify such
## parameters in place (for e.g. assigning an arbitrary value to the parameter)
## Becasue such operations will not be able to estimate the gradient
## We therefore turn-off the gradient during parameter update
with torch.no_grad():
for ly in range(model.num_layers):
## Access the gradients list to get dw and db
## and update parameters using -learing_ratedb
## For e.g.
## gradients [0] is the gradient for W1
## gradients[1] is the gradient for b1
## gradients[2] is the gradient for W2
## gradients[3] is the gradient for b2
## gradients[2*1y] is the gradient for w(1y+1)
## gradients 2**1y+1 is the gradient for b(1y+1)
### BEGIN SOLUTION ###
### END SOLUTION ###
## Print loss every epoch ##
## We do not need to track gradient during the evaluation
cost.append (loss.detach())
if not ii%20:
print('Epoch: %02d | Loss: %.5f'%((ii+1),1055))
image text in transcribed

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

Professional SQL Server 2012 Internals And Troubleshooting

Authors: Christian Bolton, Justin Langford

1st Edition

1118177657, 9781118177655

More Books

Students also viewed these Databases questions