-
Notifications
You must be signed in to change notification settings - Fork 81
/
v_function.py
34 lines (26 loc) · 1008 Bytes
/
v_function.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
import chainer
from chainer import functions as F
from chainer import links as L
class VFunction(object):
pass
class FCVFunction(chainer.ChainList, VFunction):
def __init__(self, n_input_channels, n_hidden_layers=0,
n_hidden_channels=None):
self.n_input_channels = n_input_channels
self.n_hidden_layers = n_hidden_layers
self.n_hidden_channels = n_hidden_channels
layers = []
if n_hidden_layers > 0:
layers.append(L.Linear(n_input_channels, n_hidden_channels))
for i in range(n_hidden_layers - 1):
layers.append(L.Linear(n_hidden_channels, n_hidden_channels))
layers.append(L.Linear(n_hidden_channels, 1))
else:
layers.append(L.Linear(n_input_channels, 1))
super(FCVFunction, self).__init__(*layers)
def __call__(self, state):
h = state
for layer in self[:-1]:
h = F.relu(layer(h))
h = self[-1](h)
return h