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

Pytorch refactor #168

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
853b65d
DRAFT: first draft of model
Keegan-Evans Apr 12, 2022
c0a07e6
REFACTOR: beginning of refactor
Keegan-Evans Apr 15, 2022
d57f4c0
FEAT: forward likelihood now sum(lu, lv, ly)
Keegan-Evans Apr 18, 2022
4a29a12
DEBUG: first pass package refactoring
Keegan-Evans Apr 19, 2022
14e2b13
IMP: split ILR and ALR, ALR done to ranks
Keegan-Evans Apr 27, 2022
b6778c1
IMP: ALR outputs working
Keegan-Evans Apr 27, 2022
fcc066b
FEAT: function for ordination created
Keegan-Evans Apr 29, 2022
dc3eaa9
FEAT: Produces OrdinationResults
Keegan-Evans Apr 30, 2022
449f4a2
IMP: cleanup before working on tests.
Keegan-Evans May 2, 2022
5946e33
TEST: test_multimodal runs but fails.
Keegan-Evans May 3, 2022
b32d5f1
checkpoint before cleanup for pr
Keegan-Evans May 5, 2022
11e65ff
IMP: cleanup refactor examples directory
Keegan-Evans May 5, 2022
b45f04e
IMP: remove sneaky notebook checkpoints
Keegan-Evans May 5, 2022
26377ff
IMP: restore original mmvec/multimodal.py
Keegan-Evans May 5, 2022
5cf2dcc
IMP: restore q2 goodies
Keegan-Evans May 5, 2022
15bfa63
IMP: q2 goodies
Keegan-Evans May 5, 2022
e7c7f89
IMP: add min pytorch version
Keegan-Evans May 5, 2022
edc554a
DEBUG: conda-> pip pytorch install name
Keegan-Evans May 5, 2022
5308aa1
IMP: name tweaks
Keegan-Evans May 5, 2022
6d75cd1
IMP: var renaming and observation based epochs
Keegan-Evans May 6, 2022
b38a1d2
FEAT: plugin-standup
Keegan-Evans May 19, 2022
55d1c66
Merge branch 'pytorch-refactor' of github.com:Keegan-Evans/mmvec into…
Keegan-Evans May 19, 2022
ae8c948
BUG: getting test interface to work after plugin
Keegan-Evans May 19, 2022
88d5dbc
IMP: q2 paired-omics method wired up
Keegan-Evans May 25, 2022
aa967e4
BUG: fixing index filtering on biom-tables
Keegan-Evans May 25, 2022
a26f56e
BUG: index dfs based on intersection of indexes
Keegan-Evans May 25, 2022
9d4d26e
BUG: paired-omics working now.
Keegan-Evans Jun 1, 2022
da1bdb8
TEST: adding tests for ranks_bare
Keegan-Evans Jun 3, 2022
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
378 changes: 378 additions & 0 deletions examples/refactor/ALR.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,378 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "461ff352",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e536cc44",
"metadata": {},
"outputs": [],
"source": [
"%autoreload 1"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "acadee5d",
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"<torch._C.Generator at 0x7ff5805ca0f0>"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import biom\n",
"import torch\n",
"import torch.nn as nn\n",
"%aimport mmvec.ALR\n",
"\n",
"import numpy as np\n",
"\n",
"torch.manual_seed(15)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "14d8bfab",
"metadata": {},
"outputs": [],
"source": [
"microbes = biom.load_table(\"./soil_microbes.biom\")\n",
"metabolites = biom.load_table(\"./soil_metabolites.biom\")\n",
"\n",
"model = mmvec.ALR.MMvecALR(microbes, metabolites, 15, sigma_u=1, sigma_v=1, holdout_num=5)\n",
"\n",
"microbes = microbes.to_dataframe().T\n",
"metabolites = metabolites.to_dataframe().T\n",
"microbes = microbes.loc[metabolites.index]\n",
"\n",
"microbe_idx = microbes.columns\n",
"metabolite_idx = metabolites.columns\n",
"microbes = torch.tensor(microbes.values, dtype=torch.int)\n",
"metabolites = torch.tensor(metabolites.values, dtype=torch.int64)\n",
"\n",
"microbe_relative_frequency = (microbes.T/microbes.sum(1)).T\n",
"\n",
"microbe_count = microbes.shape[1]\n",
"\n",
"metabolite_count = metabolites.shape[1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9003e38d",
"metadata": {},
"outputs": [],
"source": [
"#model = mmvec.ALR.MMvecALR(microbe_count, metabolite_count, 15, sigma_u=1, sigma_v=1)\n",
"learning_rate = 1e-3\n",
"batch_size = 500\n",
"epochs = 3000\n",
"optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, maximize=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29799ea5",
"metadata": {},
"outputs": [],
"source": [
"model.microbe_relative_freq(model.microbes_train)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b977e212",
"metadata": {},
"outputs": [],
"source": [
"maybe = mmvec.train.mmvec_training_loop(\n",
" model=model,\n",
"\n",
" batch_size=batch_size,\n",
" epochs=epochs,\n",
" learning_rate=learning_rate,\n",
" summary_interval = 25)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "30db58c0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<generator object mmvec_training_loop at 0x7ff58099d900>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_stats = pd.Dataframe.from_records(maybe, )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2eb531fe",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "1f27301f",
"metadata": {},
"outputs": [],
"source": [
"model.microbe_relative_freq(model.microbes_train)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8a16a24a",
"metadata": {},
"outputs": [],
"source": [
"l"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3663ecb4",
"metadata": {},
"outputs": [],
"source": [
"# h = model.ranks_df - model.ranks_df.mean(axis=0)\n",
"h = model.ranks_matrix\n",
"\n",
"k = model.latent_dim"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b3bcb21",
"metadata": {},
"outputs": [],
"source": [
"h.mean(dim=0).shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "52e80094",
"metadata": {},
"outputs": [],
"source": [
"from torch.linalg import svd\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b24646d3",
"metadata": {},
"outputs": [],
"source": [
"u, s, v = svd(h, full_matrices=False)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afc2f855",
"metadata": {},
"outputs": [],
"source": [
"u.shape, s.shape, v.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5b427a72",
"metadata": {},
"outputs": [],
"source": [
"s"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "752acb7a",
"metadata": {},
"outputs": [],
"source": [
"model.U"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18824f01",
"metadata": {},
"outputs": [],
"source": [
"model.get_ordination()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5f97b6d",
"metadata": {},
"outputs": [],
"source": [
"ranks = pd.DataFrame(model.ranks(),\n",
" index=microbe_idx,\n",
" columns=metabolite_idx)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "99a455e5",
"metadata": {},
"outputs": [],
"source": [
"ranks"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe702e51",
"metadata": {},
"outputs": [],
"source": [
"for param in model.parameters():\n",
" print(param.shape)\n",
"\n",
"print(model.parameters)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "76a6f81b",
"metadata": {},
"outputs": [],
"source": [
"a = torch.randint(10, (9, 3), dtype=torch.float)\n",
"b = torch.randint(10, (3,))\n",
"a, a - a.mean(dim=0), a.mean(dim=0)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "aa9e8f31",
"metadata": {},
"outputs": [],
"source": [
"bp = model.get_ordination()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6b850001",
"metadata": {},
"outputs": [],
"source": [
"import os.path"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e3b2153",
"metadata": {},
"outputs": [],
"source": [
"bp_path = os.path.join(\"/Users/keeganevans/Desktop/\", \"biplot\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b18f859b",
"metadata": {},
"outputs": [],
"source": [
"bp.write(bp_path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "34569491",
"metadata": {},
"outputs": [],
"source": [
"model.ranks_df"
]
},
{
"cell_type": "markdown",
"id": "46d440f7",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added examples/refactor/soil_metabolites.biom
Binary file not shown.
Binary file added examples/refactor/soil_microbes.biom
Binary file not shown.
Loading