Effects of class imbalance on neural network weights My question is about unbalanced classes problem in case of a classifier neural network for natural language processing (in particular, a neural network with LSTM).
I want to train a neural network to discriminate data between four classes and then I want to extract weights vectors from the embedding layer so that I can use them to have a numerical dense vector representation of my data.
If the classes are unbalanced (for example, class 1 has 27000 data, class 2 has 3600 data, class 3 has 260 data and class 4 has 10 data) and the metrics is set to "accuracy" (imaging to develop it using Keras in Python), the problem is that our classifier will tend to classify wrong the classes with less data so to reach a high accuracy. However, I do not really focus one thing: how are weights influenced by this fact ? For example, if I extract weights vectors that represent data from my embedding layer and I plot them, because of imbalanced classes problem, the effect is that data are not well discriminated in the plot according to the different classes to which they belong ? I mean: in this plot I should observe four clusters but they are all overlapping because of the unbalanced classes problem ?
Maybe it is a trivial question, so I apologize and I thank you in advance.
 A: First to your plot: the embeddings may be being further transformed farther down the network. Further layers in the network may be transforming the values of your embedding layer in a way that is not emendable to the plotting algorithm you are using. t-SNE plots usually work pretty good, but aren't perfect. In other words, just because the plot doesn't discriminate doesn't mean the model is failing.
However, you did say the classification isn't working well because of the imbalance. I want to make a slight distinction here: you actually have two problems: (a) imbalance, and (b) lack of data. Because there is a lack of data, your network doesn't have enough information to create an abstraction of the features of the class. There isn't really much to do about that; you just need more data.
You can address the imbalance problem though. To do this, use cross-entropy as a loss function instead of accuracy. Cross entropy fixes the problem of imbalanced classes. You can learn more about it here. However, with your data set, you simply don't have enough data for classes 3 and 4 to reasonably train a LSTM network.
A: You won't fully overcome your class imbalance by using an appropriate loss function alone. The network will produce a better result by almost always ignoring class 4 unless it's very easy to classify and the network can be very confident, however cross entropy will ensure that class 4 isn't predicted completely wrong because a 0.0 prediction for a 1.0 label is infinite error, or "surprise", but that doesn't preclude it from always giving class 4 a more reasonable value of say 0.3 when it's uncertain, the imbalance is likely to produce this kind of result with cross entropy. You could use that to adjust the classification, but I think a simpler approach is oversampling.
Oversampling your under represented class during training is a common approach. For example if your batch size is 8, two of those values come from each class. As was mentioned by Tanner, you have very little data in class 4, and class 3 for that matter, so now you have an issue of over fitting for class 4 (I would expect class 4 to generalize less well than class 1 and class 2). To improve that situation you should take any steps you can to augment your class 4 &
3 data. Data augmentation means you add random permutations to your data. What you add to the data should reflect actual variation you are likely to see in your real world data. Simply adding Gaussian noise to your class 4 features isn't likely to produce substantially better results. Good data augmentation requires you understand your data.
A: In general association among trained weights and class-imbalance is not that easy to establish. One way to overcome the imbalanced dataset and see how weights are changing, is using transfer learning, use balanced version the train first and then continue training with the unbalanced version of the dataset. However, this is an open research, see a survey article Survey on deep learning with class imbalance.
