-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
121 lines (101 loc) · 5.86 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import random
import pickle
import torch
import numpy as np
from torch.utils.data import Dataset
import pdb
class Dataset(Dataset):
def __init__(self, dataset_path, context_len,online_training):
self.online_buffer_size=5000
self.context_len = context_len
# load dataset
with open(r'/home/data_2/why_22/code/GODA//yuce/halfcheetah_medium-v2.pkl', 'rb') as f:
self.trajectories = pickle.load(f)
self.trajectories = self.trajectories[0:800]
self.states, self.actions, self.rewards, self.traj_lens = [], [], [], []
for path in self.trajectories:
self.states.append(path['observations'])
self.actions.append(path['actions'])
self.rewards.append(path['rewards'])
self.traj_lens.append(len(path['observations']))
#normalization
self.states = np.concatenate(self.states, axis=0)
self.actions = np.concatenate(self.actions, axis=0)
self.rewards = np.concatenate(self.rewards, axis=0)
self.states_mean, self.states_std = np.mean(self.states, axis=0), np.std(self.states, axis=0) + 1e-6
self.actions_mean, self.actions_std = np.mean(self.actions, axis=0), np.std(self.actions, axis=0) + 1e-6
self.rewards_mean, self.rewards_std = np.mean(self.rewards, axis=0), np.std(self.rewards, axis=0) + 1e-6
for traj in self.trajectories:
traj['observations'] = (traj['observations'] - self.states_mean) / self.states_std
traj['actions'] = (traj['actions'] - self.actions_mean) / self.actions_std
traj['rewards'] = (traj['rewards'] - self.rewards_mean) / self.rewards_std
num_timesteps = sum(self.traj_lens)
print('=' * 50)
print(f'Starting new experiment: {dataset_path}')
print(f'{len(self.traj_lens)} trajectories, {num_timesteps} timesteps found')
print(f'Average return: {np.mean(self.rewards):.2f}, std: {np.std(self.rewards):.2f}')
print(f'Max return: {np.max(self.rewards):.2f}, min: {np.min(self.rewards):.2f}')
print('=' * 50)
def __len__(self):
return len(self.trajectories)
def __getitem__(self, idx):
traj = self.trajectories[idx]
traj_len = traj['observations'].shape[0]
if traj_len >= self.context_len:
# sample random index to slice trajectory
si = random.randint(0, traj_len - self.context_len)
states = torch.from_numpy(traj['observations'][si : si + self.context_len])
actions = torch.from_numpy(traj['actions'][si : si + self.context_len])
rewards = torch.from_numpy(traj['rewards'][si : si + self.context_len])
# timesteps = torch.arange(start=si, end=si+self.context_len, step=1)
# all ones since no padding
return states.to(torch.float32), actions.to(torch.float32), rewards.to(torch.float32)
class TESTDataset(Dataset):
def __init__(self, dataset_path, context_len,online_training):
self.online_buffer_size=5000
self.context_len = context_len
# load dataset
with open(r'/home/data_2/why_22/code/GODA//yuce/halfcheetah_medium-v2.pkl', 'rb') as f:
self.trajectories = pickle.load(f)
self.trajectories = self.trajectories[801:1000]
self.states, self.actions, self.rewards, self.traj_lens = [], [], [], []
for path in self.trajectories:
self.states.append(path['observations'])
self.actions.append(path['actions'])
self.rewards.append(path['rewards'])
self.traj_lens.append(len(path['observations']))
#normalization
self.states = np.concatenate(self.states, axis=0)
self.actions = np.concatenate(self.actions, axis=0)
self.rewards = np.concatenate(self.rewards, axis=0)
self.states_mean, self.states_std = np.mean(self.states, axis=0), np.std(self.states, axis=0) + 1e-6
self.actions_mean, self.actions_std = np.mean(self.actions, axis=0), np.std(self.actions, axis=0) + 1e-6
self.rewards_mean, self.rewards_std = np.mean(self.rewards, axis=0), np.std(self.rewards, axis=0) + 1e-6
print(f'test:statesmean and statesstd={self.states_mean} and {self.states_std}'+'\n')
print(f'test:actions_mean and statesstd={self.actions_mean} and {self.actions_std}'+'\n')
print(f'test:rewards_mean and statesstd={self.rewards_mean} and {self.rewards_std}'+'\n')
for traj in self.trajectories:
traj['observations'] = (traj['observations'] - self.states_mean) / self.states_std
traj['actions'] = (traj['actions'] - self.actions_mean) / self.actions_std
traj['rewards'] = (traj['rewards'] - self.rewards_mean) / self.rewards_std
num_timesteps = sum(self.traj_lens)
print('=' * 50)
print(f'Starting new experiment: {dataset_path}')
print(f'{len(self.traj_lens)} trajectories, {num_timesteps} timesteps found')
print(f'Average return: {np.mean(self.rewards):.2f}, std: {np.std(self.rewards):.2f}')
print(f'Max return: {np.max(self.rewards):.2f}, min: {np.min(self.rewards):.2f}')
print('=' * 50)
def __len__(self):
return len(self.trajectories)
def __getitem__(self, idx):
traj = self.trajectories[idx]
traj_len = traj['observations'].shape[0]
if traj_len >= self.context_len:
# sample random index to slice trajectory
si = random.randint(0, traj_len - self.context_len)
states = torch.from_numpy(traj['observations'][si : si + self.context_len])
actions = torch.from_numpy(traj['actions'][si : si + self.context_len])
rewards = torch.from_numpy(traj['rewards'][si : si + self.context_len])
# timesteps = torch.arange(start=si, end=si+self.context_len, step=1)
# all ones since no padding
return states.to(torch.float32), actions.to(torch.float32), rewards.to(torch.float32)