@@ -26,18 +26,20 @@ Basic imports and preprocessing
2626 from torch.nn.functional import mse_loss
2727 from torch.distributions import Categorical
2828
29- from gbrl import GradientBoostingTrees, cuda_available, ParametricActor
29+ from gbrl import cuda_available
30+ from gbrl.models import GBTModel, ParametricActor
3031
3132 Pre-process data
3233~~~~~~~~~~~~~~~~
3334.. code-block :: python
3435
3536 # CUDA is not deterministic
36- device = ' cuda' if cuda_available else ' cpu'
37+ device = ' cuda' if cuda_available() else ' cpu'
3738 # incremental learning dataset
3839 X_numpy, y_numpy = datasets.load_diabetes(return_X_y = True , as_frame = False , scaled = False )
3940 # Reshape target as GBRL works with 2D arrays
4041 out_dim = 1 if len (y_numpy.shape) == 1 else y_numpy.shape[1 ]
42+ input_dim = X_numpy.shape[1 ]
4143
4244 X, y = th.tensor(X_numpy, dtype = th.float32, device = device), th.tensor(y_numpy, dtype = th.float32, device = device)
4345
@@ -54,19 +56,22 @@ Setting up a GBRL model
5456 ' grow_policy' : ' oblivious' }
5557
5658 optimizer = {' algo' : ' SGD' ,
57- ' lr' : 1.0 }
59+ ' lr' : 1.0 ,
60+ ' start_idx' : 0 ,
61+ ' stop_idx' : out_dim}
5862
5963 gbrl_params = {
6064 " split_score_func" : " Cosine" ,
6165 " generator_type" : " Quantile"
6266 }
6367
6468 # setting up model
65- gbt_model = GradientBoostingTrees(
69+ gbt_model = GBTModel(
70+ input_dim = input_dim,
6671 output_dim = out_dim,
6772 tree_struct = tree_struct,
68- optimizer = optimizer,
69- gbrl_params = gbrl_params,
73+ optimizers = optimizer,
74+ params = gbrl_params,
7075 verbose = 0 ,
7176 device = device)
7277 gbt_model.set_bias_from_targets(y)
@@ -94,11 +99,12 @@ For example: when using a summation reduction
9499
95100.. code-block :: python
96101
97- gbt_model = GradientBoostingTrees(
102+ gbt_model = GBTModel(
103+ input_dim = input_dim,
98104 output_dim = out_dim,
99105 tree_struct = tree_struct,
100- optimizer = optimizer,
101- gbrl_params = gbrl_params,
106+ optimizers = optimizer,
107+ params = gbrl_params,
102108 verbose = 0 ,
103109 device = device)
104110 gbt_model.set_bias_from_targets(y)
@@ -119,11 +125,12 @@ or when working with multi-dimensional outputs
119125
120126 y_multi = th.concat([y, y], dim = 1 )
121127 out_dim = y_multi.shape[1 ]
122- gbt_model = GradientBoostingTrees(
128+ gbt_model = GBTModel(
129+ input_dim = input_dim,
123130 output_dim = out_dim,
124131 tree_struct = tree_struct,
125- optimizer = optimizer,
126- gbrl_params = gbrl_params,
132+ optimizers = optimizer,
133+ params = gbrl_params,
127134 verbose = 0 ,
128135 device = device)
129136 gbt_model.set_bias_from_targets(y_multi)
@@ -144,13 +151,13 @@ Saving, loading, and copying a GBRL Model
144151
145152.. code-block :: python
146153
147- # Call the save_model method of a GBRL class
154+ # Call the save_learner method of a GBRL class
148155 # GBRL will automatically save the file with the .gbrl_model ending
149156 # The file will be saved in the current working directory
150157 # Provide the absolute path to save the file in a different directory.
151- gbt_model.save_model (' gbt_model_tutorial' )
158+ gbt_model.save_learner (' gbt_model_tutorial' )
152159 # Loading a saved model is similar and is done by calling the specific class instance.
153- loaded_gbt_model = GradientBoostingTrees.load_model (' gbt_model_tutorial' )
160+ loaded_gbt_model = GBTModel.load_learner (' gbt_model_tutorial' , device = device )
154161 # Copying a model is straighforward
155162 copied_model = gbt_model.copy()
156163
@@ -169,18 +176,21 @@ Fitting manually calculated gradients is done using the `_model.step` method tha
169176 ' grow_policy' : ' oblivious' }
170177
171178 optimizer = { ' algo' : ' SGD' ,
172- ' lr' : 1.0 }
179+ ' lr' : 1.0 ,
180+ ' start_idx' : 0 ,
181+ ' stop_idx' : 1 }
173182
174183 gbrl_params = {
175184 " split_score_func" : " Cosine" ,
176185 " generator_type" : " Quantile" }
177186
178187 # setting up model
179- gbt_model = GradientBoostingTrees(
188+ gbt_model = GBTModel(
189+ input_dim = input_dim,
180190 output_dim = 1 ,
181191 tree_struct = tree_struct,
182- optimizer = optimizer,
183- gbrl_params = gbrl_params,
192+ optimizers = optimizer,
193+ params = gbrl_params,
184194 verbose = 0 ,
185195 device = device)
186196 # works with numpy arrays as well as PyTorch tensors
@@ -193,8 +203,8 @@ Fitting manually calculated gradients is done using the `_model.step` method tha
193203 y_pred = gbt_model(X_numpy, tensor = False )
194204 loss = np.sqrt(0.5 * ((y_pred - y_numpy)** 2 ).mean())
195205 grads = y_pred - y_numpy
196- # perform a boosting step
197- gbt_model._model. step(X_numpy, grads)
206+ # perform a boosting step with manual gradients
207+ gbt_model.step(X_numpy, grads)
198208 print (f " Boosting iteration: { gbt_model.get_iteration()} RMSE loss: { loss} " )
199209
200210 Multiple boosting iterations
@@ -207,11 +217,12 @@ GBRL supports training multiple boosting iterations with targets similar to othe
207217
208218.. code-block :: python
209219
210- gbt_model = GradientBoostingTrees(
220+ gbt_model = GBTModel(
221+ input_dim = input_dim,
211222 output_dim = 1 ,
212223 tree_struct = tree_struct,
213- optimizer = optimizer,
214- gbrl_params = gbrl_params,
224+ optimizers = optimizer,
225+ params = gbrl_params,
215226 verbose = 1 ,
216227 device = device)
217228 final_loss = gbt_model.fit(X_numpy, y_numpy, iterations = 10 )
@@ -240,14 +251,17 @@ Let's start by training a simple Reinforce algorithm.
240251 num_episodes = 1000
241252 gamma = 0.99
242253 optimizer = { ' algo' : ' SGD' ,
243- ' lr' : 0.05 }
254+ ' lr' : 0.05 ,
255+ ' start_idx' : 0 ,
256+ ' stop_idx' : env.action_space.n}
244257
245258 bias = np.zeros(env.action_space.n, dtype = np.single)
246259 agent = ParametricActor(
260+ input_dim = env.observation_space.shape[0 ],
247261 output_dim = env.action_space.n,
248262 tree_struct = tree_struct,
249263 policy_optimizer = optimizer,
250- gbrl_params = gbrl_params,
264+ params = gbrl_params,
251265 verbose = 0 ,
252266 bias = bias,
253267 device = ' cpu' )
@@ -267,10 +281,10 @@ Let's start by training a simple Reinforce algorithm.
267281 action_logits = agent(obs)
268282 action = Categorical(logits = action_logits).sample()
269283 action_numpy = action.cpu().numpy()
284+ rollout_buffer[' obs' ].append(obs)
270285
271286 obs, reward, terminated, truncated, info = wrapped_env.step(action_numpy.squeeze())
272287 rollout_buffer[' rewards' ].append(reward)
273- rollout_buffer[' obs' ].append(obs)
274288 rollout_buffer[' actions' ].append(action)
275289
276290 done = terminated or truncated
0 commit comments