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.