gpt4 book ai didi

python-3.x - 在 tensorflow 2.1.0 中强制立即执行

转载 作者:行者123 更新时间:2023-12-04 10:00:20 24 4
gpt4 key购买 nike

我是 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()) ,它返回真,但在函数内,它返回假。

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

-通过 run_eagerly = True在 model.compile() 函数中

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

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

以上修复均无效。

最佳答案

我能够重现该问题,如下所示。您可以从 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.



如果我们修改 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))

输出 -
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%

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

关于python-3.x - 在 tensorflow 2.1.0 中强制立即执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61843285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com