0

I'm trying to create a custom loss function for Keras to use and I'm having some trouble. Following this post here: Custom loss function in Keras I know the syntax for creating the function, but I'm not familiar with how to work with tensors. I filled yTrue with scalars and yPred is the actual predicted values. I want to take the weighted sum of the logs of the predicted values, weighted by the scalars in yTrue. When I do something like this:

def customLoss(yTrue,yPred):
        L = 0
        for i in range(len(yTrue)):
            L += tf.math.scalar_mul(yTrue[i], K.log(yPred[i]))
        return L

The program crashes when I try to compile the model with a custom loss function, because it seems to be passing some tensors and running the loss function when I do model.compile. Printing out yTrue and yPred, I get Tensor("dense_4_target:0", shape=(?, ?), dtype=float32) Tensor("dense_4/Softmax:0", shape=(?, 4), dtype=float32) Which tells me that I have to make my custom loss function work with tensors.

I've tried return K.sum(K.prod(yTrue,K.log(yPred))) but I get

    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1722, in reduce_prod
    name=name))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 6239, in prod
    name=name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 610, in _apply_op_helper
    param_name=input_name)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 60, in _SatisfiesTypeConstraint
    ", ".join(dtypes.as_dtype(x).name for x in allowed_list)))

which I don't find very helpful

Zyzzyphus
  • 5
  • 3
  • Is this the error message you got ? Does look like though. A year back I did work with creating custom loss fn in keras. In you are customLoss you are using `tf.math` module or you do `import keras.backend as K`? – amitoz Mar 19 '19 at 02:15
  • That is an error message that I got. And I did import keras.backend as K – Zyzzyphus Mar 19 '19 at 02:32

1 Answers1

0

I figured out how to do it. I pass to model.fit(x=states,y=G) where G is an array that matches the size of yPred. These are where I put the values of my scalars. Then my loss function could just be return K.sum(K.sum(yTrue*K.log(yPred)))

Zyzzyphus
  • 5
  • 3