Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

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

$
0
0

@shan4224 wrote:

https://www.analyticsvidhya.com/blog/2018/04/a-comprehensive-guide-to-understand-and-implement-text-classification-in-python/

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)
in ()
----> 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

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.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles