@@ -54,57 +54,60 @@ Adjust the value to match the number of CPU (or GPU) devices you plan to use.
5454
5555``` python
5656import os
57+
5758os.environ[" XLA_FLAGS" ] = " --xla_force_host_platform_device_count=8"
59+
5860import jax.numpy as jnp
59- from sklearn.datasets import make_regression
61+ from sklearn.datasets import fetch_openml
62+ from sklearn.metrics import root_mean_squared_error
6063from sklearn.model_selection import train_test_split
6164
6265from bde import BdeRegressor
6366from bde.loss import GaussianNLL
6467
6568
66- # Generate and split a toy regression problem
67- X, y = make_regression(
68- n_samples = 500 ,
69- n_features = 8 ,
70- noise = 0.3 ,
71- random_state = 42 ,
72- )
73- X = X.astype(" float32" )
74- y = y.astype(" float32" )
69+ data = fetch_openml(name = " airfoil_self_noise" , as_frame = True )
70+ X = data.data.values # shape (1503, 5)
71+ y = data.target.values.reshape(- 1 , 1 ) # shape (1503, 1)
72+
7573X_train, X_test, y_train, y_test = train_test_split(
76- X,
77- y,
78- test_size = 0.2 ,
79- random_state = 0 ,
74+ X,
75+ y,
76+ test_size = 0.2 ,
77+ random_state = 0 ,
8078)
79+ # Normalize data
80+ Xmu, Xstd = jnp.mean(X_train, 0 ), jnp.std(X_train, 0 ) + 1e-8
81+ Ymu, Ystd = jnp.mean(y_train, 0 ), jnp.std(y_train, 0 ) + 1e-8
8182
82- # Convert to JAX arrays expected by the estimators
83- X_train = jnp.array(X_train)
84- y_train = jnp.array(y_train)
85- X_test = jnp.array(X_test)
86- y_test = jnp.array(y_test)
83+ Xtr = (X_train - Xmu) / Xstd
84+ Xte = (X_test - Xmu) / Xstd
85+ ytr = (y_train - Ymu) / Ystd
86+ yte = (y_test - Ymu) / Ystd
8787
8888regressor = BdeRegressor(
89- n_members = 4 ,
90- hidden_layers = [ 64 , 64 ] ,
91- seed = 123 ,
89+ hidden_layers = [ 16 , 16 ] ,
90+ n_members = 20 ,
91+ seed = 0 ,
9292 loss = GaussianNLL(),
93- activation = " relu" ,
94- epochs = 100 ,
93+ epochs = 200 ,
94+ lr = 1e-3 ,
95+ warmup_steps = 500 ,
96+ n_samples = 100 ,
97+ n_thinning = 1 ,
9598 patience = 10 ,
96- n_samples = 20 ,
97- warmup_steps = 100 ,
98- lr = 5e-4 ,
99- n_thinning = 2 ,
100- desired_energy_var_start = 0.5 ,
101- desired_energy_var_end = 0.1 ,
102- step_size_init = 0.01 ,
10399)
104100
105- regressor.fit(x = X_train, y = y_train)
106- mean, std = regressor.predict(X_test, mean_and_std = True )
107- print (" RMSE:" , jnp.sqrt(jnp.mean((mean - y_test) ** 2 )))
101+ regressor.fit(x = Xtr, y = ytr)
102+
103+ mean, std = regressor.predict(jnp.array(X_test), mean_and_std = True )
104+ mu, intervals = regressor.predict(Xte, credible_intervals = [0.9 , 0.95 ])
105+ raw = regressor.predict(Xte, raw = True )
106+ print (" RSME: " , root_mean_squared_error(y_true = yte, y_pred = mean))
107+ score = regressor.score(Xtr, ytr)
108+ print (f " the sklearn score is { score} " )
109+
110+
108111```
109112
110113### Classification Example
@@ -113,54 +116,41 @@ print("RMSE:", jnp.sqrt(jnp.mean((mean - y_test) ** 2)))
113116import os
114117
115118os.environ[" XLA_FLAGS" ] = " --xla_force_host_platform_device_count=8"
116- import jax.numpy as jnp
119+
117120from sklearn.datasets import load_iris
118121from sklearn.model_selection import train_test_split
119122
120123from bde import BdeClassifier
121124from bde.loss import CategoricalCrossEntropy
122125
123- # Prepare the Iris dataset
124- X, y = load_iris(return_X_y = True )
125- X = X.astype(" float32" )
126- y = y.astype(" int32" )
126+ iris = load_iris()
127+ X = iris.data.astype(" float32" )
128+ y = iris.target.astype(" int32" ).ravel()
127129X_train, X_test, y_train, y_test = train_test_split(
128- X,
129- y,
130- test_size = 0.2 ,
131- random_state = 0 ,
132- stratify = y,
133- )
134-
135- # Convert to JAX arrays expected by the estimators
136- X_train = jnp.array(X_train)
137- y_train = jnp.array(y_train)
138- X_test = jnp.array(X_test)
139- y_test = jnp.array(y_test)
140-
130+ X, y, test_size = 0.2 , random_state = 42 )
141131classifier = BdeClassifier(
142- n_members = 3 ,
143- hidden_layers = [32 , 32 ],
144- seed = 456 ,
132+ n_members = 2 ,
133+ hidden_layers = [16 , 16 ],
134+ seed = 0 ,
145135 loss = CategoricalCrossEntropy(),
146136 activation = " relu" ,
147- epochs = 50 ,
148- patience = 8 ,
149- n_samples = 15 ,
150- warmup_steps = 80 ,
137+ epochs = 4 ,
151138 lr = 1e-3 ,
152- n_thinning = 2 ,
153- desired_energy_var_start = 0.5 ,
154- desired_energy_var_end = 0.1 ,
155- step_size_init = 0.01 ,
156- )
157-
139+ warmup_steps = 50 ,
140+ n_samples = 2 ,
141+ n_thinning = 1 ,
142+ patience = 2
143+ )
158144classifier.fit(x = X_train, y = y_train)
159145preds = classifier.predict(X_test)
160146probs = classifier.predict_proba(X_test)
161- print (" Predicted probabilities shape:" , probs.shape)
162- accuracy = jnp.mean((jnp.array(preds) == y_test).astype(jnp.float32))
163- print (" Accuracy:" , float (accuracy))
147+ score = classifier.score(X_train, y_train)
148+ raw = classifier.predict(X_test, raw = True )
149+ print (" Predicted class probabilities:\n " , probs)
150+ print (" Predicted class labels:\n " , preds)
151+ print (" True labels:\n " , y_test)
152+ print (f " the sklearn score is { score} " )
153+ print (f " The shape of the raw predictions are { raw.shape} " )
164154```
165155
166156Workflow
0 commit comments