A text classifier predicts a label from text: whether a movie review is positive or negative, whether an email is spam, or which topic a newswire covers. The first question is whether each input has one possible label or can have several. The second is whether labeled examples are available to train the model. section 4.1 section 4.2 section 6.1.1
From newswire to topic
The Reuters dataset contains short newswires assigned to one of 46 topics. In Keras, each newswire is supplied as a sequence of word indices, and its label is an integer from 0 to 45. There are 8,982 training examples and 2,246 test examples. A classifier trained on these labeled inputs produces a score for each possible topic. section 4.2.1 section 4.2.3
Match the output layer and loss to the labeling problem:
- Two mutually exclusive classes: use one sigmoid output and binary_crossentropy. The output is a probability between 0 and 1, as in positive-versus-negative review classification. section 4.1.7
- Exactly one of many classes: use one softmax output per class. For 46 topics, the outputs form a probability distribution summing to 1. Use categorical_crossentropy for one-hot targets or sparse_categorical_crossentropy for integer targets. section 4.2.3 section 20.1.6
- Several possible labels per input: use one sigmoid output per class with binary_crossentropy and k-hot targets. This is a different problem from choosing one newswire topic. section 4.2 section 20.1.6
Listing 4.15 Model definition
model = keras.Sequential( [ layers.Dense(64, activation="relu"), layers.Dense(64, activation="relu"), layers.Dense(46, activation="softmax"), ] )
A Keras topic classifier uses two 64-unit Dense layers followed by 46 softmax outputs. The wider intermediate layers help avoid discarding information needed to distinguish many classes. section 4.2.3
Prepare text as numeric input before feeding it to a neural network. Binary vectors of words are one option; text can also be treated as a sequence when word order matters. Whatever the representation, monitor performance outside the training set: improvement on training data can accompany worsening results on unseen examples. For many classes, also avoid intermediate layers so small that they become information bottlenecks. section 4.1.7 section 14.3 section 4.4






























