|
49 | 49 | class TestCBTutorials(unittest.TestCase): |
50 | 50 | def setUp(self) -> None: |
51 | 51 | super().setUp() |
52 | | - |
53 | | - def test_cb_tutorials(self) -> None: |
54 | | - # load environment |
55 | | - device_id = 0 if torch.cuda.is_available() else -1 |
56 | | - |
57 | | - # Download UCI dataset if doesn't exist |
58 | | - uci_data_path = "./utils/instantiations/environments/uci_datasets" |
59 | | - if not os.path.exists(uci_data_path): |
60 | | - os.makedirs(uci_data_path) |
61 | | - download_uci_data(data_path=uci_data_path) |
62 | | - |
63 | | - # Built CB environment using the pendigits UCI dataset |
64 | | - pendigits_uci_dict = { |
65 | | - "path_filename": os.path.join(uci_data_path, "pendigits/pendigits.tra"), |
66 | | - "action_embeddings": "discrete", |
67 | | - "delim_whitespace": False, |
68 | | - "ind_to_drop": [], |
69 | | - "target_column": 16, |
70 | | - } |
71 | | - env = SLCBEnvironment(**pendigits_uci_dict) # pyre-ignore |
72 | | - |
73 | | - # experiment code |
74 | | - number_of_steps = 10 |
75 | | - record_period = 10 |
76 | | - |
77 | | - """ |
78 | | - SquareCB |
79 | | - """ |
80 | | - # Create a Neural SquareCB pearl agent with 1-hot action representation |
81 | | - action_representation_module = OneHotActionTensorRepresentationModule( |
82 | | - max_number_actions=env.unique_labels_num, |
83 | | - ) |
84 | | - |
85 | | - agent = PearlAgent( |
86 | | - policy_learner=NeuralBandit( |
87 | | - feature_dim=env.observation_dim + env.unique_labels_num, |
88 | | - hidden_dims=[2], |
89 | | - training_rounds=2, |
90 | | - learning_rate=0.01, |
91 | | - action_representation_module=action_representation_module, |
92 | | - exploration_module=SquareCBExploration( |
93 | | - gamma=env.observation_dim * env.unique_labels_num * number_of_steps |
94 | | - ), |
95 | | - ), |
96 | | - replay_buffer=BasicReplayBuffer(100_000), |
97 | | - device_id=device_id, |
98 | | - ) |
99 | | - |
100 | | - _ = online_learning( |
101 | | - agent=agent, |
102 | | - env=env, |
103 | | - number_of_steps=number_of_steps, |
104 | | - print_every_x_steps=10, |
105 | | - record_period=record_period, |
106 | | - learn_after_episode=True, |
107 | | - ) |
108 | | - |
109 | | - # Neural LinUCB |
110 | | - action_representation_module = OneHotActionTensorRepresentationModule( |
111 | | - max_number_actions=env.unique_labels_num, |
112 | | - ) |
113 | | - |
114 | | - agent = PearlAgent( |
115 | | - policy_learner=NeuralLinearBandit( |
116 | | - feature_dim=env.observation_dim + env.unique_labels_num, |
117 | | - hidden_dims=[2], |
118 | | - state_features_only=False, |
119 | | - training_rounds=2, |
120 | | - learning_rate=0.01, |
121 | | - action_representation_module=action_representation_module, |
122 | | - exploration_module=UCBExploration(alpha=1.0), |
123 | | - ), |
124 | | - replay_buffer=BasicReplayBuffer(100_000), |
125 | | - device_id=device_id, |
126 | | - ) |
127 | | - |
128 | | - _ = online_learning( |
129 | | - agent=agent, |
130 | | - env=env, |
131 | | - number_of_steps=number_of_steps, |
132 | | - print_every_x_steps=100, |
133 | | - record_period=record_period, |
134 | | - learn_after_episode=True, |
135 | | - ) |
136 | | - |
137 | | - # Neural LinTS |
138 | | - |
139 | | - action_representation_module = OneHotActionTensorRepresentationModule( |
140 | | - max_number_actions=env.unique_labels_num, |
141 | | - ) |
142 | | - |
143 | | - agent = PearlAgent( |
144 | | - policy_learner=NeuralLinearBandit( |
145 | | - feature_dim=env.observation_dim + env.unique_labels_num, |
146 | | - hidden_dims=[2], |
147 | | - state_features_only=False, |
148 | | - training_rounds=2, |
149 | | - learning_rate=0.01, |
150 | | - action_representation_module=action_representation_module, |
151 | | - exploration_module=ThompsonSamplingExplorationLinear(), |
152 | | - ), |
153 | | - replay_buffer=BasicReplayBuffer(100_000), |
154 | | - device_id=device_id, |
155 | | - ) |
156 | | - |
157 | | - _ = online_learning( |
158 | | - agent=agent, |
159 | | - env=env, |
160 | | - number_of_steps=number_of_steps, |
161 | | - print_every_x_steps=10, |
162 | | - record_period=record_period, |
163 | | - learn_after_episode=True, |
164 | | - ) |
0 commit comments