TypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarray

Posted

技术标签:

【中文标题】TypeError: take(): argument \'index\' (position 1) must be Tensor, not numpy.ndarray【英文标题】:TypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarrayTypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarray 【发布时间】:2019-12-02 09:35:17 【问题描述】:

我是 pytorch 的新手。我正在尝试进行交叉验证,并找到了 skorch 库,它允许用户将 sklearn 函数与 Torch 模型一起使用。所以,我定义了一个神经网络类:

torch.manual_seed(42)

class Netcross(nn.Module):

    def __init__(self):
        super().__init__()
        self.fc1 = nn.Linear(5,30)
        self.sig1 = nn.Tanh()
        #self.dout = nn.Dropout(0.2)
        self.fc2 = nn.Linear(30,30)
        self.sig2 = nn.Sigmoid()
        self.out = nn.Linear(30, 1)
        self.out_act = nn.Sigmoid()
        #self.fc1.weight = torch.nn.Parameter(torch.rand(50,5))

    def forward(self, x):
        x = self.fc1(x)
        x = self.sig1(x)
        #x = self.dout(x)
        x = self.fc2(x)
        x = self.sig2(x)
        x = self.out(x)
        y = self.out_act(x)

        return y

crossnet1 = NeuralNet(
    Netcross,
    max_epochs = 5,
    criterion=torch.nn.BCELoss,
    #user defined coeff.
    callbacks = [epoch_acc, epoch_f1, epoch_phi], 
    optimizer=torch.optim.SGD,
    optimizer__momentum=0.9,
    lr=0.85,
)
inputs = Variable(x_traintensor)
labels = Variable(y_traintensor)

crossnet1.fit(inputs, labels)

到目前为止一切都很好,该函数返回了可靠的结果,没有任何错误。当我尝试使用 GridSearchCV 函数时出现问题:

from sklearn.model_selection import GridSearchCV

param_grid = 'max_epochs':[5, 10, 20], 
              'lr': [0.1, 0.65, 0.8],
             

gs = GridSearchCV(estimator = crossnet1, param_grid = param_grid, refit = False, cv = 3, scoring = "accuracy")

gs.fit(inputs, labels)

我收到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-41-e1f3dbd9a2b0> in <module>
      3 labels1 = torch.from_numpy(np.array(labels))
      4 
----> 5 gs.fit(inputs1, labels1)

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
    720                 return results_container[0]
    721 
--> 722             self._run_search(evaluate_candidates)
    723 
    724         results = results_container[0]

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in _run_search(self, evaluate_candidates)
   1189     def _run_search(self, evaluate_candidates):
   1190         """Search all candidates in param_grid"""
-> 1191         evaluate_candidates(ParameterGrid(self.param_grid))
   1192 
   1193 

~\Anaconda3\lib\site-packages\sklearn\model_selection\_search.py in evaluate_candidates(candidate_params)
    709                                for parameters, (train, test)
    710                                in product(candidate_params,
--> 711                                           cv.split(X, y, groups)))
    712 
    713                 all_candidate_params.extend(candidate_params)

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self, iterable)
    915             # remaining jobs.
    916             self._iterating = False
--> 917             if self.dispatch_one_batch(iterator):
    918                 self._iterating = self._original_iterator is not None
    919 

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in dispatch_one_batch(self, iterator)
    757                 return False
    758             else:
--> 759                 self._dispatch(tasks)
    760                 return True
    761 

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in _dispatch(self, batch)
    714         with self._lock:
    715             job_idx = len(self._jobs)
--> 716             job = self._backend.apply_async(batch, callback=cb)
    717             # A job can complete so quickly than its callback is
    718             # called before we get here, causing self._jobs to

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in apply_async(self, func, callback)
    180     def apply_async(self, func, callback=None):
    181         """Schedule a func to be run"""
--> 182         result = ImmediateResult(func)
    183         if callback:
    184             callback(result)

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\_parallel_backends.py in __init__(self, batch)
    547         # Don't delay the application, to avoid keeping the input
    548         # arguments in memory
--> 549         self.results = batch()
    550 
    551     def get(self):

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in __call__(self)
    223         with parallel_backend(self._backend, n_jobs=self._n_jobs):
    224             return [func(*args, **kwargs)
--> 225                     for func, args, kwargs in self.items]
    226 
    227     def __len__(self):

~\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py in <listcomp>(.0)
    223         with parallel_backend(self._backend, n_jobs=self._n_jobs):
    224             return [func(*args, **kwargs)
--> 225                     for func, args, kwargs in self.items]
    226 
    227     def __len__(self):

~\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, return_n_test_samples, return_times, return_estimator, error_score)
    516     start_time = time.time()
    517 
--> 518     X_train, y_train = _safe_split(estimator, X, y, train)
    519     X_test, y_test = _safe_split(estimator, X, y, test, train)
    520 

~\Anaconda3\lib\site-packages\sklearn\utils\metaestimators.py in _safe_split(estimator, X, y, indices, train_indices)
    201             X_subset = X[np.ix_(indices, train_indices)]
    202     else:
--> 203         X_subset = safe_indexing(X, indices)
    204 
    205     if y is not None:

~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in safe_indexing(X, indices)
    214                                    indices.dtype.kind == 'i'):
    215             # This is often substantially faster than X[indices]
--> 216             return X.take(indices, axis=0)
    217         else:
    218             return X[indices]

TypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarray

怎么了?

【问题讨论】:

如果它对你有帮助,请确保你接受和/或支持答案:) 也看看这个:***.com/help/someone-answers 【参考方案1】:

将您的inputlabels 更改为np.ndarray(参见示例here)。

skorch 自动需要时,这些将被转换为torch.Tensor

总之改变你的

inputs = Variable(x_traintensor)
labels = Variable(y_traintensor)

到:

inputs = x_traintensor.numpy() # assuming x is torch.Tensor
labels = y_traintensor.numpy() # assuming y is torch.Tensor

顺便说一句。 torch.Variable 已弃用,您应该使用 torch.Tensor(data, requires_grad=True)。在这种情况下,输入和标签不需要渐变,因此Variable 更加不合适。

【讨论】:

以上是关于TypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarray的主要内容,如果未能解决你的问题,请参考以下文章

TypeError: take(): argument 'index' (position 1) must be Tensor, not numpy.ndarray

单元测试:TypeError:无法读取未定义的属性“管道”

PySpark DataFrame 上的求和操作在类型正常时给出 TypeError

mongodb 前人埋坑的奇怪问题

如何在 PySpark 中仅打印 DataFrame 的某一列?

Laravel 新用户注册通过 API POST 失败