diff --git a/kim_cnn/README.md b/kim_cnn/README.md index 1db8d06..61916a2 100644 --- a/kim_cnn/README.md +++ b/kim_cnn/README.md @@ -4,7 +4,7 @@ Implementation for Convolutional Neural Networks for Sentence Classification of ## Project Structure ``` -text-classification-cnn +kim-cnn ├── config │ └── classification.cfg ├── etc diff --git a/kim_cnn/network/cnnTextNetwork.py b/kim_cnn/network/cnnTextNetwork.py index 0e82542..f0e8d90 100644 --- a/kim_cnn/network/cnnTextNetwork.py +++ b/kim_cnn/network/cnnTextNetwork.py @@ -103,6 +103,9 @@ class cnnTextNetwork(Configurable): acc_sents = 0 # count sents number for one log_interval epoch = 0 + best_accuracy = 0 + best_model = 0 + while True: for batch in self.train_minibatch(): self.model.train() @@ -152,7 +155,19 @@ class cnnTextNetwork(Configurable): epoch += 1 accuracy = float(acc_corrects) / float(acc_sents) * 100 + + # if the new accuracy is better than the old accuracy by 0.1% + if accuracy - best_accuracy > 0.1: + best_accuracy = accuracy + best_model = epoch + print("[EPOCH] %d Accuracy: %5.2f" % (epoch, accuracy)) + + # stop training if the accuracy remains the same over 5 epochs + if (epoch - best_model) >= 5: + print('No improvement since the last {} epochs. Stopping training'.format(epoch - best_model)) + break + acc_corrects = 0 acc_sents = 0 if (epoch % self.epoch_decay == 0):