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