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

update cross-training #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
17 changes: 2 additions & 15 deletions src/algorithms/ETNA.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
# the global structure of the graph and use cross training function
# to align two embeddings

from collections import defaultdict

import algorithms.helper as helper
import networkx as nx
import numpy as np
Expand Down Expand Up @@ -570,12 +568,6 @@ def cross_training(self, seed=None):
self.emb_trainer2.emb_model.train()
orthologs = list(self.orthologs)

org1_ortholog_dict = defaultdict(int)
org2_ortholog_dict = defaultdict(int)
for i, j in orthologs:
org1_ortholog_dict[i] += 1
org2_ortholog_dict[j] += 1

if seed:
np.random.seed(seed)
np.random.shuffle(orthologs)
Expand All @@ -589,11 +581,6 @@ def cross_training(self, seed=None):
epoch_x = anchor_x[idx]
epoch_y = anchor_y[idx]

org1_weight = torch.from_numpy(
np.array([1 / org1_ortholog_dict[x] for x in epoch_x])).to(self.device)
org2_weight = torch.from_numpy(
np.array([1 / org2_ortholog_dict[x] for x in epoch_y])).to(self.device)

self.optimizer_align.zero_grad()
X1 = self.emb_trainer1.normalized_matrix[epoch_x].to(self.device)
X2 = self.emb_trainer2.normalized_matrix[epoch_y].to(self.device)
Expand All @@ -603,14 +590,14 @@ def cross_training(self, seed=None):

loss1 = F.binary_cross_entropy_with_logits(
recon1, X2, reduction='none')
loss1 = torch.mean(torch.sum(loss1, dim=1) * org1_weight)
loss1 = torch.mean(torch.sum(loss1, dim=1))

emb2 = self.emb_trainer2.emb_model.encoder(X2)
recon2 = self.emb_trainer1.emb_model.decoder(emb2)

loss2 = F.binary_cross_entropy_with_logits(
recon2, X1, reduction='none')
loss2 = torch.mean(torch.sum(loss2, dim=1) * org2_weight)
loss2 = torch.mean(torch.sum(loss2, dim=1))

loss = self.psi * (loss1 + loss2)

Expand Down
44 changes: 17 additions & 27 deletions src/demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -161,31 +161,14 @@
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<module 'algorithms.ETNA' from '/home/ETNA/src/algorithms/ETNA.py'>"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"importlib.reload(etna)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [""]
"text": [
"/Users/llc/ylab/proj-net-embed/src/algorithms/ETNA.py:101: UserWarning: nn.init.xavier_uniform is now deprecated in favor of nn.init.xavier_uniform_.\n",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should make sure to scrub all absolute file paths from the repo

" torch.nn.init.xavier_uniform(m.weight)\n"
]
}
],
"source": [
Expand All @@ -198,14 +181,14 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.724578124149233 0.1828194242946759\n"
"0.7216661736014969 0.1817707376284373\n"
]
}
],
Expand All @@ -221,11 +204,18 @@
" test_matrix, np.ones(test_matrix.shape))\n",
"print(auroc, auprc)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -239,7 +229,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.2"
"version": "3.7.7"
}
},
"nbformat": 4,
Expand Down