Imitating Unknown Policies via Exploration

Bases: ABCO

Imitating Unknown Policies via Exploration method based on (Gavenski et. al., 2020)

Source code in src/benchmark/methods/iupe.py
 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
class IUPE(ABCO):
    """Imitating Unknown Policies via Exploration method based on (Gavenski et. al., 2020)"""

    __version__ = "1.0.0"
    __author__ = "Gavenski et. al."
    __method_name__ = "Imitating Unknown Policies via Exploration"

    def __init__(
        self,
        environment: Env,
        enjoy_criteria: int = 100,
        verbose: bool = False,
        config_file: str = None
    ) -> None:
        if config_file is None:
            config_file = CONFIG_FILE
        super().__init__(environment, enjoy_criteria, verbose, config_file)
        self.save_path = f"./tmp/iupe/{self.environment_name}/"
        self.is_training = False

    def predict(
        self,
        obs: Union[np.ndarray, torch.Tensor],
        transforms: Callable[[torch.Tensor], torch.Tensor] = None
    ) -> Union[List[Number], Number]:
        """Predict method.

        Args:
            obs (Union[np.ndarray, torch.Tensor]): input observation.
            transforms (Callable[torch.Tensor, torch.Tensor]): torchvision
                transforms functions (required only for visual environments).
                Defaults to None.

        Returns:
            action (Union[List[Number], Number): predicted action.
        """
        self.policy.eval()

        if isinstance(obs, np.ndarray):
            if not self.visual:
                obs = torch.from_numpy(obs)
                if transforms is not None:
                    obs = transforms(obs)
                if len(obs.shape) == 1:
                    obs = obs[None]
            else:
                if transforms is None:
                    raise ValueError("Visual information requires transforms parameter.")
                obs = transforms(obs)
                if len(obs.shape) == 3:
                    obs = obs[None]

        obs = obs.to(self.device)

        with torch.no_grad():
            actions = self.forward(obs)
            actions = actions[0]

            if self.discrete:
                if self.is_training:
                    classes = np.arange(self.action_size)
                    prob = torch.nn.functional.softmax(actions, dim=0).cpu().detach().numpy()
                    actions = np.random.choice(classes, p=prob)
                    return actions
                return torch.argmax(actions).cpu().numpy()
            return actions.cpu().numpy()

    def train(
        self,
        n_epochs: int,
        train_dataset: Dict[str, DataLoader],
        eval_dataset: Dict[str, DataLoader] = None,
        folder: str = None
    ) -> Self:
        if folder is None:
            folder = f"../benchmark_results/iupe/{self.environment_name}"

        self.is_training = True

        try:
            super().train(
                n_epochs,
                train_dataset,
                eval_dataset,
                folder
            )
            return self
        finally:
            self.is_training = False

    def _train(self, idm_dataset: DataLoader, expert_dataset: DataLoader) -> Metrics:
        """Train loop.

        Args:
            dataset (DataLoader): train data.
        """
        if not self.idm.training:
            self.idm.train()

        if not self.policy.training:
            self.policy.train()

        idm_accumulated_loss = []
        idm_accumulated_accuracy = []

        accumulated_loss = []
        accumulated_accuracy = []

        for batch in idm_dataset:
            state, action, next_state = batch
            state = state.to(self.device)
            action = action.to(self.device)
            next_state = next_state.to(self.device)

            self.idm_optimizer.zero_grad()
            predictions = self.idm(torch.cat((state, next_state), dim=1))

            loss = self.idm_loss(predictions, action.squeeze(1).long())
            loss.backward()
            idm_accumulated_loss.append(loss.item())
            self.idm_optimizer.step()

            accuracy: Number = None
            if self.discrete:
                accuracy = accuracy_fn(predictions, action.squeeze(1))
            else:
                accuracy = (action - predictions).pow(2).sum(1).sqrt().mean().item()
            idm_accumulated_accuracy.append(accuracy)

        self.idm.eval()

        for batch in expert_dataset:
            state, _, next_state = batch
            state = state.to(self.device)
            next_state = next_state.to(self.device)

            with torch.no_grad():
                if self.discrete:
                    prediction = self.idm(torch.cat((state, next_state), dim=1))
                    # Compute probabilities to models logits
                    classes = np.arange(self.action_size)
                    prob = torch.nn.functional.softmax(prediction, dim=1).cpu().detach().numpy()
                    # Sample from the probabilities
                    samples = prob.cumsum(axis=1)
                    random = np.random.rand(prob.shape[1])
                    indexes = (samples < random).sum(axis=1)
                    action = classes[indexes]
                    # Convert to tensor
                    action = torch.tensor(action).view((-1, 1))
                    action = action.to(self.device)
                else:
                    action = self.idm(torch.cat((state, next_state), dim=1))

            self.optimizer_fn.zero_grad()
            predictions = self.forward(state)

            loss = self.loss_fn(predictions, action.squeeze(1).long())
            loss.backward()
            accumulated_loss.append(loss.item())
            self.optimizer_fn.step()

            accuracy: Number = None
            if self.discrete:
                accuracy = accuracy_fn(predictions, action.squeeze(1))
            else:
                accuracy = (action - predictions).pow(2).sum(1).sqrt().mean().item()
            accumulated_accuracy.append(accuracy)

        metrics = {
            "idm_loss": np.mean(idm_accumulated_loss),
            "idm_accuracy": np.mean(idm_accumulated_accuracy),
            "loss": np.mean(accumulated_loss),
            "accuracy": np.mean(accumulated_accuracy)
        }
        return metrics

