Answers.Body: <p>I'll answer in a couple of stages.</p>
  <p><em>I feel somewhat lost as to what the input for the NN should look like.</em></p>
  <p>Your choices boil down to two options, each with their own multitude of variants:</p>
  <ol>
  <li><p>Vector Representation: Your input is a vector of the same size as your vocabulary where the elements represent the tokens in the input example. The most basic version of this is a bag-of-words (BOW) encoding with a 1 for each word that occurs in the input example and a 0 otherwise. Some other variants are (normalized) word counts or <a href="http://tfidf.com/" rel="nofollow noreferrer">TF-IDF</a> values. With this representation padding will not be necessary as each example will be encoded as a vector of the same size as the vocabulary. However, it suffers from a variety of issues: the input is high-dimensional and very sparse making learning difficult (as you note), it does not encode word order, and the individual word representations have little (TF-IDF) to no (BOW, counts) semantic information. It also limits your NN architecture to a feed-forward network, as more &quot;interesting&quot; architectures such a RNNs, CNNs, and transformers assume a matrix-like input, described below.</p>
  </li>
  <li><p>Matrix Representation: Here your input representation is a matrix with each row being a vector (i.e. embedding) representation of the token at that index in the input example. How you actually get the pretrained embeddings into the model depends on a number of implementation-specific factors, but <a href="https://stackoverflow.com/questions/49710537/pytorch-gensim-how-to-load-pre-trained-word-embeddings">this stackoverflow question</a> shows how to load embeddings from gensim into PyTorch. Here padding is necessary because the input examples will have variable numbers of tokens. <a href="https://stackoverflow.com/a/52242625/2544396">This stackoverflow answer</a> shows how to add zero padding in PyTorch.
  This representation will be significantly better than the vector representation as it is relatively low-dimensional and non-sparse, it maintains word order, and using pretrained word-embeddings means your model will have access to semantic information. In fact, this last point leads to your next question.</p>
  </li>
  </ol>
  <p><em>Learning word embeddings creates vectors for words that are similar to each other syntax-wise, and I fail to see how that can be used to derive the weight/impact of each word on the target variable in my case.</em></p>
  <p>Word embeddings are based on the assumptions of <a href="https://en.wikipedia.org/wiki/Distributional_semantics" rel="nofollow noreferrer">distributional semantics</a>, the core tenet of which is often quoted as &quot;a word is characterized by the company it keeps&quot;. That is, the meaning of a word <em>is</em> how it relates to other words. In the context of NLP, models can make better decisions because similar words are treated similarly from the get-go.</p>
  <p>For example, say that articles about furry pets get a lot of likes (entirely plausible if you ask me). However, the mentions of furry pets in these articles will be varied, including words like &quot;dog&quot;, &quot;cat&quot;, &quot;chinchilla&quot;, &quot;poodle&quot;, &quot;doggo&quot;, &quot;good boy&quot;, etc. An input representation that treats these mentions as completely distinct (such as BOW) will need to learn individual correlations between each word and the number of likes (that's a lot of learning). A well-trained word embedding, on the other hand, will be able to immediately group these mentions together and learn general correlations between groups of similar words and likes. Fair warning, this is a very imprecise description of why word embeddings work, but I hope it gives you some intuitive understanding.</p>
  <p>Finally, since you're doing regression, make sure you choose your objective function accordingly. Mean squared error would be my first try.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-06T09:35:09.870Z
Answers.Text: Thank you for the detailed answer, this helps a lot
Answers.UserDisplayName: Najati Al-imam
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-05T16:46:44.050Z
Answers.LastActivityDate: 2020-08-05T16:46:44.050Z
Answers.UserDisplayName: primussucks
Body: <p>I am trying to predict the number of likes an article or a post will get using a NN.</p>
  <p>I have a dataframe with ~70,000 rows and 2 columns: &quot;text&quot; (predictor - strings of text) and &quot;likes&quot; (target - continuous int variable). I've been reading on the approaches that are taken in NLP problems, but I feel somewhat lost as to what the input for the NN should look like.</p>
  <p>Here is what I did so far:</p>
  <ol>
  <li>Text cleaning: removing html tags, stop words, punctuation, etc...</li>
  <li>Lower-casing the text column</li>
  <li>Tokenization</li>
  <li>Lemmatization</li>
  <li>Stemming</li>
  </ol>
  <p>I assigned the results to a new column, so now I have &quot;clean_text&quot; column with all the above applied to it. However, I'm not sure how to proceed.</p>
  <p>In most NLP problems, I have noticed that people use word embeddings, but from what I have understood, it's a method used when attempting to predict the next word in a text. Learning word embeddings creates vectors for words that are similar to each other syntax-wise, and I fail to see how that can be used to derive the weight/impact of each word on the target variable in my case.</p>
  <p>In addition, when I tried to generate a word embedding model using the Gensim library, it resulted in more than 50k words, which I think will make it too difficult or even impossible to onehot encode. Even then, I will have to one hot encode each row and then create a padding for all the rows to be of similar length to feed the NN model, but the length of each row in the new column I created &quot;clean_text&quot; varies significantly, so it will result in very big onehot encoded matrices that are kind of redundant.</p>
  <p>Am I approaching this completely wrong? and what should I do?</p>
  
Tags: <neural-networks><machine-learning><deep-learning><natural-language-processing><recurrent-neural-networks>
Title: How to use text as an input for a neural network - regression problem? How many likes/claps an article will get
UserDisplayName: Najati Al-imam
ThreadText: # How to use text as an input for a neural network - regression problem? How many likes/claps an article will get
  
  (Asked by: username_0 on 2020-08-01)
  
  I am trying to predict the number of likes an article or a post will get using a NN.
  
  I have a dataframe with ~70,000 rows and 2 columns: "text" (predictor - strings of text) and "likes" (target - continuous int variable). I've been reading on the approaches that are taken in NLP problems, but I feel somewhat lost as to what the input for the NN should look like.
  
  Here is what I did so far:
  
  1. Text cleaning: removing html tags, stop words, punctuation, etc...
  2. Lower-casing the text column
  3. Tokenization
  4. Lemmatization
  5. Stemming
  
  I assigned the results to a new column, so now I have "clean\_text" column with all the above applied to it. However, I'm not sure how to proceed.
  
  In most NLP problems, I have noticed that people use word embeddings, but from what I have understood, it's a method used when attempting to predict the next word in a text. Learning word embeddings creates vectors for words that are similar to each other syntax-wise, and I fail to see how that can be used to derive the weight/impact of each word on the target variable in my case.
  
  In addition, when I tried to generate a word embedding model using the Gensim library, it resulted in more than 50k words, which I think will make it too difficult or even impossible to onehot encode. Even then, I will have to one hot encode each row and then create a padding for all the rows to be of similar length to feed the NN model, but the length of each row in the new column I created "clean\_text" varies significantly, so it will result in very big onehot encoded matrices that are kind of redundant.
  
  Am I approaching this completely wrong? and what should I do?
  
  **Tags:** neural-networks, machine-learning, deep-learning, natural-language-processing, recurrent-neural-networks
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-08-05. Score: 3 | Accepted answer
  
  I'll answer in a couple of stages.
  
  *I feel somewhat lost as to what the input for the NN should look like.*
  
  Your choices boil down to two options, each with their own multitude of variants:
  
  1. Vector Representation: Your input is a vector of the same size as your vocabulary where the elements represent the tokens in the input example. The most basic version of this is a bag-of-words (BOW) encoding with a 1 for each word that occurs in the input example and a 0 otherwise. Some other variants are (normalized) word counts or [TF-IDF](http://tfidf.com/) values. With this representation padding will not be necessary as each example will be encoded as a vector of the same size as the vocabulary. However, it suffers from a variety of issues: the input is high-dimensional and very sparse making learning difficult (as you note), it does not encode word order, and the individual word representations have little (TF-IDF) to no (BOW, counts) semantic information. It also limits your NN architecture to a feed-forward network, as more "interesting" architectures such a RNNs, CNNs, and transformers assume a matrix-like input, described below.
  2. Matrix Representation: Here your input representation is a matrix with each row being a vector (i.e. embedding) representation of the token at that index in the input example. How you actually get the pretrained embeddings into the model depends on a number of implementation-specific factors, but [this stackoverflow question](https://stackoverflow.com/questions/49710537/pytorch-gensim-how-to-load-pre-trained-word-embeddings) shows how to load embeddings from gensim into PyTorch. Here padding is necessary because the input examples will have variable numbers of tokens. [This stackoverflow answer](https://stackoverflow.com/a/52242625/2544396) shows how to add zero padding in PyTorch.
     This representation will be significantly better than the vector representation as it is relatively low-dimensional and non-sparse, it maintains word order, and using pretrained word-embeddings means your model will have access to semantic information. In fact, this last point leads to your next question.
  
  *Learning word embeddings creates vectors for words that are similar to each other syntax-wise, and I fail to see how that can be used to derive the weight/impact of each word on the target variable in my case.*
  
  Word embeddings are based on the assumptions of [distributional semantics](https://en.wikipedia.org/wiki/Distributional_semantics), the core tenet of which is often quoted as "a word is characterized by the company it keeps". That is, the meaning of a word *is* how it relates to other words. In the context of NLP, models can make better decisions because similar words are treated similarly from the get-go.
  
  For example, say that articles about furry pets get a lot of likes (entirely plausible if you ask me). However, the mentions of furry pets in these articles will be varied, including words like "dog", "cat", "chinchilla", "poodle", "doggo", "good boy", etc. An input representation that treats these mentions as completely distinct (such as BOW) will need to learn individual correlations between each word and the number of likes (that's a lot of learning). A well-trained word embedding, on the other hand, will be able to immediately group these mentions together and learn general correlations between groups of similar words and likes. Fair warning, this is a very imprecise description of why word embeddings work, but I hope it gives you some intuitive understanding.
  
  Finally, since you're doing regression, make sure you choose your objective function accordingly. Mean squared error would be my first try.
  
  > **Comments:**
  > **username_0**: Thank you for the detailed answer, this helps a lot  
  
  ---
  
  