ValueError : While executing a shallow neural network having tfidf as featured

neural_network

#1

I have used tfidf on a text -> trainDF[‘text’] and applied on train_x, valid_x
which are created from trainDF[‘text’] for training and validation

# ngram level tf-idf 
tfidf_vect_ngram = TfidfVectorizer(analyzer='word', token_pattern=r'\w{1,}', 
ngram_range=(2,3), max_features=5000)
tfidf_vect_ngram.fit(trainDF['text'])
xtrain_tfidf_ngram =  tfidf_vect_ngram.transform(train_x)
xvalid_tfidf_ngram =  tfidf_vect_ngram.transform(valid_x)

Here a function is defined as

def  create_model_architecture(input_size):
    # create input layer 
    input_layer = layers.Input((input_size, ), sparse=True)   
    # create hidden layer
    hidden_layer = layers.Dense(100, activation="relu")(input_layer)  
    # create output layer
    output_layer = layers.Dense(1, activation="sigmoid")(hidden_layer)
    classifier = models.Model(inputs = input_layer, outputs = output_layer)
    classifier.compile(optimizer=optimizers.Adam(), loss='binary_crossentropy')
    return classifier 

Here the function is called :

   classifier = create_model_architecture(xtrain_tfidf_ngram.shape[1])
   accuracy = train_model(classifier, xtrain_tfidf_ngram, train_y, xvalid_tfidf_ngram, is_neural_net=True)
   print "NN, Ngram Level TF IDF Vectors",  accuracy

But its showing the following error :

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-51-07b521a52f02> in <module>()
----> 1 classifier = create_model_architecture(xtrain_tfidf_ngram.shape[1])
      2 accuracy = train_model(classifier, xtrain_tfidf_ngram, train_y, xvalid_tfidf_ngram, is_neural_net=True)
      3 print "NN, Ngram Level TF IDF Vectors",  accuracy

<ipython-input-50-4f1bbefd3e59> in create_model_architecture(input_size)
      4 
      5     # create hidden layer
----> 6     hidden_layer = layers.Dense(100, activation="relu")(input_layer)
      7 
      8     # create output layer

/home/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.pyc in __call__(self, inputs, **kwargs)
    573                 # Raise exceptions in case the input is not compatible
    574                 # with the input_spec specified in the layer constructor.
--> 575                 self.assert_input_compatibility(inputs)
    576 
    577                 # Collect input shapes to build layer.

/home/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/engine/topology.pyc in assert_input_compatibility(self, inputs)
    446                                  'Received type: ' +
    447                                  str(type(x)) + '. Full input: ' +
--> 448                                  str(inputs) + '. All inputs to the layer '
    449                                  'should be tensors.')
    450 

ValueError: Layer dense_16 was called with an input that isn't a symbolic tensor. Received type: <class 'theano.sparse.basic.SparseVariable'>. Full input: [SparseVariable{csr,float32}]. All inputs to the layer should be tensors.

I was wondering how to resolve the error.


#2

Hi @shan4224

Looks like the input you have provided is a sparse matrix. Try using the todense() function on the input.