Tensorflow Text Classification - Python Deep Learning - Source Dexter

Join the Sourcedexter Community

https://t.me/source_dexter  Community to talk about AI, machine learning, blockchain and more!


  1. akshay pai
    October 13, 2017 @ 12:28 pm

    A guide to building a contextual Chatbot with tensorflow: https://chatbotsmagazine.com/contextual-chat-bots-with-tensorflow-4391749d0077


    • Rachit
      June 22, 2018 @ 11:00 am

      same task for text classification i am trying using R can u please suggest me the code for the same…Thanks in advance


      • akshay pai
        June 27, 2018 @ 10:17 pm

        Sorry, Rachit, I have no idea about R, so I won’t be able to help you.


    • Rachit
      June 22, 2018 @ 11:02 am

      Even I tried the same with python but showing number of errors , i am not that much comfortable with python , can u please suggest me for R


  2. Kai Wendt
    February 19, 2018 @ 2:09 pm

    Hey, great tutorial but is there any possibility that you append the code with the accuracy of the model.


    • akshay pai
      May 16, 2018 @ 11:40 am

      Hey, that is a possibility, I will try to add that sometime. If you have done that already, then please do raise a PR on github.


  3. Anders Fåk
    April 5, 2018 @ 4:57 pm

    Thanks for an excellent example.
    This got me started with text classification on my text data.

    In your sample the training and predict section is in the same python script…. How would you define and load that model for predictions in a separate standalone script?


    • akshay pai
      May 16, 2018 @ 11:41 am

      You can save the model, using tflearn and then load it in another file. Once loaded, you can use the same methods as in this script to predict


  4. Sean O'Keefe
    April 9, 2018 @ 6:12 am

    Love this post! I’ve been trying to figure out how to do with with TF for some time.


  5. Senso
    April 10, 2018 @ 5:56 pm

    Most examples for NLP in TensorFlow involve labels which are already given by the developer who creates and trains the TensorFlow model. I am beginner in this field and this seems to fall under the category of supervised learning. Is this correct?

    Now I would like to have a training model with TensorFlow where I don’t give it any labels. It should classify sentences itself. There is a huge amount of data where labels are not clear. The hope is that TensorFlow finds clusters in the data which can be used for the labeling and classifying the sentences.


    • akshay pai
      May 16, 2018 @ 11:43 am

      Yes, this most certainly falls into the supervised learning category. I haven’t worked on unsupervised learning with tensorflow yet. But i will surely update my blog when I do so.


  6. Ramprassad
    April 18, 2018 @ 1:07 pm

    thatns for useful article. Can you try to port the same to golang?


    • akshay pai
      May 16, 2018 @ 11:37 am

      Sorry, I am not that good at golang, If you have done it, then please do raise a pull request.


  7. Ramprassad
    April 19, 2018 @ 7:49 pm

    i can test this as it is.

    But i can’t load saved model using tensorflow not tflearn.

    i understand that model is trained using tflearn and hence can be loaded in tflearn only.

    Please try to port this completely to tensorflow.


  8. Prateek
    June 7, 2018 @ 8:51 pm

    Nice article. I am using this in a personal fun project. I noticed the following.

    1. There are issues while restoring model in new code , avoiding to prepare data is cumbersome task.
    2. tflearn github is almost unsupported for issues.
    3. Even though model is restored with lot of modifications, its taking 3-4 seconds to run the program which is too slow.
    4. predictions are different every time for same input (I loaded same saved model in each run without changing trained data) Is this because I added parameter restore=False on net = tflearn.fully_connected(net, 8, restore=False) to avoid shape mismatch exceptions on reloading the model?


    • Akshay Pai
      June 7, 2018 @ 8:54 pm

      These issues that you are facing is due to the version mismatch of tensorflow or some errors with the tensors that is being formed. I am using this extensively in many places and I can assure you that the results are stable and the performance is super fast. it in milli-seconds that I get results in.


      • Prateek
        June 7, 2018 @ 9:29 pm

        I just cloned github code of this and retested. I dont see any issue with version issue in log?

        Regarding this is slow : I have large model , with 224 labels and 1600 total sentences. tflearn restoring model with code in different program is not so easy. Even with few lines of code , we still need to have reference to `words`

        I guess one can use different stemmer , I see stemmed words look weird. How about `nltk.stem.wordnetWordNetLemmatizer`

        I just want to fix this issue of different predictions each time. I couldnt get same issue being mentioned anywhere in any blog either .

        And tflearn does not have method to update the model with new data, we have call model.fit with updated data and them model.save again which will retrain the whole model and takes a lot of time.


        • Akshay Pai
          June 7, 2018 @ 9:33 pm

          You can certainly try the nltk.stem.wordnetWordNetLemmatizer . Even with that large dataset, I don’t think it should be an issue for the program to either run slowly or not to be stable. I will try to look at this when I shave some time.


    • Prateek
      June 7, 2018 @ 9:19 pm

      “Output predictions vary every time with the mentioned code as well without any changes. I think this is weird.


  9. rachit
    June 22, 2018 @ 12:16 pm

    Error importing tensorflow. Unless you are using bazel,
    you should not try to import tensorflow from its source directory;
    please exit the tensorflow source tree, and relaunch your python interpreter
    from there.


  10. Desmond
    June 26, 2018 @ 8:33 am

    Hi, that’s a great tutorial. However, do you mind to provide more strings in the data.json? I realized that when I add more string to perform the machine learning, the accuracy went downhills.


    • akshay pai
      June 27, 2018 @ 10:21 pm

      Could you send me the json that you are using? I feel it’s not due to more number of strings that the accuracy has gone down. I feel it might be due to the way in which you have categorized the data. Sometimes, categorization does not allow the neural network to correctly classify the input data.


  11. John
    June 28, 2018 @ 8:45 pm


    I downloaded source code from your Github repo. But When I running “classify_text.py” and then it gives below error message. How can I fix it ?

    TypeError: category() argument 1 must be unicode, not str


  12. Kornel
    July 8, 2018 @ 7:24 pm


    thanks for the great tutorial. I combined it with your chatbot tutorial and everything works but the prediction takes very long (20-30 seconds). I have trained the model with 3 categories with about 5000 words each category). The pickle file has 104MB. Is there a way to make the prediction faster?


  13. Simon
    July 13, 2018 @ 10:35 pm

    Thank you for posting


  14. Dipti Pawar
    August 1, 2018 @ 3:18 pm

    Thanks.. Nice Tutorial.


  15. T. Hossain
    August 6, 2018 @ 12:05 am

    Hi, is it possible to share full code with library imports and stuff? Having bunch of errors trying to achieve result. much appreciated.


    • akshay pai
      August 7, 2018 @ 10:29 pm

      Hi, at the end of the article, I have shared the link to the GitHub project from which you will get all the code. Have you checked that? Let me know if you tried it and it didn’t work.


  16. akshay pai
    August 7, 2018 @ 10:31 pm

    Thank you very much.


  17. Nuha
    September 3, 2018 @ 12:25 pm

    Thank You very very much you’ve helped me a lot. I faced some errors regarding missing modules but I fixed them and now it works well. I really appreciate people helping others.
    I have a question, how can I know the supported languages in the preprocessing methods? (specially Arabic Language) and thanks again


    • akshay pai
      September 3, 2018 @ 10:45 pm

      Hello Nuha,
      I’m glad to know that this has helped you. With regards to supported languages, I haven’t tried running it with languages other than English. But if you observe, the processing uses the Unicode notations which means it can support any given language that we know (Arabic included)

      I hope that you can get it up and running for Arabic 🙂


  18. samira
    September 18, 2018 @ 12:53 pm

    Thank you for your good scripts
    I run this script with excel file as input data and it works finely
    I want to load my model after i save it.
    how can i do it?
    thank you for your reply


  19. Amit Gupta
    December 6, 2018 @ 10:36 am

    Hi akshay, can we use word embedding using word2vec instead of bag of words? Because if there is large corpus than there will be huge number of zeros for a sentence.


    • akshay pai
      December 6, 2018 @ 10:42 am

      Hi Amit,

      WordToVec is designed to convert a word to a vector or a phrase to a vector. When using TF bag of words model, the same is happening internally.
      Though I haven’t used word-to-vec, I know that the existing approach is quote scalable.


  20. Rajesh koredla
    January 4, 2019 @ 12:41 pm

    hi akshay thank u for script it helped me lot ..
    i want to see these predicted probabilities can u do that step for me ,


    i need to see these probabilities for all predicted data and in that i will see it’s taking highest probability is printing cab u do …it will help me lot. thanks


    • akshay pai
      January 10, 2019 @ 8:48 am

      So, you’re looking to get all the probability instead of just the highest right?


      • Rajesh koredlaajesh
        January 10, 2019 @ 1:49 pm

        for x in categories_Process[np.argmax(model_CP_Process.predict([get_tf_record_CP_ProcessDescription(values[1])])*100)]:
        print (x)

        instead of argmax i want read all predicted values , i hope u understand


        • akshay pai
          January 10, 2019 @ 4:09 pm

          okay, so argmax here is giving you the index of the category that has got the highest probability. If you want all the probabilities, then all you gotta do is remove the argmax. So, instead of printing it out like “print(categories[np.argmax(model.predict([get_tf_record(sent_1)]))])” , you will have to just use “print(model.predict([get_tf_record(sent_1)])))” and that will return you the list of probabilities as a numpy array.


  21. Boris
    January 9, 2019 @ 11:09 pm

    I’m trying to run this on python 2.7 but I’m getting this error on line 14: TypeError: must be unicode, not str


    • akshay pai
      January 10, 2019 @ 8:47 am

      That’s a typical Unicode error when the string is not encoded.
      To fix it you have to encode it.
      On line 14, see where the string variable is present and then do a .encode(“utf-8”) it should work then


  22. Boris
    January 11, 2019 @ 1:17 am

    Hi! Great tutorial. How can I prevent the script for training every time I run it? Can I comment some lines like line 13 and 14 so the script can use already saved model?


Leave a Reply

%d bloggers like this: