forked from axanderssonuu/istdeco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebook.py
More file actions
42 lines (37 loc) · 1.29 KB
/
codebook.py
File metadata and controls
42 lines (37 loc) · 1.29 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
import numpy as np
import torch
import copy
class Codebook:
'''
Codebook - Helper class for doing arithmetics with the codebook
Parameters
----------
codebook : numpy array
A ndarray of shape (m, rounds, channels)
'''
def __init__(self, codebook):
self.codebook = torch.from_numpy(codebook).float().flatten(start_dim=1).T
self.codebook = self.codebook / self.codebook.sum(axis=0, keepdim=True)
def to(self, device):
'''
Put the tensors on a device.
See PyTorch doc for more info.
'''
self.codebook = self.codebook.to(device)
return self
def matmul(self, tensor):
'''
Multiplies a (m,y,x) shaped tensor with the codebook.
If the codebook is of shape (rc, m), then the output is of shape
(rc,y,x)
'''
m, ny, nx = tensor.shape
return (self.codebook @ tensor.view((m,ny*nx))).view((-1,ny,nx))
def matmul_t(self,tensor):
'''
Multiploes a (rc,y,x) shaped tensor with the codebook.
If the codebook is of shape (rc, m), then the output
is of (m,y,x).
'''
m, ny, nx = tensor.shape
return (self.codebook.t() @ tensor.view((m,ny*nx))).view((-1,ny,nx))