gr-libs 0.1.7.post0__py3-none-any.whl → 0.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- gr_libs/__init__.py +4 -1
- gr_libs/_evaluation/__init__.py +1 -0
- gr_libs/_evaluation/_analyze_results_cross_alg_cross_domain.py +260 -0
- gr_libs/_evaluation/_generate_experiments_results.py +141 -0
- gr_libs/_evaluation/_generate_task_specific_statistics_plots.py +497 -0
- gr_libs/_evaluation/_get_plans_images.py +61 -0
- gr_libs/_evaluation/_increasing_and_decreasing_.py +106 -0
- gr_libs/_version.py +2 -2
- gr_libs/all_experiments.py +294 -0
- gr_libs/environment/__init__.py +30 -9
- gr_libs/environment/_utils/utils.py +27 -0
- gr_libs/environment/environment.py +417 -54
- gr_libs/metrics/__init__.py +7 -0
- gr_libs/metrics/metrics.py +231 -54
- gr_libs/ml/__init__.py +2 -5
- gr_libs/ml/agent.py +21 -6
- gr_libs/ml/base/__init__.py +3 -1
- gr_libs/ml/base/rl_agent.py +81 -13
- gr_libs/ml/consts.py +1 -1
- gr_libs/ml/neural/__init__.py +1 -3
- gr_libs/ml/neural/deep_rl_learner.py +619 -378
- gr_libs/ml/neural/utils/__init__.py +1 -2
- gr_libs/ml/neural/utils/dictlist.py +3 -3
- gr_libs/ml/planner/mcts/{utils → _utils}/__init__.py +1 -1
- gr_libs/ml/planner/mcts/{utils → _utils}/node.py +11 -7
- gr_libs/ml/planner/mcts/{utils → _utils}/tree.py +15 -11
- gr_libs/ml/planner/mcts/mcts_model.py +571 -312
- gr_libs/ml/sequential/__init__.py +0 -1
- gr_libs/ml/sequential/_lstm_model.py +270 -0
- gr_libs/ml/tabular/__init__.py +1 -3
- gr_libs/ml/tabular/state.py +7 -7
- gr_libs/ml/tabular/tabular_q_learner.py +150 -82
- gr_libs/ml/tabular/tabular_rl_agent.py +42 -28
- gr_libs/ml/utils/__init__.py +2 -3
- gr_libs/ml/utils/format.py +28 -97
- gr_libs/ml/utils/math.py +5 -3
- gr_libs/ml/utils/other.py +3 -3
- gr_libs/ml/utils/storage.py +88 -81
- gr_libs/odgr_executor.py +268 -0
- gr_libs/problems/consts.py +1549 -1227
- gr_libs/recognizer/_utils/__init__.py +0 -0
- gr_libs/recognizer/_utils/format.py +18 -0
- gr_libs/recognizer/gr_as_rl/gr_as_rl_recognizer.py +233 -88
- gr_libs/recognizer/graml/_gr_dataset.py +233 -0
- gr_libs/recognizer/graml/graml_recognizer.py +586 -252
- gr_libs/recognizer/recognizer.py +90 -30
- gr_libs/tutorials/draco_panda_tutorial.py +58 -0
- gr_libs/tutorials/draco_parking_tutorial.py +56 -0
- gr_libs/tutorials/gcdraco_panda_tutorial.py +62 -0
- gr_libs/tutorials/gcdraco_parking_tutorial.py +57 -0
- gr_libs/tutorials/graml_minigrid_tutorial.py +64 -0
- gr_libs/tutorials/graml_panda_tutorial.py +57 -0
- gr_libs/tutorials/graml_parking_tutorial.py +52 -0
- gr_libs/tutorials/graml_point_maze_tutorial.py +60 -0
- gr_libs/tutorials/graql_minigrid_tutorial.py +50 -0
- {gr_libs-0.1.7.post0.dist-info → gr_libs-0.2.2.dist-info}/METADATA +84 -29
- gr_libs-0.2.2.dist-info/RECORD +71 -0
- {gr_libs-0.1.7.post0.dist-info → gr_libs-0.2.2.dist-info}/WHEEL +1 -1
- gr_libs-0.2.2.dist-info/top_level.txt +2 -0
- tests/test_draco.py +14 -0
- tests/test_gcdraco.py +10 -0
- tests/test_graml.py +12 -8
- tests/test_graql.py +3 -2
- evaluation/analyze_results_cross_alg_cross_domain.py +0 -277
- evaluation/create_minigrid_map_image.py +0 -34
- evaluation/file_system.py +0 -42
- evaluation/generate_experiments_results.py +0 -92
- evaluation/generate_experiments_results_new_ver1.py +0 -254
- evaluation/generate_experiments_results_new_ver2.py +0 -331
- evaluation/generate_task_specific_statistics_plots.py +0 -272
- evaluation/get_plans_images.py +0 -47
- evaluation/increasing_and_decreasing_.py +0 -63
- gr_libs/environment/utils/utils.py +0 -17
- gr_libs/ml/neural/utils/penv.py +0 -57
- gr_libs/ml/sequential/lstm_model.py +0 -192
- gr_libs/recognizer/graml/gr_dataset.py +0 -134
- gr_libs/recognizer/utils/__init__.py +0 -1
- gr_libs/recognizer/utils/format.py +0 -13
- gr_libs-0.1.7.post0.dist-info/RECORD +0 -67
- gr_libs-0.1.7.post0.dist-info/top_level.txt +0 -4
- tutorials/graml_minigrid_tutorial.py +0 -34
- tutorials/graml_panda_tutorial.py +0 -41
- tutorials/graml_parking_tutorial.py +0 -39
- tutorials/graml_point_maze_tutorial.py +0 -39
- tutorials/graql_minigrid_tutorial.py +0 -34
- /gr_libs/environment/{utils → _utils}/__init__.py +0 -0
@@ -1 +0,0 @@
|
|
1
|
-
from gr_libs.ml.sequential.lstm_model import LstmObservations
|
@@ -0,0 +1,270 @@
|
|
1
|
+
import torch
|
2
|
+
import torch.nn as nn
|
3
|
+
import torch.nn.functional as F
|
4
|
+
from torch.nn.utils.rnn import pack_padded_sequence
|
5
|
+
|
6
|
+
from gr_libs.ml.utils import device
|
7
|
+
|
8
|
+
|
9
|
+
def accuracy_per_epoch(model, data_loader):
|
10
|
+
model.eval()
|
11
|
+
correct = total = 0.0
|
12
|
+
sum_loss = 0.0
|
13
|
+
with torch.no_grad():
|
14
|
+
for (
|
15
|
+
first_traces,
|
16
|
+
second_traces,
|
17
|
+
is_same_goals,
|
18
|
+
first_traces_lengths,
|
19
|
+
second_traces_lengths,
|
20
|
+
) in data_loader:
|
21
|
+
y_pred = model.forward_tab(
|
22
|
+
first_traces, second_traces, first_traces_lengths, second_traces_lengths
|
23
|
+
)
|
24
|
+
loss = F.binary_cross_entropy(y_pred, is_same_goals)
|
25
|
+
sum_loss += loss.item()
|
26
|
+
y_pred = y_pred >= 0.5
|
27
|
+
correct += torch.sum(y_pred == is_same_goals)
|
28
|
+
total += len(is_same_goals)
|
29
|
+
return correct / total, sum_loss / 32
|
30
|
+
|
31
|
+
|
32
|
+
def accuracy_per_epoch_cont(model, data_loader):
|
33
|
+
model.eval()
|
34
|
+
correct = total = 0.0
|
35
|
+
sum_loss = 0.0
|
36
|
+
with torch.no_grad():
|
37
|
+
for (
|
38
|
+
first_traces_images,
|
39
|
+
first_traces_texts,
|
40
|
+
second_traces_images,
|
41
|
+
second_traces_texts,
|
42
|
+
is_same_goals,
|
43
|
+
first_traces_lengths,
|
44
|
+
second_traces_lengths,
|
45
|
+
) in data_loader:
|
46
|
+
y_pred = model.forward_cont(
|
47
|
+
first_traces_images,
|
48
|
+
first_traces_texts,
|
49
|
+
second_traces_images,
|
50
|
+
second_traces_texts,
|
51
|
+
first_traces_lengths,
|
52
|
+
second_traces_lengths,
|
53
|
+
)
|
54
|
+
loss = F.binary_cross_entropy(y_pred, is_same_goals)
|
55
|
+
sum_loss += loss.item()
|
56
|
+
y_pred = y_pred >= 0.5
|
57
|
+
correct += torch.sum(y_pred == is_same_goals)
|
58
|
+
total += len(is_same_goals)
|
59
|
+
return correct / total, sum_loss / 32
|
60
|
+
|
61
|
+
# class CNNImageEmbeddor(nn.Module):
|
62
|
+
# def __init__(self, obs_space, action_space, use_text=False):
|
63
|
+
# super().__init__()
|
64
|
+
# self.use_text = use_text
|
65
|
+
# self.image_conv = nn.Sequential(
|
66
|
+
# nn.Conv2d(3, 4, kernel_size=(3, 3), padding=1), # Reduced filters, added padding
|
67
|
+
# nn.ReLU(),
|
68
|
+
# nn.MaxPool2d((2, 2)),
|
69
|
+
# nn.Conv2d(4, 4, (3, 3), padding=1), # Reduced filters, added padding
|
70
|
+
# nn.ReLU(),
|
71
|
+
# nn.MaxPool2d((2, 2)), # Added additional pooling to reduce size
|
72
|
+
# nn.Conv2d(4, 8, (3, 3), padding=1), # Reduced filters, added padding
|
73
|
+
# nn.ReLU(),
|
74
|
+
# nn.BatchNorm2d(8)
|
75
|
+
# )
|
76
|
+
# n = obs_space["image"][0]
|
77
|
+
# m = obs_space["image"][1]
|
78
|
+
# self.image_embedding_size = ((n - 4) // 4 - 3) * ((m - 4) // 4 - 3) * 8
|
79
|
+
# if self.use_text:
|
80
|
+
# self.word_embedding_size = 32
|
81
|
+
# self.word_embedding = nn.Embedding(obs_space["text"], self.word_embedding_size)
|
82
|
+
# self.text_embedding_size = 128
|
83
|
+
# self.text_rnn = nn.GRU(self.word_embedding_size, self.text_embedding_size, batch_first=True)
|
84
|
+
|
85
|
+
def forward(self, images, texts):
|
86
|
+
# images shape: batch_size X max_sequence_len X sample_size. same for text.
|
87
|
+
# need to reshape image to num_channels X height X width, like nn.Conv expects it to be.
|
88
|
+
x = images.transpose(2, 4).transpose(3, 4)
|
89
|
+
orig_shape = x.shape
|
90
|
+
# combine batch and sequence to 1 dimension so conv could handle it
|
91
|
+
x = x.view(
|
92
|
+
orig_shape[0] * orig_shape[1], orig_shape[2], orig_shape[3], orig_shape[4]
|
93
|
+
) # x shape: batch_size * max_sequence_len X sample_size
|
94
|
+
x = self.image_conv(
|
95
|
+
x
|
96
|
+
) # x shape: batch_size * max_sequence_len X last_conv_size X 1 X 1
|
97
|
+
# reshape x back to divide batches from sequences
|
98
|
+
x = x.view(
|
99
|
+
orig_shape[0], orig_shape[1], x.shape[1]
|
100
|
+
) # x shape: batch_size X max_sequence_len X last_conv_size. last 2 dimensions (1,1) are collapsed to last conv.
|
101
|
+
embedding = x
|
102
|
+
|
103
|
+
if self.use_text:
|
104
|
+
embed_text = self._get_embed_text(texts)
|
105
|
+
embedding = torch.cat((embedding, embed_text), dim=1)
|
106
|
+
|
107
|
+
return embedding
|
108
|
+
|
109
|
+
def _get_embed_text(self, text):
|
110
|
+
_, hidden = self.text_rnn(self.word_embedding(text))
|
111
|
+
return hidden[-1]
|
112
|
+
|
113
|
+
|
114
|
+
class LstmObservations(nn.Module):
|
115
|
+
|
116
|
+
def __init__(
|
117
|
+
self, input_size, hidden_size
|
118
|
+
): # TODO make sure the right cuda is used!
|
119
|
+
super().__init__()
|
120
|
+
# self.embeddor = CNNImageEmbeddor(obs_space, action_space)
|
121
|
+
# check if the traces are a bunch of images
|
122
|
+
self.lstm = nn.LSTM(
|
123
|
+
input_size=input_size, hidden_size=hidden_size, batch_first=True
|
124
|
+
)
|
125
|
+
self.dropout = nn.Dropout(0.5) # Added dropout layer
|
126
|
+
# Initialize weights
|
127
|
+
for name, param in self.lstm.named_parameters():
|
128
|
+
if "weight" in name:
|
129
|
+
nn.init.xavier_uniform_(param)
|
130
|
+
elif "bias" in name:
|
131
|
+
nn.init.zeros_(param)
|
132
|
+
|
133
|
+
# tabular
|
134
|
+
def forward_tab(self, traces1, traces2, lengths1, lengths2):
|
135
|
+
out1, (ht1, ct1) = self.lstm(
|
136
|
+
pack_padded_sequence(
|
137
|
+
traces1, lengths1, batch_first=True, enforce_sorted=False
|
138
|
+
),
|
139
|
+
None,
|
140
|
+
) # traces1 & traces 2 shapes: batch_size X max sequence_length X embedding_size
|
141
|
+
out2, (ht2, ct2) = self.lstm(
|
142
|
+
pack_padded_sequence(
|
143
|
+
traces2, lengths2, batch_first=True, enforce_sorted=False
|
144
|
+
),
|
145
|
+
None,
|
146
|
+
)
|
147
|
+
# out1, _ = pad_packed_sequence(out1, batch_first=True, total_length=max(lengths1))
|
148
|
+
# out2, _ = pad_packed_sequence(out2, batch_first=True, total_length=max(lengths2))
|
149
|
+
manhattan_dis = torch.exp(
|
150
|
+
-torch.sum(torch.abs(ht1[-1] - ht2[-1]), dim=1, keepdim=True)
|
151
|
+
)
|
152
|
+
return manhattan_dis.squeeze()
|
153
|
+
|
154
|
+
# continuous
|
155
|
+
# def forward_cont(self, traces1_images, traces1_texts, traces2_images, traces2_texts, lengths1, lengths2):
|
156
|
+
# # we also embed '0' images, but we take them out of the equation in the lstm (it knows to not treat them when batching)
|
157
|
+
# traces1 = self.embeddor(traces1_images, traces1_texts)
|
158
|
+
# traces2 = self.embeddor(traces2_images, traces2_texts) # traces1 & traces 2 shapes: batch_size X max_sequence_length X embedding_size
|
159
|
+
# out1, (ht1, ct1) = self.lstm(pack_padded_sequence(traces1, lengths1, batch_first=True, enforce_sorted=False), None)
|
160
|
+
# out2, (ht2, ct2) = self.lstm(pack_padded_sequence(traces2, lengths2, batch_first=True, enforce_sorted=False), None)
|
161
|
+
# manhattan_dis = torch.exp(-torch.sum(torch.abs(ht1[-1]-ht2[-1]),dim=1,keepdim=True))
|
162
|
+
# return manhattan_dis.squeeze()
|
163
|
+
|
164
|
+
def embed_sequence(self, trace):
|
165
|
+
trace = torch.stack(
|
166
|
+
[torch.tensor(observation, dtype=torch.float32) for observation in trace]
|
167
|
+
).to(device)
|
168
|
+
out, (ht, ct) = self.lstm(trace, None)
|
169
|
+
return ht[-1]
|
170
|
+
|
171
|
+
# def embed_sequence_cont(self, sequence, preprocess_obss):
|
172
|
+
# sequence = [preprocess_obss([obs])[0] for ((obs, (_, _)), _) in sequence]
|
173
|
+
# trace_images = torch.tensor(np.expand_dims(torch.stack([step.image for step in sequence]), axis=0)).to(device)
|
174
|
+
# trace_texts = torch.tensor(np.expand_dims(torch.stack([step.text for step in sequence]), axis=0)).to(device)
|
175
|
+
# embedded_trace = self.embeddor(trace_images, trace_texts)
|
176
|
+
# out, (ht, ct) = self.lstm(embedded_trace)
|
177
|
+
# return ht[-1]
|
178
|
+
|
179
|
+
|
180
|
+
def train_metric_model(model, train_loader, dev_loader, nepochs=5, patience=2):
|
181
|
+
devAccuracy = []
|
182
|
+
best_dev_accuracy = 0.0
|
183
|
+
no_improvement_count = 0
|
184
|
+
optimizer = torch.optim.Adadelta(model.parameters(), weight_decay=0.1)
|
185
|
+
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
|
186
|
+
optimizer, mode="min", patience=2, factor=0.5
|
187
|
+
)
|
188
|
+
for epoch in range(nepochs):
|
189
|
+
sum_loss, denominator = 0.0, 0.0
|
190
|
+
model.train()
|
191
|
+
for (
|
192
|
+
first_traces,
|
193
|
+
second_traces,
|
194
|
+
is_same_goals,
|
195
|
+
first_traces_lengths,
|
196
|
+
second_traces_lengths,
|
197
|
+
) in train_loader:
|
198
|
+
model.zero_grad()
|
199
|
+
y_pred = model.forward_tab(
|
200
|
+
first_traces, second_traces, first_traces_lengths, second_traces_lengths
|
201
|
+
)
|
202
|
+
if len(is_same_goals) == 1:
|
203
|
+
is_same_goals = torch.squeeze(
|
204
|
+
is_same_goals
|
205
|
+
) # for the case of batches in size 1...
|
206
|
+
loss = F.binary_cross_entropy(y_pred, is_same_goals)
|
207
|
+
sum_loss += loss.item()
|
208
|
+
denominator += 1
|
209
|
+
loss.backward()
|
210
|
+
optimizer.step()
|
211
|
+
|
212
|
+
dev_accuracy, dev_loss = accuracy_per_epoch(model, dev_loader)
|
213
|
+
devAccuracy.append(dev_accuracy)
|
214
|
+
if dev_accuracy > best_dev_accuracy:
|
215
|
+
best_dev_accuracy = dev_accuracy
|
216
|
+
no_improvement_count = 0
|
217
|
+
else:
|
218
|
+
no_improvement_count = 1
|
219
|
+
|
220
|
+
print(
|
221
|
+
f"epoch - {epoch + 1}/{nepochs}...",
|
222
|
+
f"train loss - {sum_loss / denominator:.6f}...",
|
223
|
+
f"dev loss - {dev_loss:.6f}...",
|
224
|
+
f"dev accuracy - {dev_accuracy:.6f}",
|
225
|
+
)
|
226
|
+
|
227
|
+
if no_improvement_count >= patience:
|
228
|
+
print(f"Early stopping after {epoch + 1} epochs with no improvement.")
|
229
|
+
break
|
230
|
+
|
231
|
+
|
232
|
+
def train_metric_model_cont(model, train_loader, dev_loader, nepochs=5):
|
233
|
+
devAccuracy = []
|
234
|
+
optimizer = torch.optim.Adadelta(model.parameters(), weight_decay=1.25)
|
235
|
+
for epoch in range(nepochs):
|
236
|
+
sum_loss, denominator = 0.0, 0.0
|
237
|
+
model.train()
|
238
|
+
for (
|
239
|
+
first_traces_images,
|
240
|
+
first_traces_texts,
|
241
|
+
second_traces_images,
|
242
|
+
second_traces_texts,
|
243
|
+
is_same_goals,
|
244
|
+
first_traces_lengths,
|
245
|
+
second_traces_lengths,
|
246
|
+
) in train_loader:
|
247
|
+
model.zero_grad()
|
248
|
+
y_pred = model.forward_cont(
|
249
|
+
first_traces_images,
|
250
|
+
first_traces_texts,
|
251
|
+
second_traces_images,
|
252
|
+
second_traces_texts,
|
253
|
+
first_traces_lengths,
|
254
|
+
second_traces_lengths,
|
255
|
+
)
|
256
|
+
loss = F.binary_cross_entropy(y_pred, is_same_goals)
|
257
|
+
sum_loss += loss.item()
|
258
|
+
denominator += 1
|
259
|
+
loss.backward()
|
260
|
+
optimizer.step()
|
261
|
+
|
262
|
+
dev_accuracy, dev_loss = accuracy_per_epoch_cont(model, dev_loader)
|
263
|
+
devAccuracy.append(dev_accuracy)
|
264
|
+
|
265
|
+
print(
|
266
|
+
f"epoch - {epoch + 1}/{nepochs}...",
|
267
|
+
f"train loss - {sum_loss / denominator:.6f}...",
|
268
|
+
f"dev loss - {dev_loss:.6f}...",
|
269
|
+
f"dev accuracy - {dev_accuracy:.6f}",
|
270
|
+
)
|
gr_libs/ml/tabular/__init__.py
CHANGED
gr_libs/ml/tabular/state.py
CHANGED
@@ -2,11 +2,9 @@ from abc import ABC
|
|
2
2
|
|
3
3
|
|
4
4
|
class TabularState(ABC):
|
5
|
-
def __init__(
|
6
|
-
|
7
|
-
|
8
|
-
agent_direction: int
|
9
|
-
):
|
5
|
+
def __init__(
|
6
|
+
self, agent_x_position: int, agent_y_position: int, agent_direction: int
|
7
|
+
):
|
10
8
|
self._agent_x_position = agent_x_position
|
11
9
|
self._agent_y_position = agent_y_position
|
12
10
|
self._agent_direction = agent_direction
|
@@ -14,8 +12,10 @@ class TabularState(ABC):
|
|
14
12
|
@staticmethod
|
15
13
|
def gen_tabular_state(environment, observation):
|
16
14
|
x, y = environment.unwrapped.agent_pos
|
17
|
-
direction = observation[
|
18
|
-
return TabularState(
|
15
|
+
direction = observation["direction"]
|
16
|
+
return TabularState(
|
17
|
+
agent_x_position=x, agent_y_position=y, agent_direction=direction
|
18
|
+
)
|
19
19
|
|
20
20
|
def __str__(self):
|
21
21
|
return f"({self._agent_x_position},{self._agent_y_position}):{self._agent_direction}"
|