ValueError: ' predict_function '的意外结果(空batch_outputs).请使用' M



我尝试使用GridSearchCV(),但我也得到了错误,所以我写了一个非常耗时的代码,返回我想评估我的模型的指标。

def df_to_new(df,window_size): 
df_as_np = df.to_numpy()
X = []
y = []
for i in range(len(df_as_np)-window_size):
row = [[a] for a in df_as_np[i:i+window_size]]
X.append(row)
label = df_as_np[i+window_size]
y.append(label)

return np.array(X),np.array(y)  
from tensorflow.keras.models import load_model
import os
checkpointpath = 'C:\Users\USER\trainingFORLOOP_daily/cp.ckt'
cp = ModelCheckpoint(checkpointpath, save_best_only=True,verbose=1)     
EPOCH = [30,150,300]
learningRates = [0.0001,0.00001]
batchSize = [15,20,40]
win_size = [5,15,25]
dropout_rate = 0.2
num_features = 1
for i in learningRates:
for j in EPOCH:
for k in batchSize:
for l in win_size:
X,y = df_to_new(Ac13,l)
#Split the data
perc_train = 0.8
limit_train = int(np.floor(len(Ac13)*perc_train))
xtrain,ytrain = X[:limit_train],y[:limit_train]
xval,yval = X[limit_train:],y[limit_train:]

#create the model
model1 = Sequential() 
model1.add(InputLayer((l,1))) 
model1.add(LSTM(128))
model1.add(Dropout(dropout_rate))
model1.add(Dense(86,'relu'))
model1.add(Dropout(dropout_rate))
model1.add(Dense(1,'linear'))
model1.summary()
model1.compile(loss=MeanSquaredError(),optimizer = 
Adam(learning_rate=i),
metrics=[RootMeanSquaredError()],run_eagerly=True)
model1.fit(xtrain,ytrain,validation_data=(xval,yval),batch_size=k,epochs=j,callbacks=[cp],shuffle=False)
model1.save("my_model")
model1 = load_model("my_model")

train_predictions = model1.predict(xtrain).flatten() 
train_results = pd.DataFrame(data={'TrainPredictions':train_predictions,'Actual values':ytrain})
train_results
scale = len(train_predictions)
val_predictions = model1.predict(xval).flatten()
val_results = pd.DataFrame(data='ValidatePredictions':val_predictions,'Validation values':yval}) 

我得到以下错误(完整的回溯):

ValueError                                Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_19052/292377201.py in <module>
51                 plt.legend(bbox_to_anchor =(0.75, 1.15), ncol = 2)
52                 plt.show()
---> 53                 val_predictions = model1.predict(xval).flatten() # flatten() removes the brackets inside the data
54                 val_results = pd.DataFrame(data={'Validate Predictions':val_predictions,'Validation values':yval}) #yval are the actual values
55                 val_results
~anaconda3envstf-gpu-cuda8libsite-packageskerasutilstraceback_utils.py in error_handler(*args, **kwargs)
65     except Exception as e:  # pylint: disable=broad-except
66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
68     finally:
69       del filtered_tb
~anaconda3envstf-gpu-cuda8libsite-packageskerasenginetraining.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
1995             callbacks.on_predict_batch_end(end_step, {'outputs': batch_outputs})
1996       if batch_outputs is None:
-> 1997         raise ValueError('Unexpected result of `predict_function` '
1998                          '(Empty batch_outputs). Please use '
1999                          '`Model.compile(..., run_eagerly=True)`, or '
ValueError: Unexpected result of `predict_function` (Empty batch_outputs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.

有什么建议吗?我用了同样的方法,但每小时的数据,它工作得很好,没有任何错误。小时代码与日代码(这个)相同,唯一改变的是小时代码中的数据以一天为单位求和,得到日代码数据。

你的代码有一些问题。

  1. 你正在训练循环中创建你的模型。所以这是错误的。除非你想在每个阶段训练一个新版本。你的模型应该在你开始训练之前被定义和编译。

  2. 根据你使用的tensorflow版本,你应该在声明模型之前启用急切执行。这样就可以在不创建会话和作用域的情况下训练模型。

希望这对你有帮助!

相关内容

  • 没有找到相关文章

最新更新