keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
62.16k stars 19.49k forks source link

How do we get the specific value of the dimension of y_true when we customize the loss function? #12680

Closed yansudong closed 5 years ago

yansudong commented 5 years ago

When I customize the loss function, I want to get the specific value of the y_true dimension. I tried y_true.shape, y_true.get_shape(), y_true.get_shape().as_list(), and K.shape(y_true), but Will report an error because of data format and other issues. Does anyone have relevant experience? Who can help me? thank you very much! image

yansudong commented 5 years ago

This is my custom loss function:

def my_newloss(y_true, y_pred, alpha=0.8):
    mseloss = K.mean(K.square(y_pred - y_true), axis=-1)
    num = (K.int_shape(y_true)[0]) * (K.int_shape(y_true)[1])
    samm = K.zeros(num)
    n = 0
    for x in range(K.int_shape(y_true)[0]):
        for y in range(K.int_shape(y_true)[1]):
            z = K.reshape(y_pred[ x, y,:], [-1])
            sa = K.reshape(y_true[x, y,:], [-1])
            tem1=K.dot(z,sa)
            tem2=(K.linalg.norm(z))*(K.linalg.norm(sa))
            samm[n]=K.acos(tem1/tem2)
            n=n+1
    SAM=(K.mean(samm))*180/math.pi
    samloss = SAM
    return alpha*mseloss + (1-alpha)*samloss

Who can help me solve it? The main problem is the calculation and coding of num and samm.

ymodak commented 5 years ago

Hi! This issue isn't related to a bug/enhancement/feature request or other accepted types of issue. To ask questions, please see the following resources : StackOverflow Keras Slack channel Keras.io support Gitter Thanks! If you think we made a mistake, please open another issue explaining your request in detail.