在模型训练后 运行 时,Tensorflow 模型预测失败

Tensorflow model prediction failes when ran right after model training

我的模型预测有问题。训练工作正常,但之后我的程序在预测训练模型时失败了。当我重新运行我的代码时,训练现在被跳过了,因为它已经完成了,预测现在可以正常工作了。在 google 中,我发现此错误仅与模型训练有关,所以我猜这些解决方案对我不起作用。我认为我出错的原因是,我的视频内存在模型训练后没有完全释放。这就是为什么我尝试了以下但没有成功的原因。

tf.keras.backend.clear_session()
tf.compat.v1.reset_default_graph()
K.clear_session()

错误代码:

prediction = model.predict(x)[:, 0]#.flatten()  # flatten was needed now
  File "/home/max/PycharmProjects/Masterthesis/venv3-8-12/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/max/PycharmProjects/Masterthesis/venv3-8-12/lib/python3.8/site-packages/tensorflow/python/framework/constant_op.py", line 106, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
tensorflow.python.framework.errors_impl.InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run _EagerConst: Dst tensor is not initialized.

你对如何解决这个问题有什么想法吗?

我的设置:

我的代码:

from tensorflow.keras.models import Model, load_model
from tensorflow.keras.layers import Input, LSTM, Dense, Dropout
import tensorflow as tf
import h5py
import keras.backend as K


def loss_function(y_true, y_pred):
    alpha = K.std(y_pred) / K.std(y_true)
    beta = K.sum(y_pred) / K.sum(y_true)
    error = K.sqrt( + K.square(1 - alpha) + K.square(1 - beta))

    return error


i = Input(shape=(171, 11))
x = LSTM(100, return_sequences=True)(i)
x = LSTM(50)(x)
x = Dropout(0.1)(x)
out = Dense(1)(x)

model = Model(i, out)
model.compile(
    loss=loss_function,
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.001))

with h5py.File("db.hdf5", 'r') as db_:
    r = model.fit(
        db_["X_train"][...],
        db_["Y_train"][...],
        epochs=1,
        batch_size=64,
        verbose=1,
        shuffle=True)
model.save("model.h5")

model = load_model("model.h5", compile=False)

with h5py.File("db.hdf5", 'r') as db:
    x = db["X_val"][...]
    y = db["Y_val"][...].flatten()
    prediction = model.predict(x)[:, 0].flatten()

我找到了解决问题的方法。由于我使用的是自定义损失函数,因此我需要在再次加载模型时以某种方式指定自定义损失函数。 我通过修改此行

来完成此操作

model = load_model("model.h5", compile=False)

到这个

model = load_model("model.h5", custom_objects={"loss_function": loss_function})