-
Notifications
You must be signed in to change notification settings - Fork 16
/
utils.py
95 lines (73 loc) · 3.4 KB
/
utils.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
87
88
89
90
91
92
93
94
95
import numpy as np
import torch
from torch import nn
def soft_update(target, source, tau):
for target_param, param in zip(target.parameters(), source.parameters()):
target_param.data.copy_(target_param.data * (1.0 - tau) + param.data * tau)
def hard_update(target, source):
for target_param, param in zip(target.parameters(), source.parameters()):
target_param.data.copy_(param.data)
# Initialize Policy weights
def weights_init_(m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight, gain=1)
torch.nn.init.constant_(m.bias, 0)
class ExperienceReplayBuffer(object):
def __init__(self, state_dim, action_dim, max_size=int(1e6)):
self.max_size = max_size
self.ptr = 0
self.size = 0
self.state = np.zeros((max_size, state_dim))
self.action = np.zeros((max_size, action_dim))
self.next_state = np.zeros((max_size, state_dim))
self.reward = np.zeros((max_size, 1))
self.not_done = np.zeros((max_size, 1))
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def add(self, state, action, next_state, reward, done):
self.state[self.ptr] = state
self.action[self.ptr] = action
self.next_state[self.ptr] = next_state
self.reward[self.ptr] = reward
self.not_done[self.ptr] = 1. - done
self.ptr = (self.ptr + 1) % self.max_size
self.size = min(self.size + 1, self.max_size)
def sample(self, batch_size):
index = np.random.randint(0, self.size, size=batch_size)
return (
torch.FloatTensor(self.state[index]).to(self.device),
torch.FloatTensor(self.action[index]).to(self.device),
torch.FloatTensor(self.next_state[index]).to(self.device),
torch.FloatTensor(self.reward[index]).to(self.device),
torch.FloatTensor(self.not_done[index]).to(self.device)
)
class BetaExperienceReplayBuffer(object):
def __init__(self, state_dim, action_dim, N, max_size=int(1e6)):
self.max_size = max_size
self.ptr = 0
self.size = 0
self.state = np.zeros((max_size, state_dim))
self.action = np.zeros((max_size, action_dim))
self.beta = np.zeros((max_size, N))
self.next_state = np.zeros((max_size, state_dim))
self.reward = np.zeros((max_size, 1))
self.not_done = np.zeros((max_size, 1))
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def add(self, state, action, beta, next_state, reward, done):
self.state[self.ptr] = state
self.action[self.ptr] = action
self.beta[self.ptr] = beta
self.next_state[self.ptr] = next_state
self.reward[self.ptr] = reward
self.not_done[self.ptr] = 1. - done
self.ptr = (self.ptr + 1) % self.max_size
self.size = min(self.size + 1, self.max_size)
def sample(self, batch_size):
index = np.random.randint(0, self.size, size=batch_size)
return (
torch.FloatTensor(self.state[index]).to(self.device),
torch.FloatTensor(self.action[index]).to(self.device),
torch.FloatTensor(self.beta[index]).to(self.device),
torch.FloatTensor(self.next_state[index]).to(self.device),
torch.FloatTensor(self.reward[index]).to(self.device),
torch.FloatTensor(self.not_done[index]).to(self.device)
)