Login using Social Account
     Continue with GoogleLogin using your credentials
The Optimize function:
Copy-paste the following code for the optimize
function.
Call the propagate
function, predict
function and get_accuracies
function at the appropriate places inside the optimize
function.
def optimize(w, b, X, Y, X_val, Y_val, num_iterations, learning_rate):
prev_train_acc=0
prev_val_acc=0
costs=[]
epoch=0
final_w = w
final_b = b
for i in range(num_iterations):
# Call Propagate function to get Cost and gradients
grads, cost = << your code comes here >>(w, b, X, Y)
costs.append(cost)
# Get derivatives
dw = grads['dw']
db = grads['db']
# Update rule
w = w - learning_rate * dw
b = b - learning_rate * db
# Predict labels for train and validation sets
Y_prediction_train = << your code comes here >>(w, b, X)
Y_prediction_val = << your code comes here >>(w, b, X_val)
# Get accuracies for the train and validation predictions
train_acc = << your code comes here >>(Y_prediction_train , Y)
val_acc = << your code comes here >>(Y_prediction_val, Y_val)
if val_acc > prev_val_acc and train_acc>=val_acc:
print("*****************************")
print("Epoch - {} - train accuracy: {} %".format(i,train_acc))
print("Epoch - {} - val accuracy: {} %".format(i,val_acc))
print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
prev_train_acc = train_acc
prev_val_acc = val_acc
epoch = i
final_w = w
final_b = b
params = {'w': w, 'b': b}
grads = {'dw': dw, 'db': db}
optimal_values = {
'costs': costs,
'final w':final_w,
'final b':final_b,
'epoch':epoch,
'Train accuracy':prev_train_acc,
'Validation accuracy': prev_val_acc,
'Y_prediction_val': Y_prediction_val,
'Y_prediction_train': Y_prediction_train,
'params':params,
'grads':grads,
}
return optimal_values
Taking you to the next exercise in seconds...
Want to create exercises like this yourself? Click here.
Note - Having trouble with the assessment engine? Follow the steps listed here
Loading comments...