vu-aml / adlib

Game-Theoretic Adversarial Machine Learning Library
MIT License
57 stars 16 forks source link

dtype error thrown when running demo #5

Closed jzyee closed 6 years ago

jzyee commented 6 years ago

I have install all listed dependencies in the readme file and when i run the demo code i get the following error:


AttributeError Traceback (most recent call last) C:\Users\Anaconda3\lib\site-packages\scipy\sparse\sputils.py in getdtype(dtype, a, default) 115 try: --> 116 newdtype = a.dtype 117 except AttributeError:

AttributeError: 'list' object has no attribute 'dtype'

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)

in () 3 learning_model = svm.SVC(probability=True, kernel='linear') 4 learner1 = SimpleLearner(learning_model, training_data) ----> 5 learner1.train() C:\Users\Documents\python\adlib-master\adlib-master\learners\simple_learner.py in train(self) 26 if self.training_instances is None: 27 raise ValueError('Must set training instances before training') ---> 28 self.model.train(self.training_instances) 29 30 def predict(self, instances): C:\Users\Documents\python\adlib-master\adlib-master\learners\models\sklearner.py in train(self, instances) 36 """ 37 if isinstance(instances, List): ---> 38 (y, X) = sparsify(instances) 39 self.learner.fit(X.toarray() ,y) 40 else: C:\Users\Documents\python\adlib-master\adlib-master\data_reader\operations.py in sparsify(instances) 86 a = len(data) 87 b = len(indices) ---> 88 return (labels, csr_matrix((data, indices, indptr), shape=(len(instances), num_features))) 89 90 C:\Users\Anaconda3\lib\site-packages\scipy\sparse\compressed.py in __init__(self, arg1, shape, dtype, copy) 54 self.indices = np.array(indices, copy=copy, dtype=idx_dtype) 55 self.indptr = np.array(indptr, copy=copy, dtype=idx_dtype) ---> 56 self.data = np.array(data, copy=copy, dtype=getdtype(dtype, data)) 57 else: 58 raise ValueError("unrecognized %s_matrix constructor usage" % C:\Users\Anaconda3\lib\site-packages\scipy\sparse\sputils.py in getdtype(dtype, a, default) 119 newdtype = np.dtype(default) 120 else: --> 121 raise TypeError("could not interpret data type") 122 else: 123 newdtype = np.dtype(dtype) TypeError: could not interpret data type
JasonJiazhiZhang commented 6 years ago

Can you report your scipy version? The demo should work on recent versions of scipy (like v1.0). Specificly, in scipy\sparse\compressed.py, self.data = np.array(data, copy=copy, dtype=getdtype(dtype, data)) is changed to self.data = np.array(data, copy=copy, dtype=dtype) and should solve your issue.

jzyee commented 6 years ago

in conda list it says its 0.16.0

however when i run import scipy and run scipy.version i get '1.0.0' so i believe it is scipy v1.0.

i tried doing as mention above for the change in code to compressed.py but it still gave the same error so I've done this: training_data = np.array(loaddataset(training)) testing_data = np.array(loaddataset(testing))

and now the error is :

NotImplementedError Traceback (most recent call last)

in () 3 learning_model = svm.SVC(probability=True, kernel='linear') 4 learner1 = SimpleLearner(learning_model, training_data) ----> 5 learner1.train() C:\Users\Documents\python\adlib-master\adlib-master\learners\simple_learner.py in train(self) 26 if self.training_instances is None: 27 raise ValueError('Must set training instances before training') ---> 28 self.model.train(self.training_instances) 29 30 def predict(self, instances): C:\Users\Documents\python\adlib-master\adlib-master\learners\models\sklearner.py in train(self, instances) 39 self.learner.fit(X.toarray() ,y) 40 else: ---> 41 self.learner.fit(instances.data[0], instances.data[1]) 42 43 def predict(self, instances): NotImplementedError: memoryview: format O not supported
JasonJiazhiZhang commented 6 years ago

That won't work since load_dataset(training_) returns a list of Instance objects, which contain information such as labels, and cannot be turned into np arrays directly.

To solve your previous problem, I suggest you check the kernel environment of Jupyter Notebook, assuming you are running the demo on it. It is likely that the kernel you are using has scipy v0.16.0 installed, which is outdated for this project.

jzyee commented 6 years ago

i restarted the kernel and everything worked out, thanks!