0

我在使用 hyperas 时收到以下错误:
AttributeError: 'numpy.random.mtrand.RandomState' object has no attribute 'integers'。

错误发生在我在 optim_minimize 中定义 notebook_name 的行中。

我的完整代码:

def data():
    (xtr, ytr), (xte, yte)= mnist.load_data()
    xtr= xtr.reshape(60000, 784); xtr= xtr.astype('float32')
    xte= xte.reshape(10000, 784); xte= xte.astype('float32')
    xtr/= 255; xte/= 255
    nb_classes= 10
    ytr= np_utils.to_categorical(ytr, nb_classes)
    yte= np_utils.to_categorical(yte, nb_classes)
    return xtr, ytr, xte, yte

def create_model(xtr, ytr, xte, yte):
    # returns a dictionary of loss, status and model
    model= Sequential()
    # 1st layer:
    model.add(Dense(512, input_shape= (784,), activation= 'relu'))  # equivalently input_dim= 784    
    model.add(Dropout({{uniform(0,1)}}))
    
    # 2nd layer:
    # hyperparameter = {{choice([...])}}
    model.add(Dense(units= {{choice([256,5125,1024])}}, 
                    activation= {{choice(['relu', 'sigmoid'])}}))
    model.add(Dropout({{uniform(0,1)}}))
    
    # 3rd layer:
    if {{choice(['three','four'])}} == 'four':
        model.add(Dense(100))
        # choice between 2 different types of Dense(100) layers:
        model.add({{choice([Dropout(0.5), Activation('linear')])}})
        model.add(Activation('relu'))
    
    # 4th layer:
    model.add(Dense(10, activation= 'softmax'))
    
    model.compile(loss='categorical_crossentropy', 
                  optimizer= {{choice(['rmsprop', 'adam', 'SGD'])}},
                 metrics=['accuracy'])
    
    # Model fit:
    result= model.fit(xtr, ytr, batch_size= {{choice([64, 128])}},
                     epochs= 2, verbose= 2, validation_split= 0.1)
    validation_acc= np.amax(result.history['val_acc'])
    print('Best validation accuracy of epoch:', validation_acc)
    return {'Loss:', -validation_acc, 'status:', STATUS_OK, 'model:',model}

if __name__ == '__main__':
    best_run, best_model= optim.minimize(model= create_model, data= data,
                                        algo= tpe.suggest, max_evals= 5,
                                        trials= Trials(),
                                        notebook_name='Deep learning GridSearch')
    xtr, ytr, xte, yte= data()
    
    print('Evaluation of best performing model:')
    print(best_model.evaluate(xte, yte))
    
    print('Optimal hyperparameter choice:')
    print(best_run)
    

4

0 回答 0