python-3.xtensorflowkeraseager-execution

Forcing eager execution in tensorflow 2.1.0


I am new to tensorflow and Deep Learning as a whole.

I have created a custom loss function but it seems that within the custom loss function, eager execution is not enabled. Below is my custom loss function (it doesn't work):

def custom_error_finder(y_actual,y_pred):
    print(tf.executing_eagerly())
    count = 0
    qw = tf.py_function((y_actual).numpy())
    ya = ((y_actual[0].numpy()).decode())
    yp = ((y_pred[0].numpy()).decode())
    for i,j in ya,yp:
        if i!=j:
            count = count+1
    mse = pow(count,2)/len(ya)
    return mse

What stumps me is that outside this function, whenever I run print(tf.executing_eagerly()), it returns true but within the function, it returns false.

I have tried all the fixes I could find:

-passing run_eagerly = True in the model.compile() function

-adding model.run_eagerly() = True after the compile function

-running tf.compat.v1.enable_eager_execution() within the loss function to at least force eager execution once there.

None of the above fixes work.


Solution

  • I was able to reproduce the issue as below. You can download the dataset I am using in the program from here. I have added print("tf.executing_eagerly() Results") statements in the program to track the changes.

    Code -

    %tensorflow_version 2.x
    import tensorflow as tf
    print(tf.__version__)
    import numpy as np
    from numpy import loadtxt
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    from tensorflow.keras import backend as K
    
    print("tf.executing_eagerly() Results")
    print("Before loading dataset :",tf.executing_eagerly())
    
    # load pima indians dataset
    dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
    
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    
    # define model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    
    print("After building model :",tf.executing_eagerly())
    
    def weighted_binary_crossentropy(y_true, y_pred):
      print("In loss function :",tf.executing_eagerly())
      return K.mean(K.binary_crossentropy(y_pred, y_true))
    
    # compile model
    model.compile(loss=weighted_binary_crossentropy, optimizer='adam', metrics=['accuracy'])
    
    print("After compiling model :",tf.executing_eagerly())
    
    # Fit the model
    model.fit(X, Y, epochs=1, batch_size=150, verbose=0)
    
    # evaluate the model
    scores = model.evaluate(X, Y, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    

    Output -

    2.2.0
    tf.executing_eagerly() Results
    Before loading dataset : True
    After building model : True
    After compiling model : True
    In loss function : False
    In loss function : False
    In loss function : False
    accuracy: 34.90%
    

    Solution - As per the documentation. It mentions that,

    run_eagerly - Settable attribute indicating whether the model should run eagerly. Running eagerly means that your model will be run step by step, like Python code. Your model might run slower, but it should become easier for you to debug it by stepping into individual layer calls. By default, we will attempt to compile your model to a static graph to deliver the best execution performance.

    We can fix the issue if we modify the model.compile with run_eagerly = True argument. Shown below is the modified model.compile code,

    model.compile(loss=weighted_binary_crossentropy, run_eagerly = True, optimizer='adam', metrics=['accuracy'])
    

    Fixed Code -

    %tensorflow_version 2.x
    import tensorflow as tf
    print(tf.__version__)
    import numpy as np
    from numpy import loadtxt
    from tensorflow.keras.models import Sequential
    from tensorflow.keras.layers import Dense
    from tensorflow.keras import backend as K
    
    print("tf.executing_eagerly() Results")
    print("Before loading dataset :",tf.executing_eagerly())
    
    # load pima indians dataset
    dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
    
    # split into input (X) and output (Y) variables
    X = dataset[:,0:8]
    Y = dataset[:,8]
    
    # define model
    model = Sequential()
    model.add(Dense(12, input_dim=8, activation='relu'))
    model.add(Dense(8, activation='relu'))
    model.add(Dense(1, activation='sigmoid'))
    
    print("After building model :",tf.executing_eagerly())
    
    def weighted_binary_crossentropy(y_true, y_pred):
      print("In loss function :",tf.executing_eagerly())
      return K.mean(K.binary_crossentropy(y_pred, y_true))
    
    # compile model
    model.compile(loss=weighted_binary_crossentropy, run_eagerly = True, optimizer='adam', metrics=['accuracy'])
    
    print("After compiling model :",tf.executing_eagerly())
    
    # Fit the model
    model.fit(X, Y, epochs=1, batch_size=150, verbose=0)
    
    # evaluate the model
    scores = model.evaluate(X, Y, verbose=0)
    print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    

    Output -

    2.2.0
    tf.executing_eagerly() Results
    Before loading dataset : True
    After building model : True
    After compiling model : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    In loss function : True
    accuracy: 34.90%
    

    Hope this answers your question. Happy Learning.