Created
January 4, 2019 02:19
-
-
Save upidea/f59873224c0e76b22817ca56447fd2b6 to your computer and use it in GitHub Desktop.
callback for keras fit, caculate precison and recall.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Metrics(tf.keras.callbacks.Callback): | |
| def on_train_begin(self, logs={}): | |
| self.confusion = [] | |
| self.precision = [] | |
| self.recall = [] | |
| self.f1s = [] | |
| self.kappa = [] | |
| self.auc = [] | |
| def on_epoch_end(self, epoch, logs={}): | |
| score = np.asarray(self.model.predict(self.validation_data[0])) | |
| # predict = np.round(np.asarray(self.model.predict(self.validation_data[0]))) | |
| predict = np.asarray(self.model.predict(self.validation_data[0])).argmax(axis=1) | |
| targ = self.validation_data[1].argmax(axis=1) | |
| # self.auc.append(sklm.roc_auc_score(targ, score)) | |
| self.confusion.append(sklm.confusion_matrix(targ, predict)) | |
| self.precision.append(sklm.precision_score(targ, predict, average='macro')) | |
| print(self.precision) | |
| self.recall.append(sklm.recall_score(targ, predict, average='macro')) | |
| print(self.recall) | |
| self.f1s.append(sklm.f1_score(targ, predict, average='macro')) | |
| print(self.f1s) | |
| self.kappa.append(sklm.cohen_kappa_score(targ, predict)) | |
| return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment