我正在训练一种机器学习模型,其中包含使用Keras和Tensorflow的两个完全连接的层。在执行详尽搜索以调整某些超参数时,我为每组超参数创建了一个新模型。尽管如此,模型的训练时间在每次迭代时都会增加。为了解决此问题,我每次使用 K.clear_session()清除Keras会话。
如果我使用CPU运行代码,则代码运行正常。但是,当尝试在Tensorflow后端使用GPU时,应用程序突然崩溃,没有任何警告或错误。
以下是我用来创建新模型的功能。
def get_compiled_model(model_def, shape, model_type='ann'):
K.clear_session() #Clear the previous tensorflow graph
#tf.reset_default_graph()
#Shared parameters for the models
optimizer = Adam(lr=0, beta_1=0.5)
lossFunction = "mean_squared_error"
metrics = ["mse"]
model = None
#Create and compile the models
if model_type=='ann':
model = model_def(shape)
model.compile(optimizer = optimizer, loss = lossFunction, metrics = metrics)
else:
pass
return model
models = {'shallow-20':RULmodel_SN_5}
这是调用上面函数
的代码部分for dataset_number in max_window_size:
tunable_model.data_handler.change_dataset(dataset_number)
verbose = 1
for r in range(90, 141): #Load max_rul first as it forces reloading the dataset from file
verbose = 2
tunable_model.data_handler.max_rul = r
for w in range(15, max_window_size[dataset_number]+1):
for s in range(1,11):
print("Testing for w:{}, s:{}, r:{}".format(w, s, r))
#Set data parameters
tunable_model.data_handler.sequence_length = w
tunable_model.data_handler.sequence_stride = s
#Create and compile the models
shape = num_features*w
**model = get_compiled_model(models['shallow-20'], shape, model_type='ann')**
#Add model to tunable model
tunable_model.change_model('ModelRUL_SN', model, 'keras')
#Load the data
tunable_model.load_data(unroll=True, verbose=verbose, cross_validation_ratio=0)