Esempio n. 1
0
 #                             value_range=[32],
 #                             log=False)
 #Init autonet
 # autoPyTorch = AutoNetClassification(hyperparameter_search_space_updates=search_space_updates,  # config
 autoPyTorch = AutoNetClassification(
     # "full_cs",  # config
     networks=["resnet"],
     # torch_num_threads=2,
     log_level='info',
     budget_type='epochs',
     min_budget=5,
     max_budget=20,
     num_iterations=100,
     cuda=True,
     use_pynisher=False)
 #fit
 autoPyTorch.fit(X_train=X_train,
                 Y_train=y_train,
                 X_valid=X_test,
                 Y_valid=y_test,
                 optimize_metric="auc_metric",
                 loss_modules=["cross_entropy", "cross_entropy_weighted"])
 #predict
 y_pred = autoPyTorch.predict(X_test)
 #check
 print("Accuracy score", sklearn.metrics.accuracy_score(y_test, y_pred))
 print("Confusion matrix",
       sklearn.metrics.confusion_matrix(y_test, y_pred, labels=le.classes_))
 print(autoPyTorch)
 pytorch_model = autoPyTorch.get_pytorch_model()
 print(pytorch_model)
results_fit = autonet.fit(X_train=X_train,
                          Y_train=Y_train,
                          validation_split=0.3,
                          max_runtime=300,
                          min_budget=60,
                          max_budget=100,
                          refit=True)
# Save fit results as json
with open("logs/results_fit.json", "w") as file:
    json.dump(results_fit, file)
# See how the random configuration performs (often it just predicts 0)
score = autonet.score(X_test=X_test, Y_test=Y_test)
pred = autonet.predict(X=X_test)
print("Model prediction:", pred[0:10])
print("Accuracy score", score)
pytorch_model = autonet.get_pytorch_model()
print(pytorch_model)
# Load fit results as json
with open("logs/results_fit.json") as file:
    results_fit = json.load(file)
# Create an autonet
autonet_config = {
    "result_logger_dir": "logs/",
    "budget_type": "epochs",
    "log_level": "info",
    "use_tensorboard_logger": True,
    "validation_split": 0.0
}
autonet = AutoNetClassification(**autonet_config)
# Sample a random hyperparameter configuration as an example
hyperparameter_config = autonet.get_hyperparameter_search_space(