{"id":13415569,"url":"https://github.com/google/edward2","last_synced_at":"2025-09-05T16:20:30.421Z","repository":{"id":35238885,"uuid":"201102332","full_name":"google/edward2","owner":"google","description":"A simple probabilistic programming language.","archived":false,"fork":false,"pushed_at":"2025-07-03T08:29:09.000Z","size":22006,"stargazers_count":700,"open_issues_count":78,"forks_count":78,"subscribers_count":18,"default_branch":"main","last_synced_at":"2025-09-03T06:34:23.844Z","etag":null,"topics":["bayesian-methods","data-science","deep-learning","machine-learning","neural-networks","probabilistic-programming","statistics","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/google.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-08-07T18:01:40.000Z","updated_at":"2025-08-27T13:22:33.000Z","dependencies_parsed_at":"2023-02-18T22:00:32.027Z","dependency_job_id":"c842d3ba-ab22-4c73-ae7a-ab3ae16b1d39","html_url":"https://github.com/google/edward2","commit_stats":{"total_commits":421,"total_committers":43,"mean_commits":9.790697674418604,"dds":0.6080760095011877,"last_synced_commit":"7a11c9782b1e72eef8e2f4fe7914827b799b692f"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/google/edward2","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fedward2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fedward2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fedward2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fedward2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/google","download_url":"https://codeload.github.com/google/edward2/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fedward2/sbom","scorecard":{"id":436829,"data":{"date":"2025-08-11","repo":{"name":"github.com/google/edward2","commit":"ee025600cb3cf288230f34929d8a7110e719686b"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":5.5,"checks":[{"name":"Code-Review","score":5,"reason":"Found 16/30 approved changesets -- score normalized to 5","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Apache License 2.0: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Security-Policy","score":10,"reason":"security policy file detected","details":["Info: security policy file detected: github.com/google/.github/SECURITY.md:1","Info: Found linked content: github.com/google/.github/SECURITY.md:1","Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/google/.github/SECURITY.md:1","Info: Found text in security policy: github.com/google/.github/SECURITY.md:1"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"SAST","score":2,"reason":"SAST tool is not run on all commits -- score normalized to 2","details":["Warn: 3 commits out of 14 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-19T04:53:26.422Z","repository_id":35238885,"created_at":"2025-08-19T04:53:26.422Z","updated_at":"2025-08-19T04:53:26.422Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273572190,"owners_count":25129670,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-04T02:00:08.968Z","response_time":61,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-methods","data-science","deep-learning","machine-learning","neural-networks","probabilistic-programming","statistics","tensorflow"],"created_at":"2024-07-30T21:00:50.379Z","updated_at":"2025-09-05T16:20:30.386Z","avatar_url":"https://github.com/google.png","language":"Jupyter Notebook","readme":"# Edward2\n\nEdward2 is a _simple_ probabilistic programming language. It provides core\nutilities in deep learning ecosystems so that one can write models as\nprobabilistic programs and manipulate a model's computation for flexible\ntraining and inference. It's organized as follows:\n\n* [`edward2/`](https://github.com/google/edward2/blob/main/edward2/):\n  Library code.\n* [`examples/`](https://github.com/google/edward2/blob/main/examples):\n  Examples.\n* [`experimental/`](https://github.com/google/edward2/blob/main/experimental):\n  Active research projects.\n\nAre you upgrading from Edward? Check out the guide\n[`Upgrading_from_Edward_to_Edward2.md`](https://github.com/google/edward2/blob/main/Upgrading_From_Edward_To_Edward2.md).\nThe core utilities are fairly low-level: if you'd like a high-level module for\nuncertainty modeling, check out the guide for\n[Bayesian Layers](https://github.com/google/edward2/tree/main/edward2/tensorflow/layers).\nWe recommend the\n[Uncertainty Baselines](https://github.com/google/uncertainty-baselines)\nif you'd like to build on research-ready code.\n\n## Installation\n\nWe recommend the latest development version. To install, run\n\n```sh\npip install \"edward2 @ git+https://github.com/google/edward2.git\"\n```\n\nYou can also install the latest stable version using the following. As a caveat,\nhowever, we very rarely update the stable version (this is a passion project\nmaintained by part-timers and scheduling releases every so often sucks up time).\n\n```sh\npip install edward2\n```\n\nEdward2 supports three backends: TensorFlow (the default), JAX, and NumPy ([see\nbelow to activate](#using-the-jax-or-numpy-backend)). Installing `edward2` does\nnot automatically install any backend. To get these dependencies, use for\nexample `pip install edward2[tensorflow]\"`, replacing `tensorflow` for the\nappropriate backend. Sometimes Edward2 uses the latest changes from TensorFlow\nin which you'll need TensorFlow's nightly package: use `pip install edward2[tf-\nnightly]`.\n\n## 1. Models as Probabilistic Programs\n\n### Random Variables\n\nIn Edward2, we use\n[`RandomVariables`](https://github.com/google/edward2/blob/main/edward2/tensorflow/random_variable.py)\nto specify a probabilistic model's structure.\nA random variable `rv` carries a probability distribution (`rv.distribution`),\nwhich is a TensorFlow Distribution instance governing the random variable's methods\nsuch as `log_prob` and `sample`.\n\nRandom variables are formed like TensorFlow Distributions.\n\n```python\nimport edward2 as ed\n\nnormal_rv = ed.Normal(loc=0., scale=1.)\n## \u003ced.RandomVariable 'Normal/' shape=() dtype=float32 numpy=0.0024812892\u003e\nnormal_rv.distribution.log_prob(1.231)\n## \u003ctf.Tensor: id=11, shape=(), dtype=float32, numpy=-1.6766189\u003e\n\ndirichlet_rv = ed.Dirichlet(concentration=tf.ones([2, 3]))\n## \u003ced.RandomVariable 'Dirichlet/' shape=(2, 3) dtype=float32 numpy=\narray([[0.15864784, 0.01217205, 0.82918006],\n       [0.23385087, 0.69622266, 0.06992647]], dtype=float32)\u003e\n```\n\nBy default, instantiating a random variable `rv` creates a sampling op to form\nthe tensor `rv.value ~ rv.distribution.sample()`. The default number of samples\n(controllable via the `sample_shape` argument to `rv`) is one, and if the\noptional `value` argument is provided, no sampling op is created. Random\nvariables can interoperate with TensorFlow ops: the TF ops operate on the sample.\n\n```python\nx = ed.Normal(loc=tf.zeros(2), scale=tf.ones(2))\ny = 5.\nx + y, x / y\n## (\u003ctf.Tensor: id=109, shape=(2,), dtype=float32, numpy=array([3.9076924, 4.588356 ], dtype=float32)\u003e,\n##  \u003ctf.Tensor: id=111, shape=(2,), dtype=float32, numpy=array([-0.21846154, -0.08232877], dtype=float32)\u003e)\ntf.tanh(x * y)\n## \u003ctf.Tensor: id=114, shape=(2,), dtype=float32, numpy=array([-0.99996394, -0.9679181 ], dtype=float32)\u003e\nx[1]  # 2nd normal rv\n## \u003ced.RandomVariable 'Normal/' shape=() dtype=float32 numpy=-0.41164386\u003e\n```\n\n### Probabilistic Models\n\nProbabilistic models in Edward2 are expressed as Python functions that\ninstantiate one or more `RandomVariables`. Typically, the function (\"program\")\nexecutes the generative process and returns samples. Inputs to the\nfunction can be thought of as values the model conditions on.\n\nBelow we write Bayesian logistic regression, where binary outcomes are generated\ngiven features, coefficients, and an intercept. There is a prior over the\ncoefficients and intercept. Executing the function adds operations samples\ncoefficients and intercept from the prior and uses these samples to compute the\noutcomes.\n\n```python\ndef logistic_regression(features):\n  \"\"\"Bayesian logistic regression p(y | x) = int p(y | x, w, b) p(w, b) dwdb.\"\"\"\n  coeffs = ed.Normal(loc=tf.zeros(features.shape[1]), scale=1., name=\"coeffs\")\n  intercept = ed.Normal(loc=0., scale=1., name=\"intercept\")\n  outcomes = ed.Bernoulli(\n      logits=tf.tensordot(features, coeffs, [[1], [0]]) + intercept,\n      name=\"outcomes\")\n  return outcomes\n\nnum_features = 10\nfeatures = tf.random.normal([100, num_features])\noutcomes = logistic_regression(features)\n# \u003ced.RandomVariable 'outcomes/' shape=(100,) dtype=int32 numpy=\n# array([1, 0, ... 0, 1], dtype=int32)\u003e\n```\n\nEdward2 programs can also represent distributions beyond those which directly\nmodel data. For example, below we write a learnable distribution with the\nintention to approximate it to the logistic regression posterior.\n\n```python\ndef logistic_regression_posterior(coeffs_loc, coeffs_scale,\n                                  intercept_loc, intercept_scale):\n  \"\"\"Posterior of Bayesian logistic regression p(w, b | {x, y}).\"\"\"\n  coeffs = ed.MultivariateNormalTriL(\n      loc=coeffs_loc,\n      scale_tril=tfp.trainable_distributions.tril_with_diag_softplus_and_shift(\n          coeffs_scale),\n      name=\"coeffs_posterior\")\n  intercept = ed.Normal(\n      loc=intercept_loc,\n      scale=tf.nn.softplus(intercept_scale) + 1e-5,\n      name=\"intercept_posterior\")\n  return coeffs, intercept\n\ncoeffs_loc = tf.Variable(tf.random.normal([num_features]))\ncoeffs_scale = tf.Variable(tf.random.normal(\n    [num_features*(num_features+1) // 2]))\n\nintercept_loc = tf.Variable(tf.random.normal([]))\nintercept_scale = tf.Variable(tf.random.normal([]))\nposterior_coeffs, posterior_intercept = logistic_regression_posterior(\n    coeffs_loc, coeffs_scale, intercept_loc, intercept_scale)\n```\n\n## 2. Manipulating Model Computation\n\n### Tracing\n\nTraining and testing probabilistic models typically require more than just\nsamples from the generative process. To enable flexible training and testing, we\nmanipulate the model's computation using\n[tracing](https://github.com/google/edward2/blob/main/edward2/tracer.py).\n\nA tracer is a function that acts on another function `f` and its arguments\n`*args`, `**kwargs`. It performs various computations before returning an output\n(typically `f(*args, **kwargs)`: the result of applying the function itself).\nThe `ed.trace` context manager pushes tracers onto a stack, and any\ntraceable function is intercepted by the stack. All random variable\nconstructors are traceable.\n\nBelow we trace the logistic regression model's generative process. In\nparticular, we make predictions with its learned posterior means rather than\nwith its priors.\n\n```python\ndef set_prior_to_posterior_mean(f, *args, **kwargs):\n  \"\"\"Forms posterior predictions, setting each prior to its posterior mean.\"\"\"\n  name = kwargs.get(\"name\")\n  if name == \"coeffs\":\n    return posterior_coeffs.distribution.mean()\n  elif name == \"intercept\":\n    return posterior_intercept.distribution.mean()\n  return f(*args, **kwargs)\n\nwith ed.trace(set_prior_to_posterior_mean):\n  predictions = logistic_regression(features)\n\ntraining_accuracy = (\n    tf.reduce_sum(tf.cast(tf.equal(predictions, outcomes), tf.float32)) /\n    tf.cast(outcomes.shape[0], tf.float32))\n```\n\n### Program Transformations\n\nUsing tracing, one can also apply program transformations, which map\nfrom one representation of a model to another. This provides convenient access\nto different model properties depending on the downstream use case.\n\nFor example, Markov chain Monte Carlo algorithms often require a model's\nlog-joint probability function as input. Below we take the Bayesian logistic\nregression program which specifies a generative process, and apply the built-in\n`ed.make_log_joint` transformation to obtain its log-joint probability function.\nThe log-joint function takes as input the generative program's original inputs\nas well as random variables in the program. It returns a scalar Tensor\nsumming over all random variable log-probabilities.\n\nIn our example, `features` and `outcomes` are fixed, and we want to use\nHamiltonian Monte Carlo to draw samples from the posterior distribution of\n`coeffs` and `intercept`. To this use, we create `target_log_prob_fn`, which\ntakes just `coeffs` and `intercept` as arguments and pins the input `features`\nand output rv `outcomes` to its known values.\n\n```python\nimport no_u_turn_sampler  # local file import\n\n# Set up training data.\nfeatures = tf.random.normal([100, 55])\noutcomes = tf.random.uniform([100], minval=0, maxval=2, dtype=tf.int32)\n\n# Pass target log-probability function to MCMC transition kernel.\nlog_joint = ed.make_log_joint_fn(logistic_regression)\n\ndef target_log_prob_fn(coeffs, intercept):\n  \"\"\"Target log-probability as a function of states.\"\"\"\n  return log_joint(features,\n                   coeffs=coeffs,\n                   intercept=intercept,\n                   outcomes=outcomes)\n\ncoeffs_samples = []\nintercept_samples = []\ncoeffs = tf.random.normal([55])\nintercept = tf.random.normal([])\ntarget_log_prob = None\ngrads_target_log_prob = None\nfor _ in range(1000):\n  [\n      [coeffs, intercepts],\n      target_log_prob,\n      grads_target_log_prob,\n  ] = no_u_turn_sampler.kernel(\n          target_log_prob_fn=target_log_prob_fn,\n          current_state=[coeffs, intercept],\n          step_size=[0.1, 0.1],\n          current_target_log_prob=target_log_prob,\n          current_grads_target_log_prob=grads_target_log_prob)\n  coeffs_samples.append(coeffs)\n  intercept_samples.append(coeffs)\n```\n\nThe returned `coeffs_samples` and `intercept_samples` contain 1,000 posterior\nsamples for `coeffs` and `intercept` respectively. They may be used, for\nexample, to evaluate the model's posterior predictive on new data.\n\n## Using the JAX or NumPy backend\n\nUsing alternative backends is as simple as the following:\n\n```python\nimport edward2.numpy as ed  # NumPy backend\nimport edward2.jax as ed  # or, JAX backend\n```\n\nIn the NumPy backend, Edward2 wraps SciPy distributions. For example, here's\nlinear regression.\n\n```python\ndef linear_regression(features, prior_precision):\n  beta = ed.norm.rvs(loc=0.,\n                     scale=1. / np.sqrt(prior_precision),\n                     size=features.shape[1])\n  y = ed.norm.rvs(loc=np.dot(features, beta), scale=1., size=1)\n  return y\n```\n\n## References\n\nIn general, we recommend citing the following article.\n\n\u003e Tran, D., Hoffman, M. D., Moore, D., Suter, C., Vasudevan S., Radul A.,\n\u003e Johnson M., and Saurous R. A. (2018).\n\u003e [Simple, Distributed, and Accelerated Probabilistic Programming](https://arxiv.org/abs/1811.02091).\n\u003e In _Neural Information Processing Systems_.\n\n```none\n@inproceedings{tran2018simple,\n  author = {Dustin Tran and Matthew D. Hoffman and Dave Moore and Christopher Suter and Srinivas Vasudevan and Alexey Radul and Matthew Johnson and Rif A. Saurous},\n  title = {Simple, Distributed, and Accelerated Probabilistic Programming},\n  booktitle = {Neural Information Processing Systems},\n  year = {2018},\n}\n```\n\nIf you'd like to cite the layers module specifically, use the following article.\n\n\u003e Tran, D., Dusenberry M. W., van der Wilk M., Hafner D. (2019).\n\u003e [Bayesian Layers: A Module for Neural Network Uncertainty](https://arxiv.org/abs/1812.03973).\n\u003e In _Neural Information Processing Systems_.\n\n```none\n@inproceedings{tran2019bayesian,\n  author = {Dustin Tran and Michael W. Dusenberry and Danijar Hafner and Mark van der Wilk},\n  title={Bayesian {L}ayers: A module for neural network uncertainty},\n  booktitle = {Neural Information Processing Systems},\n  year={2019}\n}\n```\n","funding_links":[],"categories":["Jupyter Notebook","Uncategorized","Probabilistic Programming Languages (PPL) (a.k.a. Build your own Topic Model)","Sample Codes / Projects \u003ca name=\"sample\" /\u003e ⛏️📐📁"],"sub_categories":["Uncategorized","Embedding based Topic Models","Reinforcement Learning \u003ca name=\"RL\" /\u003e🔮"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fedward2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogle%2Fedward2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fedward2/lists"}