I want to define my own loss function in keras , and the bce_loss
multiply with a varible W
. Actually, W
has the same shape with the tensor bce_loss
.
If I print the tensor bce_loss
, maybe it can be shown as follow:
Tensor("loss_8/activation_14_loss/logistic_loss:0", shape=(?, 3), dtype=float32)
Now I don't know how to get the shape of bce_loss
, and make the varible W
has the the same of bce_loss
.
My code:
def myLoss(y_true, y_pred):
bce_loss = K.binary_crossentropy(y_true, y_pred)
# want to get a variable W with the same shape of bce_loss
# And W is initialized with normal distribution.
val = np.random.normal(0, 0.05, size= bce_loss.size())
W = keras.variable( val )
return K.mean(self.W*bce_loss, axis = -1)