0

I am trying to use a custom Huber loss function for my regression problem and train a VGG16 model to generate bounding box coordinates. The code is given below:

def huber(true, pred, delta=1.0):
    loss = np.where(np.abs(true-pred) < delta , 0.5*((true-pred)**2), delta*np.abs(true - pred) - 0.5*(delta**2))
    return np.sum(loss)

...
...
...
model.compile(optimizer=Adam(lr=0.001), loss=huber)

Upon running the script, I get an error with the use of the custom Huber loss function:

model.compile(optimizer=Adam(lr=0.001), loss=huber)

  File "C:\Users\...\lib\site-packages\keras\engine\training.py", line 342, in compile
    sample_weight, mask)

  File "C:\Users\...\lib\site-packages\keras\engine\training_utils.py", line 404, in weighted
    score_array = fn(y_true, y_pred)

  File "C:\Users\...\train_code.py", line 157, in huber
    loss = np.where(np.abs(true-pred) < delta , 0.5*((true-pred)**2), delta*np.abs(true - pred) - 0.5*(delta**2))

  File "<__array_function__ internals>", line 6, in where

ValueError: setting an array element with a sequence.
shiva
  • 1,177
  • 2
  • 14
  • 31
  • Does this answer your question? [ValueError: setting an array element with a sequence](https://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence) – o-90 Feb 26 '21 at 18:22
  • @gobrewers14 I tried to declare the array as a float as suggested but it does not help. – shiva Feb 26 '21 at 18:25
  • 1
    Can you share some reproducible code?? – Innat Feb 26 '21 at 18:28

0 Answers0