Answers.Body: <p>You have got text which have words. Now each of the words have dependency with words that are occurring previously or/and with the ones occurring later. So to capture the "context" in which a particular word occurs is done by sequence models in deep learning. Sequence models that are quite used often are Recurrent Neural Networks (RNN), Gated Recurrent Units (GRU) and Long Short Term Memory (LSTM) listed in the order of their functionality and complexity. To know more about them visit Jonathan Hui's <a href="https://jhui.github.io/2017/03/15/RNN-LSTM-GRU/" rel="nofollow noreferrer">blog</a>.</p>
  
  <p>Your problem is a classification problem i.e. given a sentence the classifier should tell which chapter and section it belongs to. You can use one of the sequence models to "encode" the sentence as a vector (based on the context of words in it and meaning of sentence) and pass that vector to a fully-connected neural network that has a softmax layer at the end to tell which chapter and section the text belongs to. The number of classes you are having are <code>C * S</code> (one for each chapter-section pair). From the output of softmax layer you can pick up the top 3 classes that got the highest probability for a sentence.</p>
  
  <p>Doc2Vec converts a paragraph (can also be a sentence) into a vector representation. The Doc2Vec model has been trained over a huge corpus of text to be able to capture the context of a text and represent it as a high dimensional vector.</p>
  
  <p>The corpus of text that you have, is from a probability distribution is quite different from that used for training Doc2Vec model. Sequence models (mentioned above) can learn that probability distribution for your text corpus exclusively. In other words, you have a tailor-made representation for your text so that the context can be understood much better.</p>
  
  <p>That is why I suggested you to use sequence models. You can also compare the performance obtained by using pretrained Doc2Vec as text representation with that obtained by using one of the sequence models.</p>
  
  <p>To know about the effectiveness of RNN visit Andrej Karpathy's <a href="http://karpathy.github.io/2015/05/21/rnn-effectiveness/" rel="nofollow noreferrer">blog</a>. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T01:49:41.033Z
Answers.Text: I have another question. As far as I know there are pretrained models for Word2Vec, e.g., Google's Word2Vec or Standford Univ's GloVe. We can download those models and use it. However, libraries such as Keras/Gensim provide with  means for Word2Vec using which we can train our own model by feeding with labeled sentences from our corpus. Also, Word2Vec (shallow) neural network is well known and proved to be efficient (in the scientific circles). The same for Doc2Vec.
Answers.UserDisplayName: HardFork
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T01:49:50.683Z
Answers.Text: So, wouldn't it be better to exploit the underlying Doc2Vec neural network to train my model using my domain specific corpus? Or should I try different NN architectures of RNN, LSTM and etc and later compare them? I mean should I spend my time on what people have already proved?
Answers.UserDisplayName: HardFork
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-21T05:22:49.393Z
Answers.Text: Its worth doing both so that you can compare the extent to which neural nets in Doc2Vec and RNNs can model dependencies between words.
Answers.UserDisplayName: varsh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-20T05:21:34.360Z
Answers.LastActivityDate: 2018-06-20T05:21:34.360Z
Answers.UserDisplayName: varsh
Answers.Body: <p>The easiest way would probably be not to use machine learning at all. Create an inverted index of words (ie for each word occurrence record the chapter and section), and then use an information retrieval algorithm (eg <a href="https://en.wikipedia.org/wiki/Tf%E2%80%93idf" rel="nofollow noreferrer">TF-IDF</a>) to find the section that best matches your question.</p>
  
  <p>This will be more efficient (no huge models to train) and more transparent (you can easily see why a particular section has been selected).</p>
  
  <p>Additional steps you can take to adjust performance is stemming/lemmatising of words, and adding synonym lists (eg in a car manual application you might want to treat <em>trunk</em> and <em>boot</em> as the same word).</p>
  
  <p>I have used that approach recently in a commercial project, and it performed well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-20T17:08:25.933Z
Answers.Text: Thank you very much for your suggestion. I will try it. Unfortunately cannot upvote since I do not have enough reputation.
Answers.UserDisplayName: HardFork
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-22T20:52:44.280Z
Answers.Text: I create tf-idf scores for each word for each document. Sklearn's TfidfVectorizer allows to do it in a couple of lines. But it creates scores for words, not for sentences. As I described, my task is classify a particular sentence. So, I need to come up with a method that computes similar score for sentence. For example given a sentence S, I want to compute C scores, one score for each class (chapter in my case). Do you know any approach how to go about it? Any reference? A naive approach is to sum up scores of each word or compute average over all words in the sentence.
Answers.UserDisplayName: HardFork
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-25T08:18:59.923Z
Answers.Text: @B.K. Yes, I'd sum scores or average, depending on what works better in that particular application. Adding favours longer sequences, so if that is a problem you could average instead.
Answers.UserDisplayName: Oliver Mason
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-20T09:56:16.090Z
Answers.LastActivityDate: 2018-06-20T09:56:16.090Z
Answers.UserDisplayName: Oliver Mason
Body: <p>I have a corpus, say an instruction manual. The text in this manual is grouped into chapters and each chapter is split up into sections. For example, Chapter 1/Section 1, Chapter 1/Section 2 and so on.
  Assume the corpus has C chapters and each chapter has S sections. My goal is, given a sentence or question, to classify this sentence/question. In other words I want to compute three most probable chapters to which this sentence or question belongs to. 
  I tried MultinomialNB model using sklealrn, but it did not give me the desired result. I want to try another approach, for example using a Neural Network and compare it with the MultinomialNB model. I have Googled and found Doc2Vec but haven't tried yet. </p>
  
  <p>Can anyone suggest a better or another possible approach so that I could try and compare? What is the standard approach to such kind of problem? </p>
  
