因此,我试图使用modAL创建一个贝叶斯优化工作流。我的任务是一个样本集上的回归任务,包含2048个特征和1个回归目标的样本。我目前正在使用圆周率获取函数,但是遇到了以下错误。
---------------------------------------------------------------------------
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'我的回归模型是
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
)相应的Bayesian优化对象是
learner = BayesianOptimizer(
estimator=clf,
query_strategy=gpr_model["acquisition_function"],
X_training=x_initial, y_training=y_initial.reshape(-1,1)
)最起码的可重现性例子是:
# 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])发布于 2022-04-11 19:22:07
我看到您正在BayesianOptimizer估计器参数中声明一个分类器(clf)。正如您在示例中所看到的,估计器参数是一个回归变量。在你的例子中,它应该是你的模型'SkorchNNR‘。
https://stackoverflow.com/questions/71653204
复制相似问题