我是整个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()
至少强制那里渴望执行。
以上修复程序均无效。
答案 0 :(得分:1)
我能够如下重现该问题。您可以从here下载程序中我正在使用的数据集。我在程序中添加了List<Team> sortedList = teams.stream()
.sorted( (t1, t2) -> t2.getRanking().compareTo(t1.getRanking()) )
.collect(Collectors.toList());
语句以跟踪更改。
代码-
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))
解决方案-按照documentation。它提到,
run_eagerly-可设置属性,指示模型是否应快速运行。 热情地运行意味着您的模型将像Python代码一样一步一步地运行。您的模型运行速度可能较慢,但是通过逐步进入各个层调用,您应该可以更轻松地对其进行调试。 默认情况下,我们将尝试将您的模型编译为静态图,以提供最佳的执行性能。
如果我们使用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%
参数修改model.compile
,则可以解决此问题。下面显示的是修改后的run_eagerly = True
代码,
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))
希望这能回答您的问题。学习愉快。