Tags: <machine-learning><deep-learning><classification><natural-language-processing>
Title: Question classification according to chapters
UserDisplayName: HardFork
ThreadText: # Question classification according to chapters
  
  (Asked by: username_0 on 2018-06-20)
  
  I have a corpus, say an instruction manual. The text in this manual is grouped into chapters and each chapter is split up into sections. For example, Chapter 1/Section 1, Chapter 1/Section 2 and so on.
  Assume the corpus has C chapters and each chapter has S sections. My goal is, given a sentence or question, to classify this sentence/question. In other words I want to compute three most probable chapters to which this sentence or question belongs to.
  I tried MultinomialNB model using sklealrn, but it did not give me the desired result. I want to try another approach, for example using a Neural Network and compare it with the MultinomialNB model. I have Googled and found Doc2Vec but haven't tried yet.
  
  Can anyone suggest a better or another possible approach so that I could try and compare? What is the standard approach to such kind of problem?
  
  **Tags:** machine-learning, deep-learning, classification, natural-language-processing
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2018-06-20. Score: 1 | Accepted answer
  
  You have got text which have words. Now each of the words have dependency with words that are occurring previously or/and with the ones occurring later. So to capture the "context" in which a particular word occurs is done by sequence models in deep learning. Sequence models that are quite used often are Recurrent Neural Networks (RNN), Gated Recurrent Units (GRU) and Long Short Term Memory (LSTM) listed in the order of their functionality and complexity. To know more about them visit Jonathan Hui's [blog](https://jhui.github.io/2017/03/15/RNN-LSTM-GRU/).
  
  Your problem is a classification problem i.e. given a sentence the classifier should tell which chapter and section it belongs to. You can use one of the sequence models to "encode" the sentence as a vector (based on the context of words in it and meaning of sentence) and pass that vector to a fully-connected neural network that has a softmax layer at the end to tell which chapter and section the text belongs to. The number of classes you are having are `C * S` (one for each chapter-section pair). From the output of softmax layer you can pick up the top 3 classes that got the highest probability for a sentence.
  
  Doc2Vec converts a paragraph (can also be a sentence) into a vector representation. The Doc2Vec model has been trained over a huge corpus of text to be able to capture the context of a text and represent it as a high dimensional vector.
  
  The corpus of text that you have, is from a probability distribution is quite different from that used for training Doc2Vec model. Sequence models (mentioned above) can learn that probability distribution for your text corpus exclusively. In other words, you have a tailor-made representation for your text so that the context can be understood much better.
  
  That is why I suggested you to use sequence models. You can also compare the performance obtained by using pretrained Doc2Vec as text representation with that obtained by using one of the sequence models.
  
  To know about the effectiveness of RNN visit Andrej Karpathy's [blog](http://karpathy.github.io/2015/05/21/rnn-effectiveness/).
  
  > **Comments:**
  > **username_0**: I have another question. As far as I know there are pretrained models for Word2Vec, e.g., Google's Word2Vec or Standford Univ's GloVe. We can download those models and use it. However, libraries such as Keras/Gensim provide with  means for Word2Vec using which we can train our own model by feeding with labeled sentences from our corpus. Also, Word2Vec (shallow) neural network is well known and proved to be efficient (in the scientific circles). The same for Doc2Vec.  
  > **username_0**: So, wouldn't it be better to exploit the underlying Doc2Vec neural network to train my model using my domain specific corpus? Or should I try different NN architectures of RNN, LSTM and etc and later compare them? I mean should I spend my time on what people have already proved?  
  > **username_1**: Its worth doing both so that you can compare the extent to which neural nets in Doc2Vec and RNNs can model dependencies between words.  
  
  ---
  
  ### Answer by username_2 on 2018-06-20. Score: 1
  
  The easiest way would probably be not to use machine learning at all. Create an inverted index of words (ie for each word occurrence record the chapter and section), and then use an information retrieval algorithm (eg [TF-IDF](https://en.wikipedia.org/wiki/Tf%E2%80%93idf)) to find the section that best matches your question.
  
  This will be more efficient (no huge models to train) and more transparent (you can easily see why a particular section has been selected).
  
  Additional steps you can take to adjust performance is stemming/lemmatising of words, and adding synonym lists (eg in a car manual application you might want to treat *trunk* and *boot* as the same word).
  
  I have used that approach recently in a commercial project, and it performed well.
  
  > **Comments:**
  > **username_0**: Thank you very much for your suggestion. I will try it. Unfortunately cannot upvote since I do not have enough reputation.  
  > **username_0**: I create tf-idf scores for each word for each document. Sklearn's TfidfVectorizer allows to do it in a couple of lines. But it creates scores for words, not for sentences. As I described, my task is classify a particular sentence. So, I need to come up with a method that computes similar score for sentence. For example given a sentence S, I want to compute C scores, one score for each class (chapter in my case). Do you know any approach how to go about it? Any reference? A naive approach is to sum up scores of each word or compute average over all words in the sentence.  
  > **username_2**: @B.K. Yes, I'd sum scores or average, depending on what works better in that particular application. Adding favours longer sequences, so if that is a problem you could average instead.  
  
  ---
  
  