神经网络:TypeError:fitness()得到了意外的关键字参数“嵌入”

时间:2019-07-08 20:32:33

标签: pandas optimization keras scikit-learn neural-network

我正在尝试使用skopt优化NN: https://scikit-optimize.github.io/notebooks/sklearn-gridsearchcv-replacement.html

print('...load ranges for each dimension...')
dim_num_embedding_nodes = Integer(low=32, high=512, name='embedding')
dim_hidden_layer = Integer(low=32, high=512, name='hidden_layer_size')
dim_batch_size = Integer(low=32, high=128, name='batch_size')
dim_alpha = Real(low=0,high=2,name="alpha")
dim_dropout = Real(low=0,high=1,name="dropout")
dim_recurrent_dropout = Real(low=0,high=1,name="recurrent_dropout")


dimensions = [dim_num_embedding_nodes,
              dim_hidden_layer,
              dim_batch_size,
              dim_alpha,
              dim_dropout,
              dim_recurrent_dropout
             ]
default_parameters = [512, 64, 64, 0.01, 0.3, 0.3]


def create_model(embedding_dim, hidden_layer_size,
                 alpha, dropout, recurrent_dropout):
    model = Sequential()
    model.add(Embedding(input_dim=3000, output_dim=embedding_dim, input_length=110))
    model.add(GRU(units=hidden_layer_size, recurrent_dropout=recurrent_dropout, return_sequences=False))
    model.add(ELU(alpha=alpha))
    model.add(Dropout(rate=dropout))
    model.add(Dense(2, activation='softmax'))
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=["binary_accuracy"])
    return model

print('...lets do a bunch of trials...')
@use_named_args(dimensions=dimensions)
def fitness(embedding_dim, hidden_layer_size, batch_size, 
            alpha, dropout, recurrent_dropout):
    model = create_model(embedding_dim=embedding_dim, 
                         hidden_layer_size=hidden_layer_size,
                         alpha=alpha,
                         dropout=dropout,
                         recurrent_dropout=recurrent_dropout)

    # named blackbox becuase it represents the structure
    blackbox = model.fit(x_train_sequence, y_train, batch_size=batch_size, 
                         validation_data=(x_test_sequence, y_test),
                         verbose=1, shuffle=True, epochs=3)
    # return the validation accuracy for the last epoch.
    accuracy = blackbox.history['val_binary_accuracy'][-1]

    # Print the classification accuracy.
    print()
    print("Accuracy: {0:.2%}".format(accuracy))
    print()


    return -accuracy



gp_result = gp_minimize(func=fitness,
                            dimensions=dimensions,
                            n_calls=12,
                            noise= 0.01,
                            n_jobs=-1,
                            kappa = 5,
                            x0=default_parameters)

print("best accuracy was " + str(round(gp_result.fun *-100,2))+"%.")

但是,我不断收到此错误:

  

回溯(最近通话最近一次):

     

文件“”,第1行,在       runfile('/ Users / mattiavicari / Desktop / Optimize_NLP / actual_skopt.py',   wdir ='/ Users / mattiavicari / Desktop / Optimize_NLP')

     

文件   “ /Users/mattiavicari/anaconda3/envs/Optimize_DL/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”,   运行文件中的第827行       execfile(文件名,命名空间)

     

文件   “ /Users/mattiavicari/anaconda3/envs/Optimize_DL/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py”,   第110行,在execfile中       exec(compile(f.read(),文件名,'exec'),命名空间)

     

文件“ /Users/mattiavicari/Desktop/Optimize_NLP/actual_skopt.py”,   298行,在       x0 = default_parameters)

     

文件   “ /Users/mattiavicari/anaconda3/envs/Optimize_DL/lib/python3.7/site-packages/skopt/optimizer/gp.py”,   gp_minimize中的第228行       callback = callback,n_jobs = n_jobs)

     

文件   “ /Users/mattiavicari/anaconda3/envs/Optimize_DL/lib/python3.7/site-packages/skopt/optimizer/base.py”,   第231行,在base_minimize中       y0 = list(map(func,x0))

     

文件   “ /Users/mattiavicari/anaconda3/envs/Optimize_DL/lib/python3.7/site-packages/skopt/utils.py”,   包装中的第636行       objective_value = func(** arg_dict)

     

TypeError:Fitness()收到了意外的关键字参数“嵌入”

谁能解释我在做什么错?

非常感谢!

0 个答案:

没有答案