Datasets:

ArXiv:
File size: 3,395 Bytes
3e10edb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import equinox as eqx
import jax
import jax.numpy as jnp

import popgym_arcade
from popgym_arcade.baselines.model.builder import QNetworkRNN
from popgym_arcade.baselines.utils import get_saliency_maps, vis_fn
from popgym_arcade.wrappers import LogWrapper

##
## Simpler approach:
## Compute gradients using random initial state
##
config = {
    "ENV_NAME": "MineSweeperEasy",
    "PARTIAL": False,
    "MEMORY_TYPE": "lru",
    "SEED": 0,
    "OBS_SIZE": 128,
}
# Path to your model weights
config["MODEL_PATH"] = (
    f"nips_analysis_128/PQN_RNN_{config['MEMORY_TYPE']}_{config['ENV_NAME']}_model_Partial={config['PARTIAL']}_SEED=0.pkl"
)

# Initialize the random key
rng = jax.random.PRNGKey(config["SEED"])

# Initialize the model
network = QNetworkRNN(rng, rnn_type=config["MEMORY_TYPE"], obs_size=config["OBS_SIZE"])
# Load the model
model = eqx.tree_deserialise_leaves(config["MODEL_PATH"], network)
# Compute the saliency maps
grads, obs_seq, grad_accumulator = get_saliency_maps(rng, model, config, max_steps=30)
# Visualize the saliency maps
vis_fn(grads, obs_seq, config, use_latex=True)


##
## More complex approach
## Generate custom initial state and then compute gradients
##
config = {
    "ENV_NAME": "NavigatorEasy",
    "PARTIAL": True,
    "MEMORY_TYPE": "lru",
    "SEED": 0,
    "OBS_SIZE": 128,
}
config["MODEL_PATH"] = (
    f"nips_analysis_128/PQN_RNN_{config['MEMORY_TYPE']}_{config['ENV_NAME']}_model_Partial={config['PARTIAL']}_SEED=0.pkl"
)


rng = jax.random.PRNGKey(config["SEED"])
# Initialize the model
network = QNetworkRNN(rng, rnn_type=config["MEMORY_TYPE"], obs_size=config["OBS_SIZE"])
# Load the model
model = eqx.tree_deserialise_leaves(config["MODEL_PATH"], network)

# Setup initial state
seed, _rng = jax.random.split(jax.random.key(config["SEED"]))
env, env_params = popgym_arcade.make(
    config["ENV_NAME"], partial_obs=config["PARTIAL"], obs_size=config["OBS_SIZE"]
)
env = LogWrapper(env)
n_envs = 1
vmap_reset = lambda n_envs: lambda rng: jax.vmap(env.reset, in_axes=(0, None))(
    jax.random.split(rng, n_envs), env_params
)
vmap_step = lambda n_envs: lambda rng, env_state, action: jax.vmap(
    env.step, in_axes=(0, 0, 0, None)
)(jax.random.split(rng, n_envs), env_state, action, env_params)
init_obs, init_state = vmap_reset(n_envs)(_rng)

# Replace initial state with custom initial state
new_init_state = eqx.tree_at(
    lambda x: x.env_state.action_x, init_state, replace=jnp.array([6])
)
new_init_state = eqx.tree_at(
    lambda x: x.env_state.action_y, new_init_state, replace=jnp.array([6])
)
board = (
    new_init_state.env_state.board.at[jnp.where(new_init_state.env_state.board == 2)]
    .set(0)
    .at[:, 1, 1]
    .set(2)
)
# Can also set the entire board manually if needed
# board = (
#     jnp.zeros_like(new_init_state.env_state.board)
#     # tnt
#     .at[0, 3, 2].set(1)
#     .at[0, 4, 2].set(1)
#     .at[0, 5, 3].set(1)
#     .at[0, 6, 3].set(1)
#     # goal
#     .at[0, 6, 6].set(2)
# )
new_init_state = eqx.tree_at(lambda x: x.env_state.board, new_init_state, replace=board)
new_init_obs = jax.vmap(env.get_obs)(new_init_state.env_state)


# Compute the saliency maps
grads, obs_seq, grad_accumulator = get_saliency_maps(
    rng,
    model,
    config,
    max_steps=10,
    initial_state_and_obs=(new_init_state, new_init_obs),
)
# Visualize the saliency maps
vis_fn(grads, obs_seq, config, use_latex=True)