_train(idm_dataset, expert_dataset)

Train loop.

Parameters:
  • dataset (DataLoader) –

    train data.

src/benchmark/methods/iupe.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
def _train(self, idm_dataset: DataLoader, expert_dataset: DataLoader) -> Metrics:
    """Train loop.

    Args:
        dataset (DataLoader): train data.
    """
    if not self.idm.training:
        self.idm.train()

    if not self.policy.training:
        self.policy.train()

    idm_accumulated_loss = []
    idm_accumulated_accuracy = []

    accumulated_loss = []
    accumulated_accuracy = []

    for batch in idm_dataset:
        state, action, next_state = batch
        state = state.to(self.device)
        action = action.to(self.device)
        next_state = next_state.to(self.device)

        self.idm_optimizer.zero_grad()
        predictions = self.idm(torch.cat((state, next_state), dim=1))

        loss = self.idm_loss(predictions, action.squeeze(1).long())
        loss.backward()
        idm_accumulated_loss.append(loss.item())
        self.idm_optimizer.step()

        accuracy: Number = None
        if self.discrete:
            accuracy = accuracy_fn(predictions, action.squeeze(1))
        else:
            accuracy = (action - predictions).pow(2).sum(1).sqrt().mean().item()
        idm_accumulated_accuracy.append(accuracy)

    self.idm.eval()

    for batch in expert_dataset:
        state, _, next_state = batch
        state = state.to(self.device)
        next_state = next_state.to(self.device)

        with torch.no_grad():
            if self.discrete:
                prediction = self.idm(torch.cat((state, next_state), dim=1))
                # Compute probabilities to models logits
                classes = np.arange(self.action_size)
                prob = torch.nn.functional.softmax(prediction, dim=1).cpu().detach().numpy()
                # Sample from the probabilities
                samples = prob.cumsum(axis=1)
                random = np.random.rand(prob.shape[1])
                indexes = (samples < random).sum(axis=1)
                action = classes[indexes]
                # Convert to tensor
                action = torch.tensor(action).view((-1, 1))
                action = action.to(self.device)
            else:
                action = self.idm(torch.cat((state, next_state), dim=1))

        self.optimizer_fn.zero_grad()
        predictions = self.forward(state)

        loss = self.loss_fn(predictions, action.squeeze(1).long())
        loss.backward()
        accumulated_loss.append(loss.item())
        self.optimizer_fn.step()

        accuracy: Number = None
        if self.discrete:
            accuracy = accuracy_fn(predictions, action.squeeze(1))
        else:
            accuracy = (action - predictions).pow(2).sum(1).sqrt().mean().item()
        accumulated_accuracy.append(accuracy)

    metrics = {
        "idm_loss": np.mean(idm_accumulated_loss),
        "idm_accuracy": np.mean(idm_accumulated_accuracy),
        "loss": np.mean(accumulated_loss),
        "accuracy": np.mean(accumulated_accuracy)
    }
    return metrics

predict(obs, transforms=None)

Predict method.

Parameters:
  • obs (Union[ndarray, Tensor]) –

    input observation.

  • transforms (Callable[Tensor, Tensor], default: None ) –

    torchvision transforms functions (required only for visual environments). Defaults to None.

Returns:
  • action( Union[List[Number], Number ) –

    predicted action.

src/benchmark/methods/iupe.py
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
def predict(
    self,
    obs: Union[np.ndarray, torch.Tensor],
    transforms: Callable[[torch.Tensor], torch.Tensor] = None
) -> Union[List[Number], Number]:
    """Predict method.

    Args:
        obs (Union[np.ndarray, torch.Tensor]): input observation.
        transforms (Callable[torch.Tensor, torch.Tensor]): torchvision
            transforms functions (required only for visual environments).
            Defaults to None.

    Returns:
        action (Union[List[Number], Number): predicted action.
    """
    self.policy.eval()

    if isinstance(obs, np.ndarray):
        if not self.visual:
            obs = torch.from_numpy(obs)
            if transforms is not None:
                obs = transforms(obs)
            if len(obs.shape) == 1:
                obs = obs[None]
        else:
            if transforms is None:
                raise ValueError("Visual information requires transforms parameter.")
            obs = transforms(obs)
            if len(obs.shape) == 3:
                obs = obs[None]

    obs = obs.to(self.device)

    with torch.no_grad():
        actions = self.forward(obs)
        actions = actions[0]

        if self.discrete:
            if self.is_training:
                classes = np.arange(self.action_size)
                prob = torch.nn.functional.softmax(actions, dim=0).cpu().detach().numpy()
                actions = np.random.choice(classes, p=prob)
                return actions
            return torch.argmax(actions).cpu().numpy()
        return actions.cpu().numpy()