C2_W2_Assignment Exercise 6

I am getting errors with this code:

def gradient_descent(dEdm, dEdb, m, b, X, Y, learning_rate = 0.001, num_iterations = 1000, print_cost=False):
for iteration in range(num_iterations):
### START CODE HERE ### (~ 2 lines of code)
m_new = m-(learning_ratedEdm)
b_new = b-(learning_rate
dEdb)
### END CODE HERE ###
m = m_new
b = b_new
if print_cost:
print (f"Cost after iteration {iteration}: {E(m, b, X, Y)}")

return m, b

The error is:


TypeError Traceback (most recent call last)
Cell In [28], line 1
----> 1 print(gradient_descent(dEdm, dEdb, 0, 0, X_norm, Y_norm))
2 print(gradient_descent(dEdm, dEdb, 1, 5, X_norm, Y_norm, learning_rate = 0.01, num_iterations = 10))

Cell In [27], line 4, in gradient_descent(dEdm, dEdb, m, b, X, Y, learning_rate, num_iterations, print_cost)
1 def gradient_descent(dEdm, dEdb, m, b, X, Y, learning_rate = 0.001, num_iterations = 1000, print_cost=False):
2 for iteration in range(num_iterations):
3 ### START CODE HERE ### (~ 2 lines of code)
----> 4 m_new = m-(learning_ratedEdm)
5 b_new = b-(learning_rate
dEdb)
6 ### END CODE HERE ###

TypeError: unsupported operand type(s) for *: ‘float’ and ‘function’

Could you provide me some help or hint?

The error here might be in the learning_ratedEdm if that is function you need to call it like function(parameters)…and will produce some output that you can use further on.

1 Like

I tried many ways (np.dot(_)) but still got an error. Please tell me how to fix it

@gent.spah Please help me how to figure out

You should not publish code solution here as its against code of conduct.

Your error seems to be the same as above, make sure that dEdm which seems to be a function is assigned to a variable and multiplication can happen.

Other than this I dont have access to this course’s assignment.

Dear Carlos,

Let me see if I can help.

According to what I see on your code. There is an issue with m_new and b_new.

Remember that for gradient descend you need to do the following equation:
m_new = m - learning_rate*dEdm
but dEdm is a function defined on python which means you need to call it as a function and inser the parameters over there.

In order to call a function you need to use parenthesis and include the parameters on the parenthesis something like this:

dEdm(m, b, X, Y)

The same applies for the function dEdb(m, b, X, Y)

I hope this helps!

2 Likes