0

The code below throws the error 'numpy.float64' object is not callable at last_mae = mae(val_scaled_price_client, cv_model). Nevertheless the mae function with the same parameters works just fine when used outside the loop.

loss_history = [1000.00]

for i in range(10000):

  # train
  iterations = 10

  train_auto_encoder(train_latent_customers=train_latentvars,
                   train_product_customers=train_scaled_price_client,
                   auto_encoder=auto_model,
                   iters=iterations,                              
                   batch_size=128,
                   display_step=20)                                  

  cv_model = auto_model.predict([val_latentvars, val_scaled_price_client_corrupted])
  last_mae = mae(val_scaled_price_client, cv_model)          
  loss_history.append(last_mae)

  if loss_history[-1] < loss_history[-2]:
    iterations += 10

  else:
    break

I declared a function mae in previous cells as follow

# define function to calculate MAE between true and reconstructed values
def mae(y_true, y_pred):

    # get non-zero positions
    cond = np.not_equal(y_true, 0)
    # get number of non-zero elements
    num_non_zero = np.sum(cond)
    # initialize zer matrix
    zero_matrix = np.zeros(shape=y_true.shape)
    # replace 
    predictions_corrected = np.where(cond, y_pred, zero_matrix)
    # get rmse
    mae = np.sum(np.abs(y_true - predictions_corrected)) / num_non_zero
    # return
    return(mae)
5
  • It seems the issue is due to the type conversion error. Is there a specific reason why you are declaring loss_history as a float. Commented Jun 14, 2020 at 11:25
  • mae is a number, you use it as a function. Most probably you unintentionally overwrote you function definition of mae with a number. Commented Jun 14, 2020 at 11:27
  • The reason I declared loss_history as a float is just because the mae function returns a float and I thought that might have been the problem. @Stef, I edited the post since I did define a mae function before. Commented Jun 14, 2020 at 11:58
  • As I said: you redefine mae in mae = np.sum(np.abs(y_true - predictions_corrected)) / num_non_zero. Change this to return np.sum(np.abs(y_true - predictions_corrected)) / num_non_zero and everything will be OK :) Commented Jun 14, 2020 at 12:00
  • Indeed, this solved the problem! Thanks a lot! Commented Jun 14, 2020 at 12:12

1 Answer 1

2

The problem is in your mae function. In the last but one row you overwrite your function definition of mae with a number. If you call this function once everything is OK. As soon as you call it again (as in the loop), you try to call a number instead of a function, which is impossible.

Just change

mae = np.sum(np.abs(y_true - predictions_corrected)) / num_non_zero
# return
return(mae)

to

return np.sum(np.abs(y_true - predictions_corrected)) / num_non_zero

Python is not Basic or Fortran where you assign the result to the function name to return it :).

Sign up to request clarification or add additional context in comments.

1 Comment

I had a similar issue. I used the name of a function as a variable. In the end, renaming the variable solved the issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.