0

I am trying to write my own loss function for the UNET. In this function I want to assign the all differences bigger than 10 between y_true and y_pred to 10 and all differences smaller than 1 to 1. is there any convenient way of comparing and assigning to tensors?

def weighted_cross_entropyy(i):
    def loss(y_true, y_pred):
        diff = K.abs(y_true - y_pred)
        diff[K.less(diff, 1)] == 1
        diff[K.greater(diff, 10)] == 10
        return K.mean(K.square((y_pred - y_true)* diff), axis= -1)

    return loss
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
narmin
  • 39
  • 5

1 Answers1

0

i tried to solve the problem, and reached here, but still i am receiving the error below

def weighted_cross_entropyy(i):

def loss(y_true, y_pred):
    def f1():
        return K.mean(K.square(y_pred - y_true), axis= - 1)
    def f2():
        return K.mean(K.square(y_pred - y_true), axis= - 1) * 10
    def f3(w):
        return K.mean(K.square(y_pred - y_true), axis= - 1) * w


    w = K.sqrt(K.sum(K.square(y_true - y_pred), axis=-1))
    print(w)
    r = tf.case([(tf.less(w, 0), f1), (tf.greater(w, 10), f2)], default = f3(w), exclusive=True)

    return r
return loss

the error : Shape must be rank 0 but is rank 3 for 'loss_6/conv2d_168_loss/case/cond/Switch' (op: 'Switch') with input shapes: [?,128,800], [?,128,800].

narmin
  • 39
  • 5