我尝试使用 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)
~\AppData\Local\Temp/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
~\anaconda3\envs\tf-gpu-cuda8\lib\site-packages\keras\utils\traceback_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
~\anaconda3\envs\tf-gpu-cuda8\lib\site-packages\keras\engine\training.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`.
有什么建议吗?我采用了相同的方法,但使用的是每小时数据,而且效果很好,没有任何错误。小时码和日码(这个)一样,唯一不同的是把小时码中的数据在一天内相加,得到日码数据。
您的代码有一些问题。
您正在训练循环中创建模型。所以这是错误的。除非你想在每个 epoch 训练一个新版本。您的模型应该在开始训练之前定义和编译。
根据您使用的 tensorflow 版本,您应该在声明模型之前启用即时执行。这使您可以在不创建会话和范围的情况下训练模型。
希望这可以帮助!