【问题标题】:Forcing eager execution in tensorflow 2.1.0在 tensorflow 2.1.0 中强制执行急切
【发布时间】:2020-05-16 20:42:40
【问题描述】:

我是 TensorFlow 和深度学习的新手。

我创建了一个自定义损失函数,但似乎在自定义损失函数中,未启用急切执行。下面是我的自定义损失函数(它不起作用):

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

让我难过的是,在这个函数之外,每当我运行 print(tf.executing_eagerly()) 时,它都会返回 true,但在函数内部,它会返回 false。

我已经尝试了所有我能找到的修复方法:

-在model.compile()函数中传递run_eagerly = True

-在编译函数后添加model.run_eagerly() = True

-在损失函数中运行tf.compat.v1.enable_eager_execution() 至少强制执行一次急切。

以上修复均无效。

【问题讨论】:

    标签: python-3.x tensorflow keras eager-execution


    【解决方案1】:

    我能够重现该问题,如下所示。您可以从here 下载我在程序中使用的数据集。我在程序中添加了print("tf.executing_eagerly() Results") 语句来跟踪更改。

    代码 -

    %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))
    

    输出 -

    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%
    

    解决方案 - 根据documentation。它提到,

    run_eagerly - 指示模型是否应该急切运行的可设置属性。 急切地运行意味着您的模型将像 Python 代码一样逐步运行。您的模型可能会运行得更慢,但通过单步调用单独的层调用,您应该可以更轻松地对其进行调试。 默认情况下,我们会尝试将您的模型编译为静态图以提供最佳执行性能。

    如果我们用run_eagerly = True 参数修改model.compile,我们可以解决这个问题。下图为修改后的model.compile代码,

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

    固定代码 -

    %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))
    

    输出 -

    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%
    

    希望这能回答您的问题。快乐学习。

    【讨论】:

    • 非常感谢您的回答,但我已经尝试过了。编译器基本上完全忽略了这个参数。即使我犯了一个错误,编译器也不会返回一个。另外,有什么方法可以通过邮件联系到您吗?我觉得这样解决这个问题会更快......
    • 但这对我们在 Google Colab 中的效果非常好。建议您在 colab 中运行程序并查看错误是否仍然存在。如果在 Colab 中遇到同样的错误,请随时与我们联系。如果上述答案令人满意,请您接受并投票。谢谢。
    猜你喜欢
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2020-08-28
    • 2010-12-26
    • 1970-01-01
    相关资源
    最近更新 更多