-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcritics.py
47 lines (39 loc) · 1.35 KB
/
critics.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
47
import torch
import torch.nn as nn
from utils import neuralsortsoft
class REBARCritic(nn.Module):
"""
eta * f(NeuralSort(z, tau))
"""
def __init__(self, f):
super(REBARCritic, self).__init__()
self.f = f
self.eta = nn.Parameter(torch.ones(1))
self.log_tau = nn.Parameter(torch.zeros(1))
def forward(self, z):
assert (z.ndimension() == 1) or (z.ndimension() == 2)
if z.ndimension() == 1: # just one sample
z = z.unsqueeze(0)
z = z.unsqueeze(-1)
tau = torch.exp(self.log_tau)
return self.eta * self.f(neuralsortsoft(z, tau)).squeeze(0).unsqueeze(-1)
class RELAXCritic(nn.Module):
"""
f(NeuralSort(z, tau)) + rho_\phi(z)
"""
def __init__(self, f, d, hidden_dim):
super(RELAXCritic, self).__init__()
self.f = f
self.log_tau = nn.Parameter(torch.zeros(1))
self.rho = nn.Sequential(
nn.Linear(d, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, 1)
)
def forward(self, z):
assert (z.ndimension() == 1) or (z.ndimension() == 2)
if z.ndimension() == 1: # just one sample
z = z.unsqueeze(0)
z = z.unsqueeze(-1)
tau = torch.exp(self.log_tau)
return self.f(neuralsortsoft(z, tau)).squeeze(0).unsqueeze(-1) + self.rho(z.squeeze())