Reputation: 31
So I am trying to create a Bayesian optimization workflow using modAL. My task is a regression task over a sample set consisting of samples with 2048 features and 1 regression target. I am currently using the max_PI acquisition function but have run into the following error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13924/1162646393.py in <module>
99 pvg_row+=1
100 try:
--> 101 query_idx,query_inst = learner.query(X, n_instances=20)
102 except AssertionError:
103 print("Encountered a case where the number of intances is lower than utility")
~\anaconda3\envs\fyp-al\lib\site-packages\modAL\models\base.py in query(self, X_pool, *query_args, **query_kwargs)
251 labelled upon query synthesis.
252 """
--> 253 query_result = self.query_strategy(self, X_pool, *query_args, **query_kwargs)
254
255 if isinstance(query_result, tuple):
~\anaconda3\envs\fyp-al\lib\site-packages\modAL\acquisition.py in max_PI(optimizer, X, tradeoff, n_instances)
118 The indices of the instances from X chosen to be labelled; the instances from X chosen to be labelled.
119 """
--> 120 pi = optimizer_PI(optimizer, X, tradeoff=tradeoff)
121 return multi_argmax(pi, n_instances=n_instances)
122
~\anaconda3\envs\fyp-al\lib\site-packages\modAL\acquisition.py in optimizer_PI(optimizer, X, tradeoff)
47 """
48 try:
---> 49 mean, std = optimizer.predict(X, return_std=True)
50 mean, std = mean.reshape(-1, ), std.reshape(-1, )
51 except NotFittedError:
~\anaconda3\envs\fyp-al\lib\site-packages\modAL\models\base.py in predict(self, X, **predict_kwargs)
220 Estimator predictions for X.
221 """
--> 222 return self.estimator.predict(X, **predict_kwargs)
223
224 def predict_proba(self, X: modALinput, **predict_proba_kwargs) -> Any:
TypeError: predict() got an unexpected keyword argument 'return_std'
My regressor model is
reg = SkorchNNR(
Torch_Model,
lr=1e-3,
criterion= nn.MSELoss,
max_epochs = 50,
batch_size = 128,
train_split = skorch.dataset.CVSplit(5) if int(X.shape[0]*0.1)>5 else skorch.dataset.CVSplit(int(X.shape[0]*0.1)),
optimizer =torch.optim.Adam,
device = device,
verbose = 1
)
and the corresponding Bayesian Optimizer object is
learner = BayesianOptimizer(
estimator=clf,
query_strategy=gpr_model["acquisition_function"],
X_training=x_initial, y_training=y_initial.reshape(-1,1)
)
The Minimum Reproducible Example is:
# Importing Libraries
import numpy as np
import torch
from torch import nn
import skorch
from skorch import NeuralNetRegressor as SkorchNNR
from modAL.models import BayesianOptimizer
from modAL.acquisition import max_PI
import numpy as np
# Generating Dataset
row,col = 50,2048
X = np.random.randint(2, size=(row,col)).astype(np.float32)
y = np.random.randn(row,1).astype(np.float32) ** 2
# Picking out initial training samples
train_idx = np.random.choice(range(X.shape[0]), size=int(X.shape[0]*0.2), replace=False)
x_initial = X[train_idx]
y_initial = y[train_idx]
X = np.delete(X,train_idx, axis=0)
y = np.delete(y,train_idx)
# Displaying Shape after initial selection
print(X.shape, y.shape)
print(x_initial.shape, y_initial.shape)
# PyTorch Model
class Torch_Model_BasicRegressor(nn.Module):
def __init__(self):
super(Torch_Model_BasicRegressor, self).__init__()
self.fcs = nn.Sequential(
nn.Linear(2048,256),
nn.ReLU(),
nn.Dropout(0.25),
nn.Linear(256,32),
nn.ReLU(),
nn.Dropout(0.25),
nn.Linear(32,4),
nn.ReLU(),
nn.Dropout(0.25),
nn.Linear(4,1)
)
def forward(self, x):
out = self.fcs(x)
return out
# SKORCH model
device = "cuda" if torch.cuda.is_available()==True else "cpu"
regressor = SkorchNNR(
Torch_Model_BasicRegressor,
lr=1e-5,
criterion= nn.MSELoss,
max_epochs = 25,
batch_size = 128,
train_split = skorch.dataset.ValidSplit(5) if int(X.shape[0]*0.2)>5 else skorch.dataset.ValidSplit(int(X.shape[0]*0.2)),
optimizer =torch.optim.Adam,
device = device,
verbose = 1
)
# modAL model
learner = BayesianOptimizer(
estimator=regressor,
query_strategy=max_PI,
X_training=x_initial, y_training=y_initial.reshape(-1,1)
)
# Querying & Teaching the model : Error over here
for n_query in range(15):
query_idx,query_inst = learner.query(X, n_instances=5)
learner.teach(X=query_inst,y=y[query_idx])
Upvotes: 0
Views: 1569
Reputation: 1
I'm seeing you are declaring a classifier (clf) in the BayesianOptimizer estimator parameter. As you can see in the example, the estimator parameter is a regressor. In your case it should be your model 'SkorchNNR'.
Upvotes: 0