{"id":17012008,"url":"https://github.com/renatogeh/gospn","last_synced_at":"2025-03-23T17:31:20.434Z","repository":{"id":57519981,"uuid":"61749163","full_name":"RenatoGeh/gospn","owner":"RenatoGeh","description":"A free, open-source inference and learning library for Sum-Product Networks (SPN)","archived":false,"fork":false,"pushed_at":"2019-01-16T01:35:10.000Z","size":26996,"stargazers_count":24,"open_issues_count":11,"forks_count":5,"subscribers_count":6,"default_branch":"dev","last_synced_at":"2025-03-18T22:28:08.464Z","etag":null,"topics":["classification","dataset","deep-learning","golang","gonum","graph","hacktoberfest","image-reconstruction","inference","natural-language-processing","pgm","probability","spn","statistics"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RenatoGeh.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}},"created_at":"2016-06-22T20:25:16.000Z","updated_at":"2024-12-21T13:56:31.000Z","dependencies_parsed_at":"2022-09-05T11:30:15.778Z","dependency_job_id":null,"html_url":"https://github.com/RenatoGeh/gospn","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenatoGeh%2Fgospn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenatoGeh%2Fgospn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenatoGeh%2Fgospn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenatoGeh%2Fgospn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RenatoGeh","download_url":"https://codeload.github.com/RenatoGeh/gospn/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245140988,"owners_count":20567485,"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","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":["classification","dataset","deep-learning","golang","gonum","graph","hacktoberfest","image-reconstruction","inference","natural-language-processing","pgm","probability","spn","statistics"],"created_at":"2024-10-14T06:09:13.071Z","updated_at":"2025-03-23T17:31:17.704Z","avatar_url":"https://github.com/RenatoGeh.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"GoSPN\n=====\n\n[![Build Status](https://travis-ci.org/RenatoGeh/gospn.svg?branch=stable)](https://travis-ci.org/RenatoGeh/gospn)\n[![Go Report Card](https://goreportcard.com/badge/github.com/renatogeh/gospn)](https://goreportcard.com/report/github.com/renatogeh/gospn)\n[![GoDoc](https://godoc.org/github.com/RenatoGeh/gospn?status.svg)](https://godoc.org/github.com/RenatoGeh/gospn)\n[![License](https://img.shields.io/badge/License-BSD--3-blue.svg)](https://github.com/RenatoGeh/gospn/blob/dev/LICENSE)\n\n![](./gospnpher.png \"My crude attempt at drawing Renee French's Go Gopher.\")\n\nMy crude (and slightly terrifying) rendition of Renee French's Go [Gopher](https://blog.golang.org/gopher) writing what's on his mind.\n\nGoSPN: A Sum-Product Network (SPN) Library\n------------------------------------------\n\n### Overview\n\nSum-Product Networks (SPNs) are deep probabilistic graphical models\n(PGMs) that compactly represent tractable probability distributions.\nExact inference in SPNs is computed in time linear in the number of\nedges, an attractive feature that distinguishes SPNs from other PGMs.\nHowever, learning SPNs is a tough task. There have been many advances in\nlearning the structure and parameters of SPNs in the past few years. One\ninteresting feature is the fact that we can make use of SPNs' deep\narchitecture and perform deep learning on these models. Since the number\nof hidden layers not only doesn't negatively impact the tractability of\ninference of SPNs but also augments the representability of this model,\nit is very much desirable to continue research on deep learning of SPNs.\n\nThis project aims to provide a simple framework for Sum-Product\nNetworks. Our objective is to provide inference tools and implement\nvarious learning algorithms present in literature.\n\n### Roadmap\n\n## All\n\n- [ ] Unit tests\n- [ ] Support for continuos variables\n\n## Inference\n\n- [x] Soft inference (marginal probabilities)\n- [x] Hard inference (MAP) through max-product algorithm\n\n## Structure learning\n\n- [x] Gens-Domingos learning schema (LearnSPN) [1]\n- [x] Dennis-Ventura clustering structural learning algorithm [2]\n- [x] Poon-Domingos dense architecture [3]\n\n## Weight learning\n\n- [x] Computation of SPN derivatives\n- [x] Soft generative gradient descent\n- [x] Hard generative gradient descent\n- [x] Soft discriminative gradient descent\n- [x] Hard discriminative gradient descent\n\n## Input/Output\n\n- [x] Support for `.npy` files\n- [x] Support for `.arff` dataset format (discrete variables only)\n- [ ] Support for `.csv` dataset file format\n- [x] Support for our own `.data` dataset format\n- [x] Serialization of SPNs\n\n### References\n\n- [1] *Learning the Structure of Sum-Product Networks*, R. Gens \u0026 P.\n  Domingos, ICML 2013\n- [2] *Learning the Architecture of Sum-Product Networks Using\n  Clustering on Variables*, A. Dennis \u0026 D. Ventura, NIPS 25 (2012)\n- [3] *Sum-Product Networks: A New Deep Architecture*, H. Poon \u0026 P.\n  Domingos, UAI 2011\n\n### Looking to contribute?\n\nSee the [Contribution\nGuidelines](https://github.com/RenatoGeh/gospn/blob/dev/CONTRIBUTING.md).\n\n### Branches\n\n- `dev` contains the development version of GoSPN.\n- `stable` contains a stable version of GoSPN.\n- `nlp` contains deprecated NLP model.\n\n### Usage\n\n#### As a Go library\n\nGoDocs: https://godoc.org/github.com/RenatoGeh/gospn\n\nLearning algorithms are inside the `github.com/RenatoGeh/gospn/learn`\npackage, with each algorithm as a subpackage of `learn` (e.g.\n`learn/gens`, `learn/dennis`, `learn/poon`).\n\nTo parse an ARFF format dataset and perform learning with the\nGens-Domingos structure learning algorithm:\n\nFirst import the relevant packages (e.g. `learn/gens` for Gens' structural\nlearning algorithm, `io` for `ParseArff` and `spn` for inference\nmethods):\n\n```\nimport (\n  \"github.com/RenatoGeh/gospn/learn/gens\"\n  \"github.com/RenatoGeh/gospn/io\"\n  \"github.com/RenatoGeh/gospn/spn\"\n)\n```\n\nExtract contents from an ARFF file (for now only discrete variables):\n\n```\nname, scope, values, labels := io.ParseArff(\"filename.arff\")\n```\n\nSend the relevant information to the learning algorithm:\n\n```\nS := gens.Learn(scope, values, -1, 0.0001, 4.0, 4)\n```\n\n`S` is the resulting SPN. We can now compute the marginal probabilities\ngiven a `spn.VarSet`:\n\n```\nevidence := make(spn.VarSet)\nevidence[0] = 1 // Variable 0 = 1\n// Summing out variable 1\nevidence[2] = 0 // Variable 2 = 0\n// Summing out all other variables.\np := S.Value(evidence)\n// p is the marginal Pr(evidence), since S is already valid and normalized.\n```\n\nThe method `S.Value` may repeat calculations if the SPN's graph is not a\ntree. To use dynamic programming and avoid recomputations, either use\n`spn.Inference` or `spn.Storer`:\n\n```\n// This only returns the desired probability (in logspace).\np := spn.Inference(S, evidence)\n\n// A Storer stores values for all nodes.\nT := spn.NewStorer()\nt := T.NewTicket() // Creates a new DP table.\nspn.StoreInference(S, evidence, t, T) // Stores inference values from each node to T(t).\np = T.Single(t, S) // Returns the first value inside node S: T(t, S).\n```\n\nFinding the approximate MPE works the same way. Let `evidence` be some\nevidence, the MPE is given by:\n\n```\nargs, mpe := S.ArgMax(evidence) // mpe is the probability and args is the argmax valuation.\n```\n\nSimilarly to `S.Value`, `S.ArgMax` may recompute values if the graph is\nnot a tree. Use `StoreMAP` if the graph is a general DAG instead.\n\n```\n_, args := spn.StoreMAP(S, evidence, t, T)\nmpe := T.Single(t, S)\n```\n\n### Dependencies\n\nGoSPN is written in Go. Go is an open source language originally developed\nat Google. It's a simple yet powerful and fast language built with\nefficiency in mind. Installing Go is easy. Pre-compiled packages are\navailable for FreeBSD, Linux, Mac OS X and Windows for both 32 and\n64-bit processors. For more information see \u003chttps://golang.org/doc/install\u003e.\n\n#### GoNum\n\nWe have deprecated GNU GSL in favor of GoNum (\u003chttps://github.com/gonum/\u003e).\nGoNum is written in Go, meaning when installing GoSPN, the Go package\nmanager should automatically install all dependencies (including GoNum).\n\nIn case this does not occur and something like this comes up on the\nscreen:\n\n```\ncannot find package \"[...]/gonum/stat\" in any of\n```\n\nEnter the following commands:\n\n```\ngo get -u gonum.org/v1/gonum/stat\ngo get -u gonum.org/v1/gonum/mathext\n```\n\nWe have deprecated functions that made GoSPN independent of GoNum or GNU\nGSL, so we recommend installing GoNum.\n\n#### NpyIO\n\nGoSPN supports `.npy` NumPy array dataset. We use\n[NpyIO](https://github.com/sbinet/npyio) to read the file and reformat\ninto GoSPN dataset format. Go's `go get` should automatically install\nNpyIO.\n\n#### graph-tool (optional)\n\nGraph-tool is a Python module for graph manipulation and drawing. Since\nthe SPNs we'll generate with most learning algorithms may have hundreads\nof thousands of nodes and hundreds of layers, we need a fast and\nefficient graph drawing tool for displaying our graphs. Since graph-tool\nuses C++ metaprogramming extensively, its performance is comparable to a\nC++ library.\n\nGraph-tool uses the C++ Boost Library and can be compiled with OpenMP, a\nlibrary for parallel programming on multiple cores architecture that may\ndecrease graph compilation time significantly.\n\nCompiling graph-tool can take up to 80 minutes and 3GB of RAM. If you do\nnot plan on compiling the graphs GoSPN outputs, it is highly recommended\nthat you do not install graph-tool.\n\nSubdependencies and installation instructions are listed at\n\u003chttps://graph-tool.skewed.de/download\u003e.\n\n#### Graphviz (optional)\n\nGoSPN also supports graph drawing with Graphviz. See `io/output.go`.\n\n### Compiling and Running GoSPN\n\nTo get the source code through Go's `go get` command, run the following\ncommand:\n\n```\n$ go get -u github.com/RenatoGeh/gospn\n```\n\nThen ensure all dependencies are pulled:\n\n```\ncd gospn \u0026\u0026 go build\n```\n\n### Updating GoSPN\n\nTo update GoSPN, run:\n\n```\ngo get -u github.com/RenatoGeh/gospn\n```\n\n### Datasets\n\nFor a list of all available datasets in `.data` format, see:\n\n* https://github.com/RenatoGeh/datasets\n\n### Results\n\nSome benchmarking and experiments we did with GoSPN. More can be found\nat https://github.com/renatogeh/benchmarks.\n\n#### Image classifications\n\n![Digits dataset correct\nclassifications](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/digits_percs.png)\n\n![Caltech dataset correct\nclassifications](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/caltech_percs.png)\n\n#### Image completions with prior face knowledge\n\n![Olivetti faces dataset C1 39\ncompletions](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/olivetti_3bit/r1/face_cmpl_39.png)\n\n![Olivetti faces dataset C1 9\ncompletions](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/olivetti_3bit/r1/face_cmpl_9.png)\n\n#### Image completions without prior face knowledge\n\n![Olivetti faces dataset C2 39\ncompletions](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/olivetti_3bit/r2/face_cmpl_39.png)\n\n![Olivetti faces dataset C2 9\ncompletions](https://raw.githubusercontent.com/RenatoGeh/gospn/dev/results/olivetti_3bit/r2/face_cmpl_9.png)\n\n### Literature\n\nThe following articles used GoSPN!\n\n- *Credal Sum-Product Networks*, D. Mauá \u0026 F. Cozman \u0026 D. Conaty \u0026 C.\n  Campos, PMLR 2017\n    * [pdf](http://proceedings.mlr.press/v62/mau%C3%A117a/mau%C3%A117a.pdf)\n- *Approximation Complexity of Maximum A Posteriori Inference in\n  Sum-Product Networks*, D. Conaty \u0026 D. Mauá \u0026 C. Campos, UAI 2017\n    * [pdf](https://arxiv.org/pdf/1703.06045.pdf)\n\n### Acknowledgements\n\nThis project is part of my undergraduate research project supervised by\nProf. [Denis Deratani Mauá](https://www.ime.usp.br/~ddm/) at the\nInstitute of Mathematics and Statistics - University of São Paulo. We\nhad financial support from CNPq grant #800585/2016-0.\n\nWe would like to greatly thank Diarmaid Conaty and Cassio P. de Campos, both\nfrom Queen's University Belfast, for finding and correcting several\nbugs.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenatogeh%2Fgospn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frenatogeh%2Fgospn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frenatogeh%2Fgospn/lists"}