Last active
July 2, 2017 15:23
-
-
Save joelthchao/b887e5cddcaa0d3b11a2d53506f133f8 to your computer and use it in GitHub Desktop.
Keras callback
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
from keras.callbacks import Callback | |
import numpy as np | |
class MyCallback(Callback): | |
def __init__(self, test_data, test_label, log_file=None, verbose=True): | |
self.test_data = test_data | |
self.test_label = test_label | |
self.result = [] | |
self.log = open(log_file, 'w') if log_file else None | |
self.verbose = verbose | |
def on_epoch_end(self, epoch, logs=None): | |
probs = self.model.predict(self.test_data) | |
avg_acc = calculate_acc(probs, self.test_label) # function to calculate acc | |
self.result.append(avg_acc) | |
log_str = 'Epoch {} acc= {:.4f}'.format(epoch, avg_acc) | |
if self.verbose: | |
print(log_str) | |
if self.log: | |
print(log_str, file=self.log) | |
def on_train_end(self, logs=None): | |
if self.log: | |
self.log.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment