-1

How can I define custom loss functions in Hyperas?

I am running a 4 dense layers architecture with 3 outputs. For the loss, i would like to define it as:

Loss

( (Output1 Error) / max[Output1_pred, Output1_true] ) +
( (Output2 Error) / max[Output2_pred, Output2_true] ) +
( (Output3 Error) / max[Output3_pred, Output3_true] )

Is this trivial to accomplish? How can I index y_pred and y_true in such a case?

Corse
  • 205
  • 2
  • 11
  • for the vectorized form, what does axis = 1 do? – Corse Jan 30 '19 at 04:50
  • so basically the `np.hstack()` will combine both Output_pred and Output_true in a single array, with `np.max` picking out the largest value. so the loss will be just a single scaler using the `np.sum()` function – Corse Jan 30 '19 at 05:55
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187541/discussion-between-anand-v-singh-and-corse). – anand_v.singh Jan 30 '19 at 06:03
  • minor correction `np.amax()` not `np.max()` – anand_v.singh Jan 30 '19 at 06:05

1 Answers1

0

To make a custom loss function in keras follow this Make a custom loss function in keras

Now to vectorize your loss function, so that you don't have extremely slow speed, use vectorization, to do that you can write your loss function as :

loss = np.sum(np.divide(Output_Error,np.amax(np.hstack((Output_pred,Output_true)),axis=1)))

Now let's break this down

np.hstack((M1,M2)) stacks the matrices as [M1|M2] which are both vectors in your case.

np.amax(M,axis = 1) creates a vector of maximum values along x axis i.e. for each row.

np.divide(M1,M2) Performs element wise division across matrices so you will still get a vector.

np.sum(M) provides scalar sum of the matrix M.

To learn more about any of these functions, visit https://docs.scipy.org/doc/numpy-1.15.1/reference/

If you have any queries comment down below.

anand_v.singh
  • 2,768
  • 1
  • 16
  • 35