Beispiel #1
0
def train_autopytorch(X_train,X_test,y_train,y_test,mtype,common_name_model,problemtype,classes,default_featurenames,transform_model,settings,model_session):

	# name model
	model_name=common_name_model+'.pickle'
	files=list()

	if mtype=='c': 
		from autoPyTorch import AutoNetClassification
		autonet = AutoNetClassification(log_level='debug', max_runtime=900, min_budget=50, max_budget=150)
		autonet.fit(X_train, y_train, validation_split=0.30)
		print(autonet.predict(X_test).flatten())

	if mtype=='r': 
		from autoPyTorch import AutoNetRegression
		autonet = AutoNetRegression(log_level='debug', max_runtime=900, min_budget=50, max_budget=150)
		autonet.fit(X_train, y_train)
		print(autonet.predict(X_test).flatten())

	print('saving model -->')
	torch.save(autonet, model_name)

	# get model directory
	files.append(model_name)
	files.append('configs.json')
	files.append('results.json')
	model_dir=os.getcwd()

	return model_name, model_dir, files
Beispiel #2
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)
autonet = AutoNetClassification(config_preset="tiny_cs",
                                result_logger_dir="logs/")
# Fit (note that the settings are for demonstration, you might need larger budgets)
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
}
import openml
from pprint import pprint
from autoPyTorch import AutoNetClassification
from sklearn.metrics import accuracy_score

# get OpenML task by its ID
task = openml.tasks.get_task(task_id=32)
X, y = task.get_X_and_y()
ind_train, ind_test = task.get_train_test_split_indices()

# run Auto-PyTorch
autoPyTorch = AutoNetClassification(
    "tiny_cs",  # config preset
    log_level='info',
    max_runtime=300,
    min_budget=30,
    max_budget=90)

autoPyTorch.fit(X[ind_train], y[ind_train], validation_split=0.3)

# predict
y_pred = autoPyTorch.predict(X[ind_test])

print("Accuracy score", accuracy_score(y[ind_test], y_pred))

# print network configuration
pprint(autoPyTorch.fit_result["optimized_hyperparameter_config"])