keras custom metric for path planning neural network doesnt work

Viewed 63

Firstly some info about the background of the problem. I am building an artificial neural network solving an AGV path planning problem. The input of the network is a 15x15 grid terrain of 0,1 , meaning that 0 is empty space for the robot to go and 1 is an obstacle. There are 5 fixed destinations for all samples. The only thing changing is the terrain. My output is path between the start and destinations. I want to use a custom metric that measures how many obstacles are being hit and evaluate the produced path. Below is the code. I have already made an obs list in which, obs[0][sample_index]->obstacles of the train_sample[index] and obs[1][sample_index]->obstacles of the test_sample[index]. However the after the compile the model doesnt fit. Can someone explain to me what is the problem with the custom metric?

def index(y):
    if y in y_train:
        a=y_train.index(y)
    elif y in y_test:
        a=y_test.index(y)
    return a

def custom_metric(y_true, y_pred):

    ind=index(y_true)

    obs_in_ypred=0


    if y_true in y_train:
        for i in range(0,len(y_pred),2):
            if y_pred[i]*15+y_pred[i+1] in obs[0][ind]:
                obs_in_ypred=obs_in_ypred+1
    elif y_true in y_test:
        for i in range(0,len(y_pred),2):
            if y_pred[i]*15+y_pred[i+1] in obs[1][ind]:
                obs_in_ypred=obs_in_ypred+1
        
    metric1=1-obs_in_ypred/(len(obs[0][ind]))
    metric2=1-obs_in_ypred/(len(obs[1][ind]))

    return tf.keras.backend.in_train_phase(metric1,metric2)
1 Answers

you can't have if statement inside you loss function, the if has no gradient, just think about what's the derivative of if, i'm also fairly sure the for is no good either.

If the output is a path consider reproducing this path on a 2D tensor ( where maybe cells put to 1 give the path)

Related