A full tutorial and additional databases are provided (tutorial, databases) to get you started. The following documentation is intended for users seeking additional information about ECNet’s functionality.
ECNet operates using a Server object that interfaces with data utility classes, error calculation functions, and neural network creation classes. The Server object handles importing data and model creation for your project, and serves the data to models. Configurable variables for neural networks, such as learning rate, number of neurons per hidden layer, activation functions for hidden/input/output layers, and number of training epochs are found in a .yml configuration file.
Model configuration file¶
For training, we apply the Adam optimization algorithm to feed-forward neural networks. Here is the default model configuration, outlining default values we pass to model creation and training functions:
--- beta_1: 0.9 beta_2: 0.999 decay: 0.0 epochs: 2000 epsilon: 0.0000001 hidden_layers: - - 32 - relu - - 32 - relu learning_rate: 0.001 output_activation: linear
Using the Server object¶
First, create a Python script to handle your task and copy an ECNet-formatted CSV database file to your working directory. The Server object will create a default configuration file if an existing one is not specified or found.
Your first steps are importing the Server object, initializing the Server and importing some data:
from ecnet import Server # Initialize a Server sv = Server() # If `config.yml` does not already exist in your working directory, it will be created with # default values; to specify another configuration file, use the model_config argument sv = Server(model_config='my_model_configuration.yml') # You can utilize parallel processing (multiprocessing) for model training and hyperparameter # tuning: sv = Server(num_processes=4) sv.num_processes = 8 # Import an ECNet-formatted CSV database sv.load_data('my_data.csv') # Learning, validation and test sets are defined in the database's ASSIGNMENT column; to use # random set assignments, supply the `random` and `split` arguments: sv.load_data( 'my_data.csv', random=True, split=[0.7, 0.2, 0.1] ) # 70% of data is in the learning set, 20% in the validation set and 10% in the test set
ECNet utilizes console and file logging - by default, it will not log anything to either the console or a file. To enable logging functionality, we can use ECNet’s logger:
from ecnet.utils.logging import logger # Available levels are `debug`, `info`, `warn`, `error`, `crit`, `disable` # Set the console log level to `info`: logger.stream_level = 'info' # Set the file log level to `info`: logger.file_level = 'info' # Specify a directory to save log files: logger.log_dir = 'path\to\my\log\directory'
You can change all the model configuration variables from your Python script without having to edit and re-open your model configuration file:
# Configuration variables are found in the Server's '._vars' dictionary sv._vars['learning_rate'] = 0.05 sv._vars['beta_2'] = 0.75 sv._vars['hidden_layers'] = [[32, 'relu'], [32, 'relu']] sv._vars['epochs'] = 10000
Optimal input dimensionality, i.e. finding a balance between runtime and precision/accuracy, is often beneficial. To limit input dimensionality to a specified number of influential input parameters, ECNet utilizes random forest regression:
# Find the 15 most influential input parameters sv.limit_inputs(15) # Find the 15 most influential input parameters, and save them to an ECNet-formatted database: sv.limit_inputs(15, output_filename='my_limited_data.csv')
Optimal hyperparameters are essential for mapping inputs to outputs during neural network training. ECNet utilizes an artificial bee colony, ECabc, to optimize hyperparameters such as learning rate, beta, decay and epsilon values, and number of neurons per hidden layer:
# Tune hyperparameters for 50 iterations (search cycles) with 50 employer bees: sv.tune_hyperparameters(50, 50) # By default, all bees will use the same set assignments; to shuffle them: sv.tune_hyperparameters(50, 50, shuffle='all', split=[0.7, 0.2, 0.1]) # To shuffle only the training sets (learning and validation), supply `train` instead # By default, bees are evaluated on their performance across all sets; to specify a set to # perform the evaluation: sv.tune_hyperparameters(50, 50, eval_set='test') # Available sets are `learn`, `valid`, `train`, `test`, None (all sets) # The ABC will measure error using RMSE; to change the error function used: sv.tune_hyperparameters(50, 50, eval_fn='mean_abs_error') # Available functions are `rmse`, `mean_abs_error`, `med_abs_error`
ECNet is able to create an ensemble of neural networks (candidates chosen from pools) to predict a final value for the project. Projects can be saved and used at a later time.
# Create a project 'my_project' with 5 pools, 75 candidates per pool: sv.create_project( 'my_project', num_pools=5, num_candidates=75, ) # Train neural networks using the number of epochs in your configuration file: sv.train() # To use periodic validation (training halts when validation set performance stops improving), # supply the validate argument: sv.train(validate=True) # We can shuffle either 'train' (learning and validation) or 'all' sets with the shuffle # argument and a split: sv.train( shuffle='train', split=[0.7, 0.2, 0.1] ) # We can retrain pre-existing candidates: sv.train(retrain=True) # By default, best neural networks are selected from pools based on their performance on all # sets; to specify a set used for evaluation: sv.train(selection_set='test') # Available sets are `learn`, `valid`, `train`, `test`, None (all sets) # By default, candidates are evaluated by measuring their RMSE on the supplied set; to specify # another error function: sv.train(selection_fn='mean_abs_error') # Available functions are `rmse`, `mean_abs_error`, `med_abs_error` # Save your project sv.save_project() # You can save it with a name other than the one assigned sv.save_project(filename='path/to/my/save.prj') # When a project is saved, it will remove the folder structure it originated from; if this is # unwanted: sv.save_project(clean_up=False) # A saved project contains all candidate neural networks, even if they have not been selected; # to remove all non-chosen candidate neural networks: sv.save_project(del_candidates=True) # Predict values for the test set: test_results = sv.use(dset='test') # You can predict for 'learn', 'valid', 'train', 'test', or None (all) sets # If you want to save these results to a CSV file, supply the output_filename argument sv.use(dset='test', output_filename='my/test/results.csv') # Calculates errors for the test set (any combination of these error functions can be supplied as # arguments, and any dset listed above) test_errors = sv.errors('rmse','r2','mean_abs_error','med_abs_error', dset='test')
Once you save a project, the .prj file can be used at a later time:
from ecnet.server import Server # Specify a 'prj_file' argument to open a pre-existing project sv = Server(prj_file='my_project.prj') # Open an ECNet-formatted database with new data sv.load_data('new_data.csv') # Save results to output file # - NOTE: no 'dset' argument for 'use_model' defaults to using all currently loaded data sv.use(output_filename='my/new/test/results.csv')