roamanalytics / mittens

A fast implementation of GloVe, with optional retrofitting
Apache License 2.0
243 stars 31 forks source link

TypeError: exponent must be an integer #5

Closed michaelcapizzi closed 6 years ago

michaelcapizzi commented 6 years ago

When trying to build the mittens model, I get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-30-814206757e5a> in <module>()
      2     cooccur_matrix,
      3     vocab=vocab,
----> 4     initial_embedding_dict=vector_dict
      5 )

~/anaconda3/envs/mittens/lib/python3.6/site-packages/mittens/mittens_base.py in fit(self, X, vocab, initial_embedding_dict, fixed_initialization)
     78             X, vocab, initial_embedding_dict
     79         )
---> 80         weights, log_coincidence = self._initialize(X)
     81         return self._fit(X, weights, log_coincidence,
     82                          vocab=vocab,

~/anaconda3/envs/mittens/lib/python3.6/site-packages/mittens/mittens_base.py in _initialize(self, coincidence)
    143         self.n_words = coincidence.shape[0]
    144         bounded = np.minimum(coincidence, self.xmax)
--> 145         weights = (bounded / float(self.xmax)) ** self.alpha
    146         log_coincidence = log_of_array_ignoring_zeros(coincidence)
    147         return weights, log_coincidence

~/anaconda3/envs/mittens/lib/python3.6/site-packages/numpy/matrixlib/defmatrix.py in __pow__(self, other)
    320 
    321     def __pow__(self, other):
--> 322         return matrix_power(self, other)
    323 
    324     def __ipow__(self, other):

~/anaconda3/envs/mittens/lib/python3.6/site-packages/numpy/matrixlib/defmatrix.py in matrix_power(M, n)
    139         raise ValueError("input must be a square array")
    140     if not issubdtype(type(n), N.integer):
--> 141         raise TypeError("exponent must be an integer")
    142 
    143     from numpy.linalg import inv

TypeError: exponent must be an integer

I was able to identify that the error is because self.alpha is set to 0.75. If I set that to 1.0 I do not get the error (though I will obviously not be weighting things as intended).

Note: I am using numpy 1.14.5

michaelcapizzi commented 6 years ago

It was my error! I was using a numpy.matrix for my cooccurrence matrix ("densified" from a scipy sparse matrix) instead of a numpy.ndarray.

Now everything works as expected.