Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated #22

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- '1.7'
- '1.8'
- '1.9'
- 'nightly'
- '1.10'
os:
- ubuntu-latest
arch:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "JointEnergyModels"
uuid = "48c56d24-211d-4463-bbc0-7a701b291131"
authors = ["Patrick Altmeyer"]
version = "0.1.3"
version = "0.1.4"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# `JointEnergyModels.jl`


*Joint Energy Models in Julia.*

[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliatrustworthyai.github.io/JointEnergyModels.jl/stable) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliatrustworthyai.github.io/JointEnergyModels.jl/dev) [![Build Status](https://github.com/juliatrustworthyai/JointEnergyModels.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/juliatrustworthyai/JointEnergyModels.jl/actions/workflows/CI.yml?query=branch%3Amain) [![Coverage](https://codecov.io/gh/juliatrustworthyai/JointEnergyModels.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/juliatrustworthyai/JointEnergyModels.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) [![License](https://img.shields.io/github/license/juliatrustworthyai/JointEnergyModels.jl)](LICENSE) [![Package Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/JointEnergyModels/.png)](https://pkgs.genieframework.com?packages=JointEnergyModels)
Expand Down
2 changes: 1 addition & 1 deletion README.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ crossref:
tbl-prefix: Table
bibliography: bib.bib
output: asis
jupyter: julia-1.8
jupyter: julia-1.10
execute:
freeze: auto
eval: true
Expand Down
4,054 changes: 2,027 additions & 2,027 deletions README_files/figure-commonmark/cell-3-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9,183 changes: 4,608 additions & 4,575 deletions README_files/figure-commonmark/cell-8-output-1.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions _freeze/docs/src/explanation/samplers/execute-results/md.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "577028b23128dbdcef11147ab874511c",
"result": {
"engine": "jupyter",
"markdown": "---\ntitle: MNIST\n---\n\n\n\n\n\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nusing TaijaData: load_mnist\nusing CounterfactualExplanations.Models: load_mnist_mlp, load_mnist_ensemble\n```\n:::\n\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nX, y = load_mnist()\nK = size(y, 1)\nD = size(X, 1)\nmlp = load_mnist_mlp().model\nf_mlp(x) = mlp(x)\nens = load_mnist_ensemble().model\nf_ens(x) = sum(map(mlp -> mlp(x), ens))/length(ens)\nbatch_size = 100\n```\n:::\n\n\n## Sampling\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\n𝒟x = Uniform(0,1)\n𝒟y = Categorical(ones(K) ./ K)\nsampler = UnconditionalSampler(𝒟x; input_size=(D,))\nconditional_sampler = ConditionalSampler(𝒟x, 𝒟y; input_size=(D,))\nopt = ImproperSGLD()\nn_iter = 256\n```\n:::\n\n\n### Conditional Draws\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\n_w = 1500\nplts = []\nneach = 10\nfor i in 1:10\n x = conditional_sampler(f_mlp, opt; niter=n_iter, y=i, n_samples=neach)\n plts_i = []\n for j in 1:size(x,2)\n xj = reshape(x[:,j], (28,28))\n plts_i = [plts_i..., heatmap(rotl90(xj), axis=nothing, cb=false)]\n end\n plt = plot(plts_i..., size=(_w,0.10*_w), layout=(1,10))\n plts = [plts..., plt]\nend\nplot(plts..., size=(_w,_w), layout=(10,1))\n```\n:::\n\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\n_w = 1500\nplts = []\nneach = 10\nfor i in 1:10\n x = conditional_sampler(f_ens, opt; niter=n_iter, y=i, n_samples=neach)\n plts_i = []\n for j in 1:size(x,2)\n xj = reshape(x[:,j], (28,28))\n plts_i = [plts_i..., heatmap(rotl90(xj), axis=nothing, cb=false)]\n end\n plt = plot(plts_i..., size=(_w,0.10*_w), layout=(1,10))\n plts = [plts..., plt]\nend\nplot(plts..., size=(_w,_w), layout=(10,1))\n```\n:::\n\n\n### Unconditional Draws\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\n_w = 1500\nplts = []\nneach = 10\nfor i in 1:10\n x = sampler(f_mlp, opt; niter=n_iter, n_samples=neach)\n plts_i = []\n for j in 1:size(x,2)\n xj = reshape(x[:,j], (28,28))\n plts_i = [plts_i..., heatmap(rotl90(xj), axis=nothing, cb=false)]\n end\n plt = plot(plts_i..., size=(_w,0.10*_w), layout=(1,10))\n plts = [plts..., plt]\nend\nplot(plts..., size=(_w,_w), layout=(10,1))\n```\n:::\n\n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\n_w = 1500\nplts = []\nneach = 10\nfor i in 1:10\n x = sampler(f_ens, opt; niter=n_iter, n_samples=neach)\n plts_i = []\n for j in 1:size(x,2)\n xj = reshape(x[:,j], (28,28))\n plts_i = [plts_i..., heatmap(rotl90(xj), axis=nothing, cb=false)]\n end\n plt = plot(plts_i..., size=(_w,0.10*_w), layout=(1,10))\n plts = [plts..., plt]\nend\nplot(plts..., size=(_w,_w), layout=(10,1))\n```\n:::\n\n\n",
"supporting": [
"samplers_files"
],
"filters": []
}
}
11 changes: 11 additions & 0 deletions _freeze/docs/src/how_to_guides/mlj_flux/execute-results/md.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"hash": "155039eb05e14711352a223bba059fae",
"result": {
"engine": "jupyter",
"markdown": "---\ntitle: Compatibility with `MLJFlux`\n---\n\n\n\n\n\n\n## Synthetic Data\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nnobs=2000\nX, y = make_circles(nobs, noise=0.1, factor=0.5)\nXplot = Float32.(permutedims(matrix(X)))\nX = table(permutedims(Xplot))\ndisplay(scatter(Xplot[1,:], Xplot[2,:], group=y, label=\"\"))\nbatch_size = Int(round(nobs/10))\n```\n:::\n\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\nsampler = ConditionalSampler(X, y, batch_size=batch_size)\nclf = JointEnergyClassifier(\n sampler;\n builder=MLJFlux.MLP(hidden=(32, 32, 32,), σ=Flux.relu),\n batch_size=batch_size,\n finaliser=Flux.softmax,\n loss=Flux.Losses.crossentropy,\n jem_training_params=(α=[1.0,1.0,1e-1],verbosity=10,),\n)\n```\n:::\n\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nprintln(typeof(clf) <: MLJFlux.MLJFluxModel)\n```\n:::\n\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nmach = machine(clf, X, y)\nfit!(mach)\n```\n:::\n\n\n::: {.cell execution_count=6}\n``` {.julia .cell-code}\njem = mach.model.jem\nbatch_size = mach.model.batch_size\nX = Float32.(permutedims(matrix(X)))\ny_labels = Int.(y.refs)\ny = Flux.onehotbatch(y.refs, sort(unique(y_labels)))\n```\n:::\n\n\n::: {.cell execution_count=7}\n``` {.julia .cell-code}\nif typeof(jem.sampler) <: ConditionalSampler\n \n plts = []\n for target in 1:size(y,1)\n X̂ = generate_conditional_samples(jem, batch_size, target; niter=1000) \n ex = extrema(hcat(X,X̂), dims=2)\n xlims = ex[1]\n ylims = ex[2]\n x1 = range(1.0f0.*xlims...,length=100)\n x2 = range(1.0f0.*ylims...,length=100)\n plt = contour(\n x1, x2, (x, y) -> softmax(jem([x, y]))[target], \n fill=true, alpha=0.5, title=\"Target: $target\", cbar=false,\n xlims=xlims,\n ylims=ylims,\n )\n scatter!(X[1,:], X[2,:], color=vec(y_labels), group=vec(y_labels), alpha=0.5)\n scatter!(\n X̂[1,:], X̂[2,:], \n color=repeat([target], size(X̂,2)), \n group=repeat([target], size(X̂,2)), \n shape=:star5, ms=10\n )\n push!(plts, plt)\n end\n plt = plot(plts..., layout=(1, size(y,1)), size=(size(y,1)*400, 400))\n display(plt)\nend\n```\n:::\n\n\n## MNIST \n\n::: {.cell execution_count=8}\n``` {.julia .cell-code}\n# Data:\nnobs = 1000\nn_digits = 28\nXtrain, ytrain, _, _, _, _ = load_mnist_data(nobs=nobs, n_digits=n_digits)\nXtrain = table(permutedims(MLUtils.flatten(Xtrain)))\nytrain = coerce(Flux.onecold(ytrain, 0:9), Multiclass)\n\n# Hyperparameters:\nD = n_digits^2 \nK = 10 \nM = 32\nlr = 1e-3 \nnum_epochs = 500\nmax_patience = 5 \nbatch_size = Int(round(nobs/10))\nα = [1.0,1.0,1e-2]\n```\n:::\n\n\n::: {.cell execution_count=9}\n``` {.julia .cell-code}\nactivation = Flux.swish\nbuilder = MLJFlux.MLP(hidden=(M,M,M,), σ=activation)\n```\n:::\n\n\n::: {.cell execution_count=10}\n``` {.julia .cell-code}\n# We initialize the full model\n𝒟x = Uniform(-1,1)\n𝒟y = Categorical(ones(K) ./ K)\nsampler = ConditionalSampler(𝒟x, 𝒟y, input_size=(D,), batch_size=10)\nclf = JointEnergyClassifier(\n sampler;\n builder=builder,\n batch_size=batch_size,\n finaliser=Flux.softmax,\n loss=Flux.Losses.crossentropy,\n jem_training_params=(α=α,verbosity=10,),\n sampling_steps=20,\n optimiser=Flux.Optimise.Adam(lr),\n)\n```\n:::\n\n\n::: {.cell execution_count=11}\n``` {.julia .cell-code}\nmach = machine(clf, Xtrain, ytrain)\nfit!(mach)\n```\n:::\n\n\n::: {.cell execution_count=12}\n``` {.julia .cell-code}\njem = mach.model.jem\nn_iter = 1000\n_w = 1500\nplts = []\nneach = 10\nfor i in 1:10\n x = jem.sampler(jem.chain, jem.sampling_rule; niter=n_iter, n_samples=neach, y=i)\n plts_i = []\n for j in 1:size(x, 2)\n xj = x[:,j]\n xj = reshape(xj, (n_digits, n_digits))\n plts_i = [plts_i..., heatmap(rotl90(xj), axis=nothing, cb=false)]\n end\n plt = plot(plts_i..., size=(_w,0.10*_w), layout=(1,10))\n plts = [plts..., plt]\nend\nplot(plts..., size=(_w,_w), layout=(10,1))\n```\n:::\n\n\n",
"supporting": [
"mlj_flux_files"
],
"filters": []
}
}
3 changes: 2 additions & 1 deletion _freeze/docs/src/index/execute-results/md.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"hash": "8d26f827b22cadaf45d98344b37a0051",
"result": {
"engine": "jupyter",
"markdown": "```@meta\nCurrentModule = JointEnergyModels\n```\n\n# `JointEnergyModels.jl`\n\nDocumentation for [JointEnergyModels.jl](https://github.com/juliatrustworthyai/JointEnergyModels.jl).\n\n---\nexecute: \n eval: false\n---\n\n*Joint Energy Models in Julia.*\n\n[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://juliatrustworthyai.github.io/JointEnergyModels.jl/stable)\n[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://juliatrustworthyai.github.io/JointEnergyModels.jl/dev)\n[![Build Status](https://github.com/juliatrustworthyai/JointEnergyModels.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/juliatrustworthyai/JointEnergyModels.jl/actions/workflows/CI.yml?query=branch%3Amain)\n[![Coverage](https://codecov.io/gh/juliatrustworthyai/JointEnergyModels.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/juliatrustworthyai/JointEnergyModels.jl)\n[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)\n[![License](https://img.shields.io/github/license/juliatrustworthyai/JointEnergyModels.jl)](LICENSE)\n[![Package Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/JointEnergyModels/)](https://pkgs.genieframework.com?packages=JointEnergyModels)\n\n\n\n`JointEnergyModels.jl` is a package for training Joint Energy Models in Julia. Joint Energy Models (JEM) are hybrid models that learn to discriminate between classes $y$ and generate input data $x$. They were introduced in @grathwohl2020your, which provides the foundation for the methodologies implemented in this package.\n\n## 🔁 Status\n\nThis package is still in its infancy and the API is subject to change. Currently, the package can be used to train JEMs for classification. It is also possible to train pure Energy-Based Models (EBMs) for the generative task only. The package is compatible with `Flux.jl`. Work on compatibility with `MLJ.jl` (through `MLJFlux.jl`) is currently under way.\n\nWe welcome contributions and feedback at this early stage. To install the development version of the package you can run the following command:\n\n```{.julia}\nusing Pkg\nPkg.add(url=\"https://github.com/juliatrustworthyai/JointEnergyModels.jl\")\n```\n\n## 🔍 Usage Example\n\nBelow we first generate some synthetic data:\n\n::: {.cell execution_count=2}\n``` {.julia .cell-code}\nnobs=2000\nX, y = make_circles(nobs, noise=0.1, factor=0.5)\nXplot = Float32.(permutedims(matrix(X)))\nX = table(permutedims(Xplot))\nplt = scatter(Xplot[1,:], Xplot[2,:], group=y, label=\"\")\nbatch_size = Int(round(nobs/10))\ndisplay(plt)\n```\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-commonmark/cell-3-output-1.svg){}\n:::\n:::\n\n\nThe `MLJ` compatible classifier can be instantiated as follows:\n\n::: {.cell execution_count=3}\n``` {.julia .cell-code}\n𝒟x = Normal()\n𝒟y = Categorical(ones(2) ./ 2)\nsampler = ConditionalSampler(𝒟x, 𝒟y, input_size=size(Xplot)[1:end-1], batch_size=batch_size)\nclf = JointEnergyClassifier(\n sampler;\n builder=MLJFlux.MLP(hidden=(32, 32, 32,), σ=Flux.relu),\n batch_size=batch_size,\n finaliser=x -> x,\n loss=Flux.Losses.logitcrossentropy,\n)\n```\n:::\n\n\nIt uses the `MLJFlux` package to build the model:\n\n::: {.cell execution_count=4}\n``` {.julia .cell-code}\nprintln(typeof(clf) <: MLJFlux.MLJFluxModel)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\ntrue\n```\n:::\n:::\n\n\nThe model can be wrapped in data and trained using the `fit!` function:\n\n::: {.cell execution_count=5}\n``` {.julia .cell-code}\nmach = machine(clf, X, y)\nfit!(mach)\n```\n:::\n\n\nThe results are visualised below. The model has learned to discriminate between the two classes (as indicated by the contours) and to generate samples from each class (as indicated by the stars).\n\n\n\n::: {.cell execution_count=7}\n\n::: {.cell-output .cell-output-display}\n![](index_files/figure-commonmark/cell-8-output-1.svg){}\n:::\n:::\n\n\n## 🎓 References\n\n",
"supporting": [
"index_files/figure-commonmark"
"index_files"
],
"filters": []
}
Expand Down
Loading
Loading