使用skopt

时间:2018-08-13 17:29:22

标签: python tensorflow keras

我正在遵循this教程来优化模型的超参数。发布question并获得立即答复后,我成功创建了一个带有for循环的多层LSTM模型。下面是我的最终模型:

from keras.layers.core import Dense
from keras.layers import LSTM, Input
from keras.models import Model

from keras.optimizers import RMSprop
from keras.initializers import glorot_uniform, glorot_normal, RandomUniform



def create_model(learning_rate, num_lstm_layers, num_lstm_units, activation):
    init = glorot_normal(seed=None)
    init1 = RandomUniform(minval=-0.05, maxval=0.05)
    input_tensor_mine = x = Input(shape=(x_train.shape[1], x_train.shape[2]), name='input_layerrrr')

    for i in range(num_lstm_layers):

        name = 'layer_lstm_{0}'.format(i+1)

        if( (i==0) and (num_lstm_layers==1) ):
            x = LSTM(units=num_lstm_units, dropout=0.2, recurrent_dropout=0.2, 
                    return_sequences=False, kernel_initializer=init, 
                    activation=activation, name=name)(input_tensor_mine)
        elif(i != (num_lstm_layers-1) ):
            x = LSTM(units=num_lstm_units, dropout=0.2, recurrent_dropout=0.2,  
                    return_sequences=True, kernel_initializer=init, 
                    activation=activation, name=name)(x)
        else:
            x = LSTM(units=num_lstm_units, dropout=0.2, recurrent_dropout=0.2,  
                    return_sequences=False, kernel_initializer=init, 
                    activation=activation, name=name)(x)
    x = Dense(1, activation='linear', kernel_initializer= init1)(x)

    model = Model(input_tensor_mine, x)

    optimizer = RMSprop(lr=learning_rate, rho=0.9, epsilon=None, decay=0.0)
    model.compile(loss='mean_squared_error', optimizer=optimizer, metrics=['mse'] )
    print(model.summary())
    return model      

然后,我继续为上述教程中描述的fitness函数编写自己的代码:

path_best_model = 'best_model.keras'
best_mse=10

@use_named_args(dimensions=dimensions)
def fitness(learning_rate, num_lstm_layers, num_lstm_units, activation):

    model = create_model(learning_rate=learning_rate,
                         num_lstm_layers=num_lstm_layers,
                         num_lstm_units=num_lstm_units,
                         activation=activation)
    history = model.fit(x_train, y_train, epochs=3, 
                        batch_size=64, 
                        validation_data=(x_test, y_test),
                        shuffle=False)
    mse = history.history['val_loss'][-1]
    global best_mse

    if mse<best_mse:
        model.save(path_best_model)
        best_mse = mse

    del model
    K.clear_session()

    return mse

我的问题是,如果model.save(path_best_model)没有被注释掉,并且我运行了 fitness 功能,则在gp_minimize创建了3个自动模型后,我收到以下错误:

#running the automatic creation of LSTM models using gp_minimize and fitness function

from skopt import gp_minimize
search_result = gp_minimize(func=fitness,
                            dimensions=dimensions,
                            acq_func='EI',
                            n_calls=40,
                            x0=default_parameters)
  

错误:

     

ValueError:提取参数不能为   解释为张量。 (张量Tensor(“ layer_lstm_1 / kernel:0”,   shape =(331,188),dtype = float32_ref)不是此图的元素。)

如果有人想了解有关3个创建的模型的更多详细信息,我将其中的model.summary()添加到问题中。

0 个答案:

没有答案