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
| access_token = '***ENTER YOUR ACCESS TOKEN HERE***' | |
| class botCallback(tf.keras.callbacks.Callback): | |
| def __init__(self,access_token): | |
| self.access_token = access_token | |
| self.ping_url = 'https://api.telegram.org/bot'+str(self.access_token)+'/getUpdates' | |
| self.response = requests.get(self.ping_url).json() | |
| self.chat_id = self.response['result'][0]['message']['chat']['id'] | |
| def send_message(self,message): |
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
| checkpoint = tf.keras.callbacks.ModelCheckpoint(save_dir+model_name, monitor='val_accuracy', verbose=1, | |
| save_best_only=True, mode='max') | |
| reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.5, patience=4, | |
| verbose=1, mode='max', min_lr=0.00001) | |
| early_stop = tf.keras.callbacks.EarlyStopping(monitor="val_loss", mode="min", patience=5) | |
| callbacks_list = [checkpoint, reduce_lr, early_stop, bot_callback, plotter] |
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
| history = model.fit_generator(train_data_gen, | |
| steps_per_epoch = TRAIN_STEPS_PER_EPOCH, | |
| epochs = 20, | |
| callbacks = callbacks_list, | |
| validation_data = validation_data_gen, | |
| validation_steps=VAL_STEPS_PER_EPOCH) |
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 Plotter(botCallback): | |
| def __init__(self,access_token): | |
| super().__init__(access_token) | |
| def on_train_begin(self,logs=None): | |
| self.batch = 0 | |
| self.epoch = [] | |
| self.train_loss = [] | |
| self.val_loss = [] | |
| self.train_acc = [] |
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
| def TP(y, pred, th=0.5): | |
| pred_t = (pred > th) | |
| return np.sum((pred_t == True) & (y == 1)) |
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
| def TN(y, pred, th=0.5): | |
| pred_t = (pred > th) | |
| return np.sum((pred_t == False) & (y == 0)) |
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
| def FP(y, pred, th=0.5): | |
| pred_t = (pred > th) | |
| return np.sum((pred_t == True) & (y == 0)) |
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
| def FN(y, pred, th=0.5): | |
| pred_t = (pred > th) | |
| return np.sum((pred_t == False) & (y == 1)) |
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
| def precision(y, pred, th=0.5): | |
| tp = TP(y,pred,th) | |
| fp = FP(y,pred,th) | |
| return tp/(tp+fp) |
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
| def recall(y, pred, th=0.5): | |
| tp = TP(y,pred,th) | |
| fn = FN(y,pred,th) | |
| return tp/(tp+fn) |