我的模型預測有問題。訓練效果很好,但之后我的程式在預測訓練模型時失敗了。當我重新運行我的代碼時,現在跳過了訓練,因為它已經完成了,預測現在可以正常作業了。在谷歌中,我發現這個錯誤僅與模型訓練有關,所以我猜這些解決方案對我不起作用。我認為我的錯誤的原因是,我的視頻記憶體在模型訓練后沒有完全釋放。這就是為什么我嘗試了以下但沒有成功的原因。
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.
您對如何解決問題有任何想法嗎?
我的設定:
- 蟒蛇:3.8.12
- 張量流GPU:2.7.0
- 系統:Manjaro Linux
- 庫達:11.5
- 顯卡:NVIDIA GeForce GTX 980 Ti
我的代碼:
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()
uj5u.com熱心網友回復:
我找到了解決我的問題的方法。由于我使用的是自定義損失函式,因此在再次加載模型時不知何故需要指定自定義損失函式。我通過修改這一行來實作這一點
model = load_model("model.h5", compile=False)
對這個
model = load_model("model.h5", custom_objects={"loss_function": loss_function})
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/412643.html
標籤:
上一篇:產品頁面專案驗證PHP
