-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_emb.py
executable file
·46 lines (40 loc) · 1.49 KB
/
gen_emb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import os
from utils import get_network
from collections import namedtuple
import numpy as np
import torch
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'-dataset', type=str, required=True,
help='the dataset you want to work on')
args = parser.parse_args()
train_X = torch.Tensor(np.load(f'data/{args.dataset}/train_X.npy'))
train_Y = torch.Tensor(np.load(f'data/{args.dataset}/train_Y.npy'))
test_X = torch.Tensor(np.load(f'data/{args.dataset}/test_X.npy'))
test_Y = torch.Tensor(np.load(f'data/{args.dataset}/test_Y.npy'))
train_emb, test_emb = [], []
for net_ in os.listdir(f'networks/{args.dataset}'):
if net_.startswith('.'):
# ignore the .gitignore file.
continue
net_args = namedtuple('args', ['dataset', 'net'])
net_args.net = net_.strip('.pth')
net_args.gpu = False
net_args.dataset = args.dataset
net = get_network(net_args)
net.load_state_dict(torch.load(
f'networks/{args.dataset}/{net_}', map_location=torch.device('cpu')))
net.eval()
with torch.no_grad():
net(train_X)
train_emb.append(net.z.detach().numpy())
with torch.no_grad():
net(test_X)
test_emb.append(net.z.detach().numpy())
train_emb = np.stack(train_emb, axis=2)
test_emb = np.stack(test_emb, axis=2)
np.save(f'emb/{args.dataset}/train_X.npy', train_emb)
np.save(f'emb/{args.dataset}/train_Y.npy', train_Y)
np.save(f'emb/{args.dataset}/test_X.npy', test_emb)
np.save(f'emb/{args.dataset}/test_Y.npy', test_Y)