modAL-python / modAL

A modular active learning framework for Python
https://modAL-python.github.io/
MIT License
2.21k stars 324 forks source link

TypeError: <class 'torch.Tensor'> datatype is not supported #128

Open Khalimat opened 3 years ago

Khalimat commented 3 years ago

Hello, I am trying to reproduce code here. but getting the following error when execute query_idx, query_instance = learner.query(X_pool, n_instances=100)


TypeError Traceback (most recent call last)

in 2 for idx in range(n_queries): 3 print('Query no. %d' % (idx + 1)) ----> 4 query_idx, query_instance = learner.query(X_pool, n_instances=100) 5 learner.teach( 6 X=X_pool[query_idx], y=y_pool[query_idx], only_new=True, ~/anaconda3/lib/python3.8/site-packages/modAL/models/base.py in query(self, X_pool, *query_args, **query_kwargs) 259 return query_result 260 --> 261 return query_result, retrieve_rows(X_pool, query_result) 262 263 def score(self, X: modALinput, y: modALinput, **score_kwargs) -> Any: ~/anaconda3/lib/python3.8/site-packages/modAL/utils/data.py in retrieve_rows(X, I) 99 return np.array(X)[I].tolist() 100 --> 101 raise TypeError('%s datatype is not supported' % type(X)) 102 103 TypeError: datatype is not supported Linux, Python 3.8.5 Env: skorch == '0.10.0' torch == '1.7.1' torchvision == '0.8.2' Could you please give me advice how to fix the error. Maybe I am dooing something wrong or need other versions of packages. Thanks!
cliangyu commented 3 years ago

Got the same wrror here Tensor datatype was not supported, but ndarray works for TF

cliangyu commented 3 years ago

Added X = X.detach().cpu().numpy() y = y.detach().cpu().numpy()

after

X, y = next(iter(dataloader))

, and then it worked. The cause is tensor datatype is not recognized by learner.

Sar1636 commented 2 years ago

In the learner.teach, I modified the y=y_pool[query_idx] to the code below learner.teach(X = X, y = torch.from_numpy(y).type(torch.LongTensor), only_new = True)

image

I added the below code inside the _add_training_data function

self.X_training = self.X_training.detach().cpu().numpy()
self.y_training = self.y_training.detach().cpu().numpy()

image

then at the end of the teach function i added this

self.X_training = torch.from_numpy(self.X_training)
self.y_training = torch.from_numpy(self.y_training)

image