# DeepParameters - Integrating Deep Learning and Bayesian Networks
**DeepParameters** is a Python library for learning Bayesian Network parameters -- Conditional Probability Distributions (CPDs) -- using deep learning models. The package is highly flexibile allowing for multiple parameters to learn Bayesian network parameters.
Developed and tested on OSX; for errors on other platforms please contact the designation persons for their logging of the bugs.
## Installation
```bash
pip install DeepParameters
```
A DEFAULT_DIR is set up by default as `outputs/`, use bash command:
```bash
export DEFAULT_DIR=/path/to/custom/directory
```
On windows:
```bash
set DEFAULT_DIR=C:\path\to\custom\directory
```
## Dependencies
bng has the following non-optional dependencies:
- numpy
- pandas
- pgmpy
- matplotlib
- sklearn
- pickle
- os
- pathlib
- datetime
- json
- scipy
- tensorflow
- networkx
- keras
- tensorflow-probability
## Usage/Examples
The main function is **learn_cpd_for_node**
Then function is used to create a probabilistic graphical model (PGM) and accompanying sample data.
**Parameters**:
```python
Learn Conditional Probability Distributions (CPDs) using a deep learning model for a single node.
Parameters:
- node (str): The node for which to learn the CPD.
- data (DataFrame): The dataset containing the samples.
- true_model (BayesianNetwork): The true Bayesian network model used for comparison.
- learnt_bn_structure (BayesianNetwork or dict): The learned Bayesian network structure.
- num_parameters (int): Number of parameters for the deep learning model.
- indegree (int): The maximum indegree of the Bayesian network.
- es_patience (int): Patience for early stopping during model training.
- train_size (float): The proportion of the dataset to include in the train split.
- val_size (float): The proportion of the training dataset to include in the validation split.
- epochs (int): The number of training epochs for the deep learning model.
- batch_size (int): The batch size for training the deep learning model.
- sample_size (int): The size of data to sample from the Bayesian network.
- visualize_it (bool): Whether to visualize the model training process.
- network_type (str): The type of deep learning network to use. Options include 'naive', 'simple', 'medium', 'large', 'bnn'.
- sampling_method (str): The method to use for sampling the learned weights. Options include '1', '2', '3', '4'.
Accepted formats:
* '1', '2', '3', '4'
* Full method names ('weighted', 'stratified', 'kde', 'bayesian')
* First letters ('w', 's', 'k', 'b')
Returns:
- A TabularCPD object for the node.
```
**Returns**:
```
dict: Dictionary containing the model, samples, and runtime.
```
```python
from DeepParameters.LearnDeepLearningCPDs import learn_cpd_for_node
# Define a simple Bayesian Network
model = BayesianNetwork([('A', 'B'), ('B', 'C')])
# Define CPDs for the model
cpd_a = TabularCPD(variable='A', variable_card=2, values=[[0.6], [0.4]])
cpd_b = TabularCPD(variable='B', variable_card=2, values=[[0.6, 0.4], [0.5, 0.5]], evidence=['A'], evidence_card=[2])
cpd_c = TabularCPD(variable='C', variable_card=2, values=[[0.9, 0.4], [0.1, 0.6]], evidence=['B'], evidence_card=[2])
# Add CPDs to the model
model.add_cpds(cpd_a, cpd_b, cpd_c)
# Verify the model
assert model.check_model()
# Generate sample data from the model
sampler = BayesianModelSampling(model)
data = sampler.forward_sample(size=1000)
# Learn CPD for node 'B'
cpd_b_learned = learn_cpd_for_node('B', data, model, model, num_parameters=10, network_type='autoencoder', sampling_method="4")
print("Learned CPD for node 'B':")
print(cpd_b_learned)
```
```python
Generating for node: C: 100%
3/3 [00:00<00:00, 33.39it/s]
Epoch 1/10
24/24 ━━━━━━━━━━━━━━━━━━━━ 4s 62ms/step - accuracy: 0.5176 - kl_divergence: 0.5771 - loss: 0.6213 - val_accuracy: 0.5750 - val_kl_divergence: 0.2780 - val_loss: 0.6081
Epoch 2/10
24/24 ━━━━━━━━━━━━━━━━━━━━ 1s 29ms/step - accuracy: 0.4230 - kl_divergence: 0.1474 - loss: 0.6355 - val_accuracy: 0.5750 - val_kl_divergence: 4.6373e-05 - val_loss: 0.5593
Epoch 3/10
24/24 ━━━━━━━━━━━━━━━━━━━━ 0s 18ms/step - accuracy: 0.4687 - kl_divergence: 2.6430e-05 - loss: 1.1652 - val_accuracy: 0.3750 - val_kl_divergence: -1.7157e-06 - val_loss: 0.6282
Epoch 4/10
24/24 ━━━━━━━━━━━━━━━━━━━━ 1s 24ms/step - accuracy: 0.4434 - kl_divergence: -1.7775e-06 - loss: 8.0636 - val_accuracy: 0.3750 - val_kl_divergence: -1.7728e-06 - val_loss: 0.6203
Epoch 4: early stopping
learned_weights [0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625
0.015625 0.015625]
learned_weights [0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625
0.015625 0.015625]
Learned CPD for node 'B':
+------+---------------------+--------------------+
| A | A(0) | A(1) |
+------+---------------------+--------------------+
| B(0) | 0.5000000112159115 | 0.499999977353993 |
+------+---------------------+--------------------+
| B(1) | 0.49999998878408836 | 0.5000000226460071 |
+------+---------------------+--------------------+
```
## Citing
Please use the following bibtex for citing bng in your research:
@{mulaudzi2024deepparameters,
title={DeepParameters: Bayesian Network parameter learning using Deep Learning in Python},
author={Mulaudzi, Rudzani},
year={2024},
organization={University of Witwaterand}
}
## Licensing
bng is released under MIT License.
## Contributing
Coming soon. Email rudzani.mulaudzi2@students.wits.ac.za
Raw data
{
"_id": null,
"home_page": "https://github.com/rudzanimulaudzi/DeepParameters",
"name": "DeepParameters",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": null,
"author": "Your Name",
"author_email": "rudzani.mulaudzi2@students.wits.ac.za",
"download_url": "https://files.pythonhosted.org/packages/58/20/96599053b23d66574d89ec81a9b785dbd7aa914930708f1761f3592512df/deepparameters-0.0.6.tar.gz",
"platform": null,
"description": "# DeepParameters - Integrating Deep Learning and Bayesian Networks\n\n**DeepParameters** is a Python library for learning Bayesian Network parameters -- Conditional Probability Distributions (CPDs) -- using deep learning models. The package is highly flexibile allowing for multiple parameters to learn Bayesian network parameters.\n\nDeveloped and tested on OSX; for errors on other platforms please contact the designation persons for their logging of the bugs.\n\n## Installation\n\n```bash\npip install DeepParameters\n```\n\nA DEFAULT_DIR is set up by default as `outputs/`, use bash command:\n\n```bash\nexport DEFAULT_DIR=/path/to/custom/directory\n```\nOn windows:\n\n```bash\nset DEFAULT_DIR=C:\\path\\to\\custom\\directory\n```\n\n## Dependencies\n\nbng has the following non-optional dependencies:\n\n- numpy\n- pandas\n- pgmpy\n- matplotlib\n- sklearn\n- pickle\n- os\n- pathlib\n- datetime\n- json\n- scipy\n- tensorflow\n- networkx\n- keras\n- tensorflow-probability\n\n\n## Usage/Examples\n\nThe main function is **learn_cpd_for_node**\n\nThen function is used to create a probabilistic graphical model (PGM) and accompanying sample data.\n\n**Parameters**:\n```python\nLearn Conditional Probability Distributions (CPDs) using a deep learning model for a single node.\n \n Parameters:\n - node (str): The node for which to learn the CPD.\n - data (DataFrame): The dataset containing the samples.\n - true_model (BayesianNetwork): The true Bayesian network model used for comparison.\n - learnt_bn_structure (BayesianNetwork or dict): The learned Bayesian network structure.\n - num_parameters (int): Number of parameters for the deep learning model.\n - indegree (int): The maximum indegree of the Bayesian network.\n - es_patience (int): Patience for early stopping during model training.\n - train_size (float): The proportion of the dataset to include in the train split.\n - val_size (float): The proportion of the training dataset to include in the validation split.\n - epochs (int): The number of training epochs for the deep learning model.\n - batch_size (int): The batch size for training the deep learning model.\n - sample_size (int): The size of data to sample from the Bayesian network.\n - visualize_it (bool): Whether to visualize the model training process.\n - network_type (str): The type of deep learning network to use. Options include 'naive', 'simple', 'medium', 'large', 'bnn'.\n - sampling_method (str): The method to use for sampling the learned weights. Options include '1', '2', '3', '4'.\n \n Accepted formats:\n * '1', '2', '3', '4'\n * Full method names ('weighted', 'stratified', 'kde', 'bayesian')\n * First letters ('w', 's', 'k', 'b')\n \n Returns:\n - A TabularCPD object for the node.\n```\n\n**Returns**:\n```\ndict: Dictionary containing the model, samples, and runtime.\n```\n\n```python\nfrom DeepParameters.LearnDeepLearningCPDs import learn_cpd_for_node\n\n# Define a simple Bayesian Network\nmodel = BayesianNetwork([('A', 'B'), ('B', 'C')])\n\n# Define CPDs for the model\ncpd_a = TabularCPD(variable='A', variable_card=2, values=[[0.6], [0.4]])\ncpd_b = TabularCPD(variable='B', variable_card=2, values=[[0.6, 0.4], [0.5, 0.5]], evidence=['A'], evidence_card=[2])\ncpd_c = TabularCPD(variable='C', variable_card=2, values=[[0.9, 0.4], [0.1, 0.6]], evidence=['B'], evidence_card=[2])\n\n# Add CPDs to the model\nmodel.add_cpds(cpd_a, cpd_b, cpd_c)\n\n# Verify the model\nassert model.check_model()\n\n# Generate sample data from the model\nsampler = BayesianModelSampling(model)\ndata = sampler.forward_sample(size=1000)\n\n# Learn CPD for node 'B'\ncpd_b_learned = learn_cpd_for_node('B', data, model, model, num_parameters=10, network_type='autoencoder', sampling_method=\"4\")\n\nprint(\"Learned CPD for node 'B':\")\nprint(cpd_b_learned)\n\n\n```\n```python\nGenerating\u2007for\u2007node:\u2007C:\u2007100%\n\u20073/3\u2007[00:00<00:00,\u200733.39it/s]\nEpoch 1/10\n24/24 \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 4s 62ms/step - accuracy: 0.5176 - kl_divergence: 0.5771 - loss: 0.6213 - val_accuracy: 0.5750 - val_kl_divergence: 0.2780 - val_loss: 0.6081\nEpoch 2/10\n24/24 \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 1s 29ms/step - accuracy: 0.4230 - kl_divergence: 0.1474 - loss: 0.6355 - val_accuracy: 0.5750 - val_kl_divergence: 4.6373e-05 - val_loss: 0.5593\nEpoch 3/10\n24/24 \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 0s 18ms/step - accuracy: 0.4687 - kl_divergence: 2.6430e-05 - loss: 1.1652 - val_accuracy: 0.3750 - val_kl_divergence: -1.7157e-06 - val_loss: 0.6282\nEpoch 4/10\n24/24 \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 1s 24ms/step - accuracy: 0.4434 - kl_divergence: -1.7775e-06 - loss: 8.0636 - val_accuracy: 0.3750 - val_kl_divergence: -1.7728e-06 - val_loss: 0.6203\nEpoch 4: early stopping\nlearned_weights [0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625\n 0.015625 0.015625]\nlearned_weights [0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625 0.015625\n 0.015625 0.015625]\nLearned CPD for node 'B':\n+------+---------------------+--------------------+\n| A | A(0) | A(1) |\n+------+---------------------+--------------------+\n| B(0) | 0.5000000112159115 | 0.499999977353993 |\n+------+---------------------+--------------------+\n| B(1) | 0.49999998878408836 | 0.5000000226460071 |\n+------+---------------------+--------------------+\n```\n\n## Citing\n\nPlease use the following bibtex for citing bng in your research:\n\n@{mulaudzi2024deepparameters,\n title={DeepParameters: Bayesian Network parameter learning using Deep Learning in Python},\n author={Mulaudzi, Rudzani},\n year={2024},\n organization={University of Witwaterand}\n}\n\n## Licensing\n\nbng is released under MIT License. \n\n\n\n\n\n## Contributing\n\nComing soon. Email rudzani.mulaudzi2@students.wits.ac.za\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A package for learning CPDs using deep learning models",
"version": "0.0.6",
"project_urls": {
"Homepage": "https://github.com/rudzanimulaudzi/DeepParameters"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fd98d697936bee4b584613d661945694ac32da0bd3a98ca505d4aef911adee41",
"md5": "cde549c5f5586e2627e6546954b80b61",
"sha256": "af8bc158b02ae2239c76467dbafb46a609fae7164c1cf7fc334bc44089c5e123"
},
"downloads": -1,
"filename": "DeepParameters-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "cde549c5f5586e2627e6546954b80b61",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 21440,
"upload_time": "2025-02-04T21:10:08",
"upload_time_iso_8601": "2025-02-04T21:10:08.943401Z",
"url": "https://files.pythonhosted.org/packages/fd/98/d697936bee4b584613d661945694ac32da0bd3a98ca505d4aef911adee41/DeepParameters-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "582096599053b23d66574d89ec81a9b785dbd7aa914930708f1761f3592512df",
"md5": "aa77ac846e026a897a704d84543d5356",
"sha256": "f1285fc0536bc2c3984c5af5d5b33d7554dfd6122358e376c5799bd3a1278126"
},
"downloads": -1,
"filename": "deepparameters-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "aa77ac846e026a897a704d84543d5356",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 19885,
"upload_time": "2025-02-04T21:10:11",
"upload_time_iso_8601": "2025-02-04T21:10:11.037997Z",
"url": "https://files.pythonhosted.org/packages/58/20/96599053b23d66574d89ec81a9b785dbd7aa914930708f1761f3592512df/deepparameters-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-02-04 21:10:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "rudzanimulaudzi",
"github_project": "DeepParameters",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "deepparameters"
}