Graph Neural Network regression example¶
In [1]:
Copied!
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
from photonai.base import Hyperpipe, PipelineElement
from photonai_graph.GraphUtilities import get_random_connectivity_data, get_random_labels
from sklearn.model_selection import KFold
Make random matrices to simulate connectivity matrices
In [2]:
Copied!
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=100)
y = get_random_labels(l_type="regression", number_of_labels=100)
X = get_random_connectivity_data(number_of_nodes=50, number_of_individuals=100)
y = get_random_labels(l_type="regression", number_of_labels=100)
Design your Pipeline
In [3]:
Copied!
my_pipe = Hyperpipe('basic_gembedding_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='sk_opt',
optimizer_params={'n_configurations': 25},
metrics=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorPercentage', percentage=90.0))
my_pipe.add(PipelineElement('GATRegressor', nn_epochs=20))
my_pipe = Hyperpipe('basic_gembedding_pipe',
inner_cv=KFold(n_splits=5),
outer_cv=KFold(n_splits=5),
optimizer='sk_opt',
optimizer_params={'n_configurations': 25},
metrics=['mean_absolute_error'],
best_config_metric='mean_absolute_error')
my_pipe.add(PipelineElement('GraphConstructorPercentage', percentage=90.0))
my_pipe.add(PipelineElement('GATRegressor', nn_epochs=20))
Finally fit the Model to our data.
In [4]:
Copied!
my_pipe.fit(X, y)
my_pipe.fit(X, y)
===================================================================================================== PHOTONAI ANALYSIS: basic_gembedding_pipe ===================================================================================================== Found 100 target classes: [0.00874305 0.00978279 0.01684908 0.01864002 0.02878775 0.0380922 0.04400718 0.04610329 0.05092174 0.0892 0.10305128 0.11028915 0.11331007 0.11451747 0.13014074 0.1311517 0.13936134 0.1523791 0.15836214 0.16120798 0.16871828 0.17312219 0.17351841 0.18967885 0.21932981 0.21981833 0.22732574 0.23998515 0.25867357 0.25922975 0.26565241 0.27771751 0.3248586 0.34853162 0.36116923 0.36600805 0.36657406 0.37062866 0.37227097 0.38511651 0.39640219 0.40260934 0.42360043 0.45597903 0.45714692 0.46265326 0.47479857 0.48107235 0.48709224 0.49093987 0.5049475 0.52372692 0.52592589 0.53830752 0.54028384 0.55143858 0.57980651 0.60899729 0.63157625 0.63317618 0.64372636 0.65827753 0.66609777 0.67242643 0.67472376 0.6754865 0.68753517 0.68959095 0.69713651 0.69843049 0.70608723 0.70808492 0.71043583 0.72133499 0.74232506 0.75322741 0.76119203 0.77110671 0.80045163 0.80089896 0.80728273 0.81925845 0.82038838 0.84296948 0.84871132 0.84939813 0.85342537 0.88798733 0.90499699 0.90739427 0.91046008 0.91151194 0.92274944 0.92311316 0.93346087 0.93731997 0.94471613 0.94523274 0.95207524 0.97393969] ***************************************************************************************************** Outer Cross validation Fold 1 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 0%| | 0/20 [00:00<?, ?it/s]/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai_graph/NeuralNets/dgl_base.py:357: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at ../torch/csrc/utils/tensor_new.cpp:230.) return batched_graph, torch.tensor(labels, dtype=torch.float32) 100%|██████████| 20/20 [00:02<00:00, 9.29it/s] 100%|██████████| 20/20 [00:02<00:00, 9.76it/s] 100%|██████████| 20/20 [00:02<00:00, 9.54it/s] 100%|██████████| 20/20 [00:02<00:00, 9.42it/s] 100%|██████████| 20/20 [00:01<00:00, 10.02it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2769 | 0.2832 | +---------------------+-------------------+------------------+
100%|██████████| 20/20 [00:02<00:00, 7.43it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2746 | 0.2018 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 2 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 100%|██████████| 20/20 [00:02<00:00, 9.60it/s] 100%|██████████| 20/20 [00:02<00:00, 9.85it/s] 100%|██████████| 20/20 [00:02<00:00, 9.92it/s] 100%|██████████| 20/20 [00:02<00:00, 9.96it/s] 100%|██████████| 20/20 [00:01<00:00, 10.04it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2680 | 0.2730 | +---------------------+-------------------+------------------+
100%|██████████| 20/20 [00:02<00:00, 7.80it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2677 | 0.2291 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 3 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 100%|██████████| 20/20 [00:02<00:00, 9.91it/s] 100%|██████████| 20/20 [00:01<00:00, 10.09it/s] 100%|██████████| 20/20 [00:02<00:00, 9.75it/s] 100%|██████████| 20/20 [00:02<00:00, 9.92it/s] 100%|██████████| 20/20 [00:02<00:00, 9.88it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2459 | 0.2453 | +---------------------+-------------------+------------------+
100%|██████████| 20/20 [00:02<00:00, 7.51it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2456 | 0.3195 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 4 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 100%|██████████| 20/20 [00:02<00:00, 9.84it/s] 100%|██████████| 20/20 [00:02<00:00, 9.75it/s] 100%|██████████| 20/20 [00:02<00:00, 9.36it/s] 100%|██████████| 20/20 [00:02<00:00, 9.30it/s] 100%|██████████| 20/20 [00:01<00:00, 10.12it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2495 | 0.2530 | +---------------------+-------------------+------------------+
100%|██████████| 20/20 [00:02<00:00, 7.58it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2495 | 0.3070 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Outer Cross validation Fold 5 ***************************************************************************************************** Did not find any hyperparameter to convert into skopt space.
/opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/photonai/optimization/scikit_optimize/sk_opt.py:169: UserWarning: Did not find any hyperparameter to convert into skopt space. warnings.warn(msg) 100%|██████████| 20/20 [00:01<00:00, 10.21it/s] 100%|██████████| 20/20 [00:02<00:00, 9.82it/s] 100%|██████████| 20/20 [00:01<00:00, 10.27it/s] 100%|██████████| 20/20 [00:02<00:00, 9.80it/s] 100%|██████████| 20/20 [00:02<00:00, 9.50it/s]
----------------------------------------------------------------------------------------------------- BEST_CONFIG ----------------------------------------------------------------------------------------------------- {} ----------------------------------------------------------------------------------------------------- VALIDATION PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2639 | 0.2622 | +---------------------+-------------------+------------------+
100%|██████████| 20/20 [00:02<00:00, 7.60it/s]
----------------------------------------------------------------------------------------------------- TEST PERFORMANCE ----------------------------------------------------------------------------------------------------- +---------------------+-------------------+------------------+ | METRIC | PERFORMANCE TRAIN | PERFORMANCE TEST | +---------------------+-------------------+------------------+ | mean_absolute_error | 0.2624 | 0.2548 | +---------------------+-------------------+------------------+ ***************************************************************************************************** Finished all outer fold computations.
100%|██████████| 20/20 [00:03<00:00, 5.77it/s]
***************************************************************************************************** ANALYSIS INFORMATION ================================================================================ Project Folder: /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/basic_gembedding_pipe_results_2023-02-17_16-35-15, Computation Time: 2023-02-17 16:35:15.353761 - 2023-02-17 16:36:26.644101 Duration: 0:01:11.290340 Optimized for: mean_absolute_error Hyperparameter Optimizer: sk_opt DUMMY RESULTS ======================================================================================= +-------------------+--+ | PERFORMANCE DUMMY | | +-------------------+--+ +-------------------+--+ AVERAGE PERFORMANCE ACROSS OUTER FOLDS ============================================================== +---------------------+---------------+--------------+-----------+----------+ | Metric Name | Training Mean | Training Std | Test Mean | Test Std | +---------------------+---------------+--------------+-----------+----------+ | mean_absolute_error | 0.259941 | 0.010922 | 0.262448 | 0.044888 | +---------------------+---------------+--------------+-----------+----------+ BEST HYPERPARAMETER CONFIGURATION =================================================================== {} +--------+---------------------+----------------------------+ | fold # | mean_absolute_error | Best Hyperparameter Config | +--------+---------------------+----------------------------+ | 1* | 0.2018 | {} | | 2 | 0.2291 | {} | | 3 | 0.3195 | {} | | 4 | 0.3070 | {} | | 5 | 0.2548 | {} | +--------+---------------------+----------------------------+ PHOTONAI 2.3.0 ====================================================================================== Your results are stored in /home/runner/work/photonai_graph/photonai_graph/documentation/docs/examples/basic_gembedding_pipe_results_2023-02-17_16-35-15 Go to https://explorer.photon-ai.com and upload your photonai_results.json for convenient result visualization! For more info and documentation visit https://www.photon-ai.com
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/IPython/core/formatters.py:972, in MimeBundleFormatter.__call__(self, obj, include, exclude) 969 method = get_real_method(obj, self.print_method) 971 if method is not None: --> 972 return method(include=include, exclude=exclude) 973 return None 974 else: File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:631, in BaseEstimator._repr_mimebundle_(self, **kwargs) 629 output = {"text/plain": repr(self)} 630 if get_config()["display"] == "diagram": --> 631 output["text/html"] = estimator_html_repr(self) 632 return output File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:417, in estimator_html_repr(estimator) 403 fallback_msg = ( 404 "In a Jupyter environment, please rerun this cell to show the HTML" 405 " representation or trust the notebook. <br />On GitHub, the" 406 " HTML representation is unable to render, please try loading this page" 407 " with nbviewer.org." 408 ) 409 out.write( 410 f"<style>{style_with_id}</style>" 411 f'<div id="{container_id}" class="sk-top-container">' (...) 415 '<div class="sk-container" hidden>' 416 ) --> 417 _write_estimator_html( 418 out, 419 estimator, 420 estimator.__class__.__name__, 421 estimator_str, 422 first_call=True, 423 ) 424 out.write("</div></div>") 426 html_output = out.getvalue() File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:151, in _write_estimator_html(out, estimator, estimator_label, estimator_label_details, first_call) 149 """Write estimator to html in serial, parallel, or by itself (single).""" 150 if first_call: --> 151 est_block = _get_visual_block(estimator) 152 else: 153 with config_context(print_changed_only=True): File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:127, in _get_visual_block(estimator) 123 # check if estimator looks like a meta estimator wraps estimators 124 if hasattr(estimator, "get_params"): 125 estimators = [ 126 (key, est) --> 127 for key, est in estimator.get_params(deep=False).items() 128 if hasattr(est, "get_params") and hasattr(est, "fit") 129 ] 130 if estimators: 131 return _VisualBlock( 132 "parallel", 133 [est for _, est in estimators], 134 names=[f"{key}: {est.__class__.__name__}" for key, est in estimators], 135 name_details=[str(est) for _, est in estimators], 136 ) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:211, in BaseEstimator.get_params(self, deep) 209 out = dict() 210 for key in self._get_param_names(): --> 211 value = getattr(self, key) 212 if deep and hasattr(value, "get_params") and not isinstance(value, type): 213 deep_items = value.get_params().items() AttributeError: 'Hyperpipe' object has no attribute 'best_config_metric'
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/IPython/core/formatters.py:342, in BaseFormatter.__call__(self, obj) 340 method = get_real_method(obj, self.print_method) 341 if method is not None: --> 342 return method() 343 return None 344 else: File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:625, in BaseEstimator._repr_html_inner(self) 620 def _repr_html_inner(self): 621 """This function is returned by the @property `_repr_html_` to make 622 `hasattr(estimator, "_repr_html_") return `True` or `False` depending 623 on `get_config()["display"]`. 624 """ --> 625 return estimator_html_repr(self) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:417, in estimator_html_repr(estimator) 403 fallback_msg = ( 404 "In a Jupyter environment, please rerun this cell to show the HTML" 405 " representation or trust the notebook. <br />On GitHub, the" 406 " HTML representation is unable to render, please try loading this page" 407 " with nbviewer.org." 408 ) 409 out.write( 410 f"<style>{style_with_id}</style>" 411 f'<div id="{container_id}" class="sk-top-container">' (...) 415 '<div class="sk-container" hidden>' 416 ) --> 417 _write_estimator_html( 418 out, 419 estimator, 420 estimator.__class__.__name__, 421 estimator_str, 422 first_call=True, 423 ) 424 out.write("</div></div>") 426 html_output = out.getvalue() File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:151, in _write_estimator_html(out, estimator, estimator_label, estimator_label_details, first_call) 149 """Write estimator to html in serial, parallel, or by itself (single).""" 150 if first_call: --> 151 est_block = _get_visual_block(estimator) 152 else: 153 with config_context(print_changed_only=True): File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/utils/_estimator_html_repr.py:127, in _get_visual_block(estimator) 123 # check if estimator looks like a meta estimator wraps estimators 124 if hasattr(estimator, "get_params"): 125 estimators = [ 126 (key, est) --> 127 for key, est in estimator.get_params(deep=False).items() 128 if hasattr(est, "get_params") and hasattr(est, "fit") 129 ] 130 if estimators: 131 return _VisualBlock( 132 "parallel", 133 [est for _, est in estimators], 134 names=[f"{key}: {est.__class__.__name__}" for key, est in estimators], 135 name_details=[str(est) for _, est in estimators], 136 ) File /opt/hostedtoolcache/Python/3.9.16/x64/lib/python3.9/site-packages/sklearn/base.py:211, in BaseEstimator.get_params(self, deep) 209 out = dict() 210 for key in self._get_param_names(): --> 211 value = getattr(self, key) 212 if deep and hasattr(value, "get_params") and not isinstance(value, type): 213 deep_items = value.get_params().items() AttributeError: 'Hyperpipe' object has no attribute 'best_config_metric'
Out[4]:
Hyperpipe(name='basic_gembedding_pipe')