Skip to content

Commit

Permalink
Deploying to gh-pages from @ 7732bb4 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
liuanji committed Feb 15, 2024
1 parent 4d1ca76 commit 3fb4614
Show file tree
Hide file tree
Showing 68 changed files with 158 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: 91dfa39bc45fef944cf143f45c8d2be4
config: 6f0c17a356e9b39c9908867ce6b2ddbf
tags: 645f666f9bcd5a90fca523b33c5a78b7
Binary file modified .doctrees/environment.pickle
Binary file not shown.
Binary file modified .doctrees/getting-started/tutorials/index.doctree
Binary file not shown.
Binary file modified .doctrees/getting-started/tutorials/train_hmm.doctree
Binary file not shown.
Binary file modified .doctrees/getting-started/tutorials/train_mnist_hclt.doctree
Binary file not shown.
Binary file modified .doctrees/getting-started/tutorials/train_mnist_pd.doctree
Binary file not shown.
Binary file modified .doctrees/getting-started/tutorials/train_mnist_pdhclt.doctree
Binary file not shown.
4 changes: 4 additions & 0 deletions _downloads/0749894295c4ed0d731492395c04895e/train_hmm.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""
Example
=======
dddd
"""

# sphinx_gallery_thumbnail_path = 'imgs/juice.png'

import pyjuice as juice
import torch
import torchvision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Example\n"
"\n# Example\n\naaaa\n"
]
},
{
Expand All @@ -15,7 +15,7 @@
},
"outputs": [],
"source": [
"import pyjuice as juice\nimport torch\nimport torchvision\nimport time\nfrom torch.utils.data import TensorDataset, DataLoader\n\n\ndef evaluate(pc, loader):\n lls_total = 0.0\n for batch in loader:\n x = batch[0].to(pc.device)\n lls = pc(x)\n lls_total += lls.mean().detach().cpu().numpy().item()\n \n lls_total /= len(loader)\n return lls_total\n\n\ndef mini_batch_em_epoch(num_epochs, pc, optimizer, scheduler, train_loader, test_loader, device):\n for epoch in range(num_epochs):\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n optimizer.zero_grad()\n\n lls = pc(x)\n lls.mean().backward()\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n optimizer.step()\n if scheduler is not None:\n scheduler.step()\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n\n print(f\"[Epoch {epoch}/{num_epochs}][train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef full_batch_em_epoch(pc, train_loader, test_loader, device):\n with torch.no_grad():\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n lls = pc(x)\n pc.backward(x, flows_memory = 1.0)\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n pc.mini_batch_em(step_size = 1.0, pseudocount = 0.1)\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n print(f\"[train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef pd_hclt_test():\n\n device = torch.device(\"cuda:0\")\n\n train_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = True, download = True)\n test_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = False, download = True)\n\n train_data = train_dataset.data.reshape(60000, 28*28)\n test_data = test_dataset.data.reshape(10000, 28*28)\n\n num_features = train_data.size(1)\n\n train_loader = DataLoader(\n dataset = TensorDataset(train_data),\n batch_size = 512,\n shuffle = True,\n drop_last = True\n )\n test_loader = DataLoader(\n dataset = TensorDataset(test_data),\n batch_size = 512,\n shuffle = False,\n drop_last = True\n )\n\n ns = juice.structures.PDHCLT(\n train_data.cuda(),\n data_shape = (28, 28),\n num_latents = 128,\n split_intervals = (4, 4),\n structure_type = \"sum_dominated\"\n )\n pc = juice.TensorCircuit(ns)\n\n pc.to(device)\n\n optimizer = juice.optim.CircuitOptimizer(pc, lr = 0.1, pseudocount = 0.1)\n scheduler = juice.optim.CircuitScheduler(\n optimizer, \n method = \"multi_linear\", \n lrs = [0.9, 0.1, 0.05], \n milestone_steps = [0, len(train_loader) * 100, len(train_loader) * 350]\n )\n\n pc.print_statistics()\n\n mini_batch_em_epoch(350, pc, optimizer, None, train_loader, test_loader, device)\n full_batch_em_epoch(pc, train_loader, test_loader, device)\n\n\nif __name__ == \"__main__\":\n torch.manual_seed(2391)\n pd_hclt_test()"
"# sphinx_gallery_thumbnail_path = 'imgs/juice.png'\n\nimport pyjuice as juice\nimport torch\nimport torchvision\nimport time\nfrom torch.utils.data import TensorDataset, DataLoader\n\n\ndef evaluate(pc, loader):\n lls_total = 0.0\n for batch in loader:\n x = batch[0].to(pc.device)\n lls = pc(x)\n lls_total += lls.mean().detach().cpu().numpy().item()\n \n lls_total /= len(loader)\n return lls_total\n\n\ndef mini_batch_em_epoch(num_epochs, pc, optimizer, scheduler, train_loader, test_loader, device):\n for epoch in range(num_epochs):\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n optimizer.zero_grad()\n\n lls = pc(x)\n lls.mean().backward()\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n optimizer.step()\n if scheduler is not None:\n scheduler.step()\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n\n print(f\"[Epoch {epoch}/{num_epochs}][train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef full_batch_em_epoch(pc, train_loader, test_loader, device):\n with torch.no_grad():\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n lls = pc(x)\n pc.backward(x, flows_memory = 1.0)\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n pc.mini_batch_em(step_size = 1.0, pseudocount = 0.1)\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n print(f\"[train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef pd_hclt_test():\n\n device = torch.device(\"cuda:0\")\n\n train_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = True, download = True)\n test_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = False, download = True)\n\n train_data = train_dataset.data.reshape(60000, 28*28)\n test_data = test_dataset.data.reshape(10000, 28*28)\n\n num_features = train_data.size(1)\n\n train_loader = DataLoader(\n dataset = TensorDataset(train_data),\n batch_size = 512,\n shuffle = True,\n drop_last = True\n )\n test_loader = DataLoader(\n dataset = TensorDataset(test_data),\n batch_size = 512,\n shuffle = False,\n drop_last = True\n )\n\n ns = juice.structures.PDHCLT(\n train_data.cuda(),\n data_shape = (28, 28),\n num_latents = 128,\n split_intervals = (4, 4),\n structure_type = \"sum_dominated\"\n )\n pc = juice.TensorCircuit(ns)\n\n pc.to(device)\n\n optimizer = juice.optim.CircuitOptimizer(pc, lr = 0.1, pseudocount = 0.1)\n scheduler = juice.optim.CircuitScheduler(\n optimizer, \n method = \"multi_linear\", \n lrs = [0.9, 0.1, 0.05], \n milestone_steps = [0, len(train_loader) * 100, len(train_loader) * 350]\n )\n\n pc.print_statistics()\n\n mini_batch_em_epoch(350, pc, optimizer, None, train_loader, test_loader, device)\n full_batch_em_epoch(pc, train_loader, test_loader, device)\n\n\nif __name__ == \"__main__\":\n torch.manual_seed(2391)\n pd_hclt_test()"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"\n# Example\n"
"\n# Example\n\nbbbb\n"
]
},
{
Expand All @@ -15,7 +15,7 @@
},
"outputs": [],
"source": [
"import pyjuice as juice\nimport torch\nimport torchvision\nimport time\nfrom torch.utils.data import TensorDataset, DataLoader\n\n\ndef evaluate(pc, loader):\n lls_total = 0.0\n for batch in loader:\n x = batch[0].to(pc.device)\n lls = pc(x)\n lls_total += lls.mean().detach().cpu().numpy().item()\n \n lls_total /= len(loader)\n return lls_total\n\n\ndef mini_batch_em_epoch(num_epochs, pc, optimizer, scheduler, train_loader, test_loader, device):\n for epoch in range(num_epochs):\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n optimizer.zero_grad()\n\n lls = pc(x)\n lls.mean().backward()\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n optimizer.step()\n if scheduler is not None:\n scheduler.step()\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n\n print(f\"[Epoch {epoch}/{num_epochs}][train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef full_batch_em_epoch(pc, train_loader, test_loader, device):\n with torch.no_grad():\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n lls = pc(x)\n pc.backward(x, flows_memory = 1.0)\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n pc.mini_batch_em(step_size = 1.0, pseudocount = 0.1)\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n print(f\"[train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef train_mnist_pd():\n\n device = torch.device(\"cuda:0\")\n\n train_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = True, download = True)\n test_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = False, download = True)\n\n train_data = train_dataset.data.reshape(60000, 28*28)\n test_data = test_dataset.data.reshape(10000, 28*28)\n\n num_features = train_data.size(1)\n\n train_loader = DataLoader(\n dataset = TensorDataset(train_data),\n batch_size = 512,\n shuffle = True,\n drop_last = True\n )\n test_loader = DataLoader(\n dataset = TensorDataset(test_data),\n batch_size = 512,\n shuffle = False,\n drop_last = True\n )\n\n ns = juice.structures.PD(\n data_shape = (28, 28),\n num_latents = 512,\n split_intervals = (4, 4),\n structure_type = \"sum_dominated\"\n )\n pc = juice.TensorCircuit(ns)\n pc.print_statistics()\n\n pc.to(device)\n\n optimizer = juice.optim.CircuitOptimizer(pc, lr = 0.1, pseudocount = 0.0001)\n\n mini_batch_em_epoch(350, pc, optimizer, None, train_loader, test_loader, device)\n full_batch_em_epoch(pc, train_loader, test_loader, device)\n\n\nif __name__ == \"__main__\":\n train_mnist_pd()"
"# sphinx_gallery_thumbnail_path = 'imgs/juice.png'\n\nimport pyjuice as juice\nimport torch\nimport torchvision\nimport time\nfrom torch.utils.data import TensorDataset, DataLoader\n\n\ndef evaluate(pc, loader):\n lls_total = 0.0\n for batch in loader:\n x = batch[0].to(pc.device)\n lls = pc(x)\n lls_total += lls.mean().detach().cpu().numpy().item()\n \n lls_total /= len(loader)\n return lls_total\n\n\ndef mini_batch_em_epoch(num_epochs, pc, optimizer, scheduler, train_loader, test_loader, device):\n for epoch in range(num_epochs):\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n optimizer.zero_grad()\n\n lls = pc(x)\n lls.mean().backward()\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n optimizer.step()\n if scheduler is not None:\n scheduler.step()\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n\n print(f\"[Epoch {epoch}/{num_epochs}][train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef full_batch_em_epoch(pc, train_loader, test_loader, device):\n with torch.no_grad():\n t0 = time.time()\n train_ll = 0.0\n for batch in train_loader:\n x = batch[0].to(device)\n\n lls = pc(x)\n pc.backward(x, flows_memory = 1.0)\n\n train_ll += lls.mean().detach().cpu().numpy().item()\n\n pc.mini_batch_em(step_size = 1.0, pseudocount = 0.1)\n\n train_ll /= len(train_loader)\n\n t1 = time.time()\n test_ll = evaluate(pc, loader=test_loader)\n t2 = time.time()\n print(f\"[train LL: {train_ll:.2f}; test LL: {test_ll:.2f}].....[train forward+backward+step {t1-t0:.2f}; test forward {t2-t1:.2f}] \")\n\n\ndef train_mnist_pd():\n\n device = torch.device(\"cuda:0\")\n\n train_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = True, download = True)\n test_dataset = torchvision.datasets.MNIST(root = \"./examples/data\", train = False, download = True)\n\n train_data = train_dataset.data.reshape(60000, 28*28)\n test_data = test_dataset.data.reshape(10000, 28*28)\n\n num_features = train_data.size(1)\n\n train_loader = DataLoader(\n dataset = TensorDataset(train_data),\n batch_size = 512,\n shuffle = True,\n drop_last = True\n )\n test_loader = DataLoader(\n dataset = TensorDataset(test_data),\n batch_size = 512,\n shuffle = False,\n drop_last = True\n )\n\n ns = juice.structures.PD(\n data_shape = (28, 28),\n num_latents = 512,\n split_intervals = (4, 4),\n structure_type = \"sum_dominated\"\n )\n pc = juice.TensorCircuit(ns)\n pc.print_statistics()\n\n pc.to(device)\n\n optimizer = juice.optim.CircuitOptimizer(pc, lr = 0.1, pseudocount = 0.0001)\n\n mini_batch_em_epoch(350, pc, optimizer, None, train_loader, test_loader, device)\n full_batch_em_epoch(pc, train_loader, test_loader, device)\n\n\nif __name__ == \"__main__\":\n train_mnist_pd()"
]
}
],
Expand Down
Loading

0 comments on commit 3fb4614

Please sign in to comment.