在 tensorflow 2.1.0 中强制急切执行
Forcing eager execution in tensorflow 2.1.0
我对 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())
时,它 returns 为真,但在函数内,它 returns 为假。
我已经尝试了我能找到的所有修复方法:
-在model.compile()函数中传递run_eagerly = True
-在编译函数
后添加model.run_eagerly() = True
-运行ning tf.compat.v1.enable_eager_execution()
在损失函数中至少强制执行一次。
None 以上修复工作。
我能够重现问题如下。你可以从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 - 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.
如果我们用 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%
希望这能回答您的问题。快乐学习。
我对 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())
时,它 returns 为真,但在函数内,它 returns 为假。
我已经尝试了我能找到的所有修复方法:
-在model.compile()函数中传递run_eagerly = True
-在编译函数
后添加model.run_eagerly() = True
-运行ning tf.compat.v1.enable_eager_execution()
在损失函数中至少强制执行一次。
None 以上修复工作。
我能够重现问题如下。你可以从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 - 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.
如果我们用 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%
希望这能回答您的问题。快乐学习。