simec icon indicating copy to clipboard operation
simec copied to clipboard

AttributeError: 'numpy.ndarray' object has no attribute 'tocoo' when sparse_inputs=True

Open rwgardner2 opened this issue 5 years ago • 3 comments

Simple, relevant snippet of code

   N_DIMS_EMBEDDING = 3
   m_sim = np.array([(1.0, .7, .3),                                                                                                    
                     (.7, 1.0, .2),                                                                                                    
                     (.3, .2, 1.0)])                                                                                                   
   #define feature matrix - one-hot encoding                                                                                           
   m_feat = np.array([(1.0, 0.0, 0.0),                                                                                                 
                      (0.0, 1.0, 0.0),                                                                                                 
                      (0.0, 0.0, 1.0)])                                                                                                
                                                                                                                                       
   #compute the embedding                                                                                                              
   simec = SimilarityEncoder(m_sim.shape[0], N_DIMS_EMBEDDING, m_sim.shape[0], sparse_inputs=True)                                     
                                                                                                                                       
   simec.fit(m_feat, m_sim, epochs=1000)    

It gives the error

Traceback (most recent call last):
  File "simec_test_reduced.py", line 74, in <module>
    simec.fit(m_feat, m_sim, epochs=1000)
  File "/home/rgardner/projects/explore_learn/ai2thor_utils/simec/simec.py", line 284, in fit
    self.model.fit(X, S, epochs=epochs, batch_size=batch_size, verbose=verbose)
  File "/home/rgardner/projects/explore_learn/simec/env/lib/python3.6/site-packages/keras/engine/training.py", line 1239, in fit
    validation_freq=validation_freq)
  File "/home/rgardner/projects/explore_learn/simec/env/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 196, in fit_loop
    outs = fit_function(ins_batch)
  File "/home/rgardner/projects/explore_learn/simec/env/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 3262, in __call__
    sparse_coo = value.tocoo()
AttributeError: 'numpy.ndarray' object has no attribute 'tocoo'

The error only happens when I set sparse_inputs=True in the SimilarityEncoder constructor.

I think it may go away with newer versions of Keras and Tensorflow (so maybe you don't care about it).

I'm using:

Keras (2.3.1) Keras-Applications (1.0.8) Keras-Preprocessing (1.1.2)

tensorflow (1.14.0) tensorflow-estimator (1.14.0)

rwgardner2 avatar Jun 24 '20 02:06 rwgardner2

Yes, this goes away with

Keras 2.4.2 Keras-Applications 1.0.8 Keras-Preprocessing 1.1.2

tensorflow 2.2.0 tensorflow-estimator 2.2.0

For this reason, I don't care about the issue (It could just be closed) except there is another problem when using those versions of keras and tensorflow, so I needed to use the old versions. (See separate submitted issue.)

rwgardner2 avatar Jun 24 '20 02:06 rwgardner2

Not sure why this is related to the keras versions but in general when you set sparse_inputs=True you should give the model a scipy sparse matrix, not a numpy array as input. But yeah, I'll have a look and at least improve the documentation.

cod3licious avatar Jun 26 '20 09:06 cod3licious

Ah, I was wondering about something like that. I was thrown of by the effects of the package versions. Thanks.

rwgardner2 avatar Jun 26 '20 13:06 rwgardner2