gnnwr.models module
- class gnnwr.models.GNNWR(train_dataset, valid_dataset, test_dataset, dense_layers=None, start_lr: float = 0.1, optimizer='Adagrad', drop_out=0.2, batch_norm=True, activate_func=PReLU(num_parameters=1), model_name='GNNWR_20231128-111456', model_save_path='../gnnwr_models', write_path='../gnnwr_runs/20231128-111456', use_gpu: bool = True, use_ols: bool = True, log_path='../gnnwr_logs/', log_file_name='gnnwr20231128-111456.log', log_level=20, optimizer_params=None)[source]
Bases:
object
GNNWR(Geographically neural network weighted regression) is a model to address spatial non-stationarity in various domains with complex geographical processes, which comes from the paper Geographically neural network weighted regression for the accurate estimation of spatial non-stationarity.
- Parameters:
train_dataset (baseDataset) – the dataset of training
valid_dataset (baseDataset) – the dataset of validation
test_dataset (baseDataset) – the dataset of testing
dense_layers (list) –
the dense layers of the model (default:
None
)Default structure is a geometric sequence of power of 2, the minimum is 2, and the maximum is the power of 2 closest to the number of neurons in the input layer.
i.e.
[2,4,8,16,32,64,128,256]
start_lr (float) – the start learning rate of the model (default:
0.1
)optimizer (str, optional) – the optimizer of the model (default:
"Adagrad"
) choose from “SGD”,”Adam”,”RMSprop”,”Adagrad”,”Adadelta”drop_out (float) – the drop out rate of the model (default:
0.2
)batch_norm (bool, optional) – whether use batch normalization (default:
True
)activate_func (torch.nn) – the activate function of the model (default:
nn.PReLU(init=0.4)
)model_name (str) – the name of the model (default:
"GNNWR_" + datetime.datetime.today().strftime("%Y%m%d-%H%M%S")
)model_save_path (str) – the path of the model (default:
"../gnnwr_models"
)write_path (str) – the path of the log (default:
"../gnnwr_runs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
)use_gpu (bool) – whether use gpu or not (default:
True
)use_ols (bool) – whether use ols or not (default:
True
)log_path (str) – the path of the log (default:
"../gnnwr_logs/"
)log_file_name (str) – the name of the log (default:
"gnnwr" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + ".log"
)log_level (int) – the level of the log (default:
logging.INFO
)optimizer_params (dict, optional) –
the params of the optimizer and the scheduler (default:
None
)if optimizer is SGD, the params are:
maxlr: float, the max learning rate (default:0.1
)minlr: float, the min learning rate (default:0.01
)upepoch: int, the epoch of learning rate up (default:10000
)decayepoch: int, the epoch of learning rate decay (default:20000
)decayrate: float, the rate of learning rate decay (default:0.1
)stop_change_epoch: int, the epoch of learning rate stop change (default:30000
)stop_lr: float, the learning rate when stop change (default:0.001
)if optimizer is Other, the params are:
scheduler: str, the name of the scheduler (default:"CosineAnnealingWarmRestarts"
) in {"MultiStepLR","CosineAnnealingLR","CosineAnnealingWarmRestarts"
}scheduler_milestones: list, the milestones of the scheduler MultiStepLR (default:[500,1000,2000,4000]
)scheduler_gamma: float, the gamma of the scheduler MultiStepLR (default:0.5
)scheduler_T_max: int, the T_max of the scheduler CosineAnnealingLR (default:1000
)scheduler_eta_min: float, the eta_min of the scheduler CosineAnnealingLR and CosineAnnealingWarmRestarts (default:0.01
)scheduler_T_0: int, the T_0 of the scheduler CosineAnnealingWarmRestarts (default:100
)scheduler_T_mult: int, the T_mult of the scheduler CosineAnnealingWarmRestarts (default:3
)
- getLoss()[source]
get network’s loss
- Returns:
the list of the loss in training process and validation process
- Return type:
list
- getWeights()[source]
get weight of each argument
- Returns:
the Pandas dataframe of the weight of each argument in train_dataset
- Return type:
dataframe
- gpumodel_to_cpu(path, save_path, use_model=True)[source]
convert gpu model to cpu model
- Parameters:
path (str) – the path of the model
save_path (str) – the path of the new model
use_model (bool) – whether use dict to load the model (default:
True
)
- init_optimizer(optimizer, optimizer_params=None)[source]
initialize the optimizer
- Parameters:
optimizer (str) – the optimizer of the model (default:
"Adagrad"
) choose from “SGD”,”Adam”,”RMSprop”,”Adagrad”,”Adadelta”optimizer_params (dict, optional) –
the params of the optimizer and the scheduler (default:
None
)if optimizer is SGD, the params are:
maxlr: float, the max learning rate (default:0.1
)minlr: float, the min learning rate (default:0.01
)upepoch: int, the epoch of learning rate up (default:10000
)decayepoch: int, the epoch of learning rate decay (default:20000
)decayrate: float, the rate of learning rate decay (default:0.1
)stop_change_epoch: int, the epoch of learning rate stop change (default:30000
)stop_lr: float, the learning rate when stop change (default:0.001
)if optimizer is Other, the params are:
scheduler: str, the name of the scheduler (default:"CosineAnnealingWarmRestarts"
) in {"MultiStepLR","CosineAnnealingLR","CosineAnnealingWarmRestarts"
}scheduler_milestones: list, the milestones of the scheduler MultiStepLR (default:[500,1000,2000,4000]
)scheduler_gamma: float, the gamma of the scheduler MultiStepLR (default:0.5
)scheduler_T_max: int, the T_max of the scheduler CosineAnnealingLR (default:1000
)scheduler_eta_min: float, the eta_min of the scheduler CosineAnnealingLR and CosineAnnealingWarmRestarts (default:0.01
)scheduler_T_0: int, the T_0 of the scheduler CosineAnnealingWarmRestarts (default:100
)scheduler_T_mult: int, the T_mult of the scheduler CosineAnnealingWarmRestarts (default:3
)
- load_model(path, use_dict=False, map_location=None)[source]
To load a model that was automatically saved during the training process. Given that the saved file only contains the neural network components, the suggested procedure is as follows: first, instantiate an object corresponding to the model’s architectural design, and then subsequently invoke the load_model method to import the parameters and weights.
- Parameters:
path (str) – the path of the model
use_dict (bool) – whether use dict to load the model (default:
False
)map_location (str) – the location of the model (default:
None
) the location can be"cpu"
or"cuda"
- predict(dataset)[source]
predict the result of the dataset
- Parameters:
dataset (baseDataset,predictDataset) – the dataset to be predicted
- Returns:
the Pandas dataframe of the dataset with the predicted result
- Return type:
dataframe
- predict_weight(dataset)[source]
predict the spatial weight of the dataset
- Parameters:
dataset (baseDataset,predictDataset) – the dataset to be predicted
- Returns:
the Pandas dataframe of the dataset with the predicted spatial weight
- Return type:
dataframe
- reg_result(filename=None, model_path=None, use_dict=False, only_return=False, map_location=None)[source]
save the regression result of the model, including the weight of each argument, the bias, the predicted result
- Parameters:
filename (str) – the path of the result file (default:
None
) | iffilename
isNone
, the result will not be saved as filemodel_path (str) – the path of the model (default:
None
) | ifmodel_path
isNone
, the model will be loaded fromself._modelSavePath + "/" + self._modelName + ".pkl"
use_dict (bool) – whether use dict to load the model (default:
False
) | ifuse_dict
isTrue
, the model will be loaded frommodel_path
as dictonly_return (bool) – whether only return the result (default:
False
) | ifonly_return
isTrue
, the result will not be saved as filemap_location (str) – the location of the model (default:
None
) the location can be"cpu"
or"cuda"
- Returns:
the Pandas dataframe of the result
- Return type:
dataframe
- result(path=None, use_dict=False, map_location=None)[source]
print the result of the model, including the model structure, optimizer,the result of test dataset
- Parameters:
path (str) – the path of the model(default:
None
) | ifpath
isNone
, the model will be loaded fromself._modelSavePath + "/" + self._modelName + ".pkl"
use_dict (bool) – whether use dict to load the model (default:
False
) | ifuse_dict
isTrue
, the model will be loaded frompath
as dictmap_location (str) – the location of the model (default:
None
) the location can be"cpu"
or"cuda"
- run(max_epoch=1, early_stop=-1, print_frequency=50, show_detailed_info=True)[source]
train the model and validate the model
- Parameters:
max_epoch (int) – the max epoch of the training (default:
1
)early_stop (int) –
if the model has not been updated for
early_stop
epochs, the training will stop (default:-1
)if
early_stop
is-1
, the training will not stop until the max epochprint_frequency (int) – the frequency of printing the information (default:
50
)
- class gnnwr.models.GTNNWR(train_dataset, valid_dataset, test_dataset, dense_layers=None, start_lr: float = 0.1, optimizer='Adam', drop_out=0.2, batch_norm=True, activate_func=PReLU(num_parameters=1), model_name='GTNNWR_20231128-111456', model_save_path='../gtnnwr_models', write_path='../gtnnwr_runs/20231128-111456', use_gpu: bool = True, use_ols: bool = True, log_path: str = '../gtnnwr_logs/', log_file_name: str = 'gtnnwr20231128-111456.log', log_level: int = 20, optimizer_params=None, STPNN_outsize=1, STNN_SPNN_params=None)[source]
Bases:
GNNWR
GTNNWR model is a model based on GNNWR and STPNN, which is a model that can be used to solve the problem of spatial-temporal non-stationarity.
- Parameters:
train_dataset (baseDataset) – the dataset for training
valid_dataset (baseDataset) – the dataset for validation
test_dataset (baseDataset) – the dataset for test
dense_layers (list) – the dense layers of the model (default:
None
) | i.e.[[3],[128,64,32]]
the first list in input is hidden layers of STPNN, the second one is hidden layers of SWNN.start_lr (float) – the start learning rate (default:
0.1
)optimizer (str, optional) – the optimizer of the model (default:
"Adagrad"
) choose from “SGD”,”Adam”,”RMSprop”,”Adagrad”,”Adadelta”drop_out (float) – the drop out rate of the model (default:
0.2
)batch_norm (bool, optional) – whether use batch normalization (default:
True
)activate_func (torch.nn) – the activate function of the model (default:
nn.PReLU(init=0.4)
)model_name (str) – the name of the model (default:
"GNNWR_" + datetime.datetime.today().strftime("%Y%m%d-%H%M%S")
)model_save_path (str) – the path of the model (default:
"../gnnwr_models"
)write_path (str) – the path of the log (default:
"../gnnwr_runs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
)use_gpu (bool) – whether use gpu or not (default:
True
)use_ols (bool) – whether use ols or not (default:
True
)log_path (str) – the path of the log (default:
"../gnnwr_logs/"
)log_file_name (str) – the name of the log (default:
"gnnwr" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") + ".log"
)log_level (int) – the level of the log (default:
logging.INFO
)optimizer_params (dict, optional) –
the params of the optimizer and the scheduler (default:
None
)if optimizer is SGD, the params are:
maxlr: float, the max learning rate (default:0.1
)minlr: float, the min learning rate (default:0.01
)upepoch: int, the epoch of learning rate up (default:10000
)decayepoch: int, the epoch of learning rate decay (default:20000
)decayrate: float, the rate of learning rate decay (default:0.1
)stop_change_epoch: int, the epoch of learning rate stop change (default:30000
)stop_lr: float, the learning rate when stop change (default:0.001
)if optimizer is Other, the params are:
scheduler: str, the name of the scheduler (default:"CosineAnnealingWarmRestarts"
) in {"MultiStepLR","CosineAnnealingLR","CosineAnnealingWarmRestarts"
}scheduler_milestones: list, the milestones of the scheduler MultiStepLR (default:[500,1000,2000,4000]
)scheduler_gamma: float, the gamma of the scheduler MultiStepLR (default:0.5
)scheduler_T_max: int, the T_max of the scheduler CosineAnnealingLR (default:1000
)scheduler_eta_min: float, the eta_min of the scheduler CosineAnnealingLR and CosineAnnealingWarmRestarts (default:0.01
)scheduler_T_0: int, the T_0 of the scheduler CosineAnnealingWarmRestarts (default:100
)scheduler_T_mult: int, the T_mult of the scheduler CosineAnnealingWarmRestarts (default:3
)STPNN_outsize (int) – the output size of STPNN(default:
1
)STNN_SPNN_params (dict) –
the params of STNN and SPNN(default:
None
)STPNN_batch_norm:bool
whether use batchnorm in STNN and SPNN or not (Default:
True
)