-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmodel.py
54 lines (46 loc) · 1.7 KB
/
model.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
48
49
50
51
52
53
54
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class BaseModel:
def get_weight(self):
weight = []
for param in self.parameters():
weight.append(param.data.numpy().flatten())
weight = np.concatenate(weight, 0)
return weight
def set_weight(self, solution):
offset = 0
for param in self.parameters():
param_shape = param.data.numpy().shape
param_size = np.prod(param_shape)
src_param = solution[offset: offset + param_size]
if len(param_shape) > 1:
src_param = src_param.reshape(param_shape)
param.data = torch.FloatTensor(src_param)
offset += param_size
assert offset == len(solution)
class StandardFCNet(nn.Module, BaseModel):
def __init__(self, state_dim, action_dim, hidden_size):
super(StandardFCNet, self).__init__()
self.fc1 = nn.Linear(state_dim, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, action_dim)
def forward(self, x):
x = Variable(torch.FloatTensor(x))
x = F.tanh(self.fc1(x))
x = F.tanh(self.fc2(x))
x = self.fc3(x)
return x
class SingleHiddenLayerNet(nn.Module, BaseModel):
def __init__(self, state_dim, action_dim):
super(SingleHiddenLayerNet, self).__init__()
hidden_size = 200
self.fc1 = nn.Linear(state_dim, hidden_size)
self.fc2 = nn.Linear(hidden_size, action_dim)
def forward(self, x):
x = Variable(torch.FloatTensor(x))
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x