-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (31 loc) · 1.06 KB
/
Copy pathutils.py
File metadata and controls
46 lines (31 loc) · 1.06 KB
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
from __future__ import division
import math
import numpy as np
import torch
from torch.autograd import Variable
def batch_slices(n_samples, batch_size=32):
n_batches = math.ceil(n_samples / batch_size)
batches = [slice(ix * batch_size, (ix + 1) * batch_size)
for ix in range(n_batches)]
return batches
def S_from_Ainv(Ainv):
"""See footnote in notes.pdf"""
# Ainv = torch.FloatTensor(Ainv).view(1 + n_active, 1 + n_active)
S = Ainv[1:, 1:]
k = Ainv[0, 0]
b = Ainv[0, 1:].unsqueeze(0)
S -= (1 / k) * (b * b.t())
return S
def expand_with_zeros(x, rows, cols):
orig_rows, orig_cols = x.size()
ret = x
if orig_cols < cols:
horiz = Variable(x.data.new(orig_rows, cols - orig_cols).zero_())
ret = torch.cat([ret, horiz], dim=-1)
if orig_rows < rows:
vert = Variable(x.data.new(rows - orig_rows, cols).zero_())
ret = torch.cat([ret, vert], dim=0)
return ret
def zeros_like(torch_var):
data = torch_var.data.new(torch_var.size()).zero_()
return Variable(data)