-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpert_trajectories.py
86 lines (67 loc) · 2.56 KB
/
expert_trajectories.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import argparse
import numpy as np
import gym
import torch
import torch.nn as nn
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
torch.manual_seed(0)
if use_cuda:
torch.cuda.manual_seed(0)
class Net(nn.Module):
"""
Actor-Critic Network for PPO
"""
def __init__(self):
super(Net, self).__init__()
# print("input shape", args.img_stack)
self.img_stack = 4
self.cnn_base = nn.Sequential( # input shape (4, 96, 96)
nn.Conv2d(self.img_stack, 8, kernel_size=4, stride=2),
nn.ReLU(), # activation
nn.Conv2d(8, 16, kernel_size=3, stride=2), # (8, 47, 47)
nn.ReLU(), # activation
nn.Conv2d(16, 32, kernel_size=3, stride=2), # (16, 23, 23)
nn.ReLU(), # activation
nn.Conv2d(32, 64, kernel_size=3, stride=2), # (32, 11, 11)
nn.ReLU(), # activation
nn.Conv2d(64, 128, kernel_size=3, stride=1), # (64, 5, 5)
nn.ReLU(), # activation
nn.Conv2d(128, 256, kernel_size=3, stride=1), # (128, 3, 3)
nn.ReLU(), # activation
) # output shape (256, 1, 1)
self.v = nn.Sequential(nn.Linear(256, 100), nn.ReLU(), nn.Linear(100, 1))
self.fc = nn.Sequential(nn.Linear(256, 100), nn.ReLU())
self.alpha_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.beta_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.apply(self._weights_init)
@staticmethod
def _weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu'))
nn.init.constant_(m.bias, 0.1)
def forward(self, x):
x = self.cnn_base(x)
x = x.view(-1, 256)
v = self.v(x)
x = self.fc(x)
alpha = self.alpha_head(x) + 1
beta = self.beta_head(x) + 1
return (alpha, beta), v
class Agent():
"""
Agent for testing
"""
def __init__(self, trainedWeights):
self.net = Net().float().to(device)
self.trainedWeights = trainedWeights
def act(self, state):
state = np.transpose(state, axes=[0, 3, 1, 2])
state = torch.from_numpy(state).float().to(device)
with torch.no_grad():
(alpha, beta), v = self.net(state)
action = alpha / (alpha + beta)
action = action.squeeze().cpu().numpy()
return action
def load_param(self):
self.net.load_state_dict(torch.load(self.trainedWeights, map_location=torch.device('cpu')))