Question: 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))
 The train(...) function will implement batch qradient descent to train the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!