From 6d3c3ddbe6f9c7548b8502b4432ad9690ccb386f Mon Sep 17 00:00:00 2001 From: Vijayabhaskar Date: Wed, 31 Jan 2018 15:53:49 +0530 Subject: [PATCH 1/6] casted ReduceLRonPlateau lr log to float Previously lr log had dtype numpy.float32 which throws JSON serializable error if used along with Remote monitor callback. --- keras/callbacks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index 9b726f65e0c..49422d2481e 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -913,7 +913,7 @@ def on_train_begin(self, logs=None): def on_epoch_end(self, epoch, logs=None): logs = logs or {} - logs['lr'] = K.get_value(self.model.optimizer.lr) + logs['lr'] = float(K.get_value(self.model.optimizer.lr)) current = logs.get(self.monitor) if current is None: warnings.warn( From eac2eb217af93f29c98de14996dd0cc496660024 Mon Sep 17 00:00:00 2001 From: Vijayabhaskar J Date: Thu, 1 Feb 2018 10:21:14 +0530 Subject: [PATCH 2/6] Undone changes made:Revert "casted ReduceLRonPlateau lr log to float" This reverts commit 6d3c3ddbe6f9c7548b8502b4432ad9690ccb386f. --- keras/callbacks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index 49422d2481e..9b726f65e0c 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -913,7 +913,7 @@ def on_train_begin(self, logs=None): def on_epoch_end(self, epoch, logs=None): logs = logs or {} - logs['lr'] = float(K.get_value(self.model.optimizer.lr)) + logs['lr'] = K.get_value(self.model.optimizer.lr) current = logs.get(self.monitor) if current is None: warnings.warn( From 21587a610023cbb265cef434e434ce7e4da4c441 Mon Sep 17 00:00:00 2001 From: Vijayabhaskar J Date: Thu, 1 Feb 2018 16:59:06 +0530 Subject: [PATCH 3/6] fixed RemoteMonitor json to handle np.float32 and np.int32 types --- keras/callbacks.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index 9b726f65e0c..bfbd96d9d47 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -89,8 +89,8 @@ def on_batch_begin(self, batch, logs=None): self._delta_ts_batch_begin.append(time.time() - t_before_callbacks) delta_t_median = np.median(self._delta_ts_batch_begin) if (self._delta_t_batch > 0. and - delta_t_median > 0.95 * self._delta_t_batch and - delta_t_median > 0.1): + delta_t_median > 0.95 * self._delta_t_batch and + delta_t_median > 0.1): warnings.warn('Method on_batch_begin() is slow compared ' 'to the batch update (%f). Check your callbacks.' % delta_t_median) @@ -113,7 +113,7 @@ def on_batch_end(self, batch, logs=None): self._delta_ts_batch_end.append(time.time() - t_before_callbacks) delta_t_median = np.median(self._delta_ts_batch_end) if (self._delta_t_batch > 0. and - (delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1)): + (delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1)): warnings.warn('Method on_batch_end() is slow compared ' 'to the batch update (%f). Check your callbacks.' % delta_t_median) @@ -547,6 +547,10 @@ def on_epoch_end(self, epoch, logs=None): send['epoch'] = epoch for k, v in logs.items(): send[k] = v + if isinstance(v, (np.float16, np.float32)): + send[k] = np.float64(v) + elif isinstance(v, (np.int16, np.int32)): + send[k] = np.float64(v) try: requests.post(self.root + self.path, {self.field: json.dumps(send)}, @@ -899,7 +903,7 @@ def _reset(self): RuntimeWarning) self.mode = 'auto' if (self.mode == 'min' or - (self.mode == 'auto' and 'acc' not in self.monitor)): + (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: From f83c0a5ca893f2ce5176b5651f083e4abac76ff1 Mon Sep 17 00:00:00 2001 From: Vijayabhaskar J Date: Thu, 1 Feb 2018 17:34:39 +0530 Subject: [PATCH 4/6] fixed RemoteMonitor json to handle np.float32 and np.int32 types --- keras/callbacks.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index bfbd96d9d47..c8ac331e159 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -89,8 +89,8 @@ def on_batch_begin(self, batch, logs=None): self._delta_ts_batch_begin.append(time.time() - t_before_callbacks) delta_t_median = np.median(self._delta_ts_batch_begin) if (self._delta_t_batch > 0. and - delta_t_median > 0.95 * self._delta_t_batch and - delta_t_median > 0.1): + delta_t_median > 0.95 * self._delta_t_batch and + delta_t_median > 0.1): warnings.warn('Method on_batch_begin() is slow compared ' 'to the batch update (%f). Check your callbacks.' % delta_t_median) @@ -113,7 +113,7 @@ def on_batch_end(self, batch, logs=None): self._delta_ts_batch_end.append(time.time() - t_before_callbacks) delta_t_median = np.median(self._delta_ts_batch_end) if (self._delta_t_batch > 0. and - (delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1)): + (delta_t_median > 0.95 * self._delta_t_batch and delta_t_median > 0.1)): warnings.warn('Method on_batch_end() is slow compared ' 'to the batch update (%f). Check your callbacks.' % delta_t_median) @@ -903,7 +903,7 @@ def _reset(self): RuntimeWarning) self.mode = 'auto' if (self.mode == 'min' or - (self.mode == 'auto' and 'acc' not in self.monitor)): + (self.mode == 'auto' and 'acc' not in self.monitor)): self.monitor_op = lambda a, b: np.less(a, b - self.epsilon) self.best = np.Inf else: From a4edf68cddaed8e62aa2b1129ab8ba2f106183b8 Mon Sep 17 00:00:00 2001 From: Vijayabhaskar Date: Thu, 1 Feb 2018 18:30:10 +0530 Subject: [PATCH 5/6] fixed np.int32 json unserializable error in Remotemonitor --- keras/callbacks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index c8ac331e159..93a174aa7b0 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -550,7 +550,7 @@ def on_epoch_end(self, epoch, logs=None): if isinstance(v, (np.float16, np.float32)): send[k] = np.float64(v) elif isinstance(v, (np.int16, np.int32)): - send[k] = np.float64(v) + send[k] = np.int64(v) try: requests.post(self.root + self.path, {self.field: json.dumps(send)}, From 1336d922199921645f8cbd353115a765e9c7fbc4 Mon Sep 17 00:00:00 2001 From: Vijayabhaskar J Date: Fri, 2 Feb 2018 15:02:19 +0530 Subject: [PATCH 6/6] fix RemoteMonitor json to handle np.float32 and np.int32 types --- keras/callbacks.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/keras/callbacks.py b/keras/callbacks.py index 93a174aa7b0..ebdd3692bed 100644 --- a/keras/callbacks.py +++ b/keras/callbacks.py @@ -546,11 +546,10 @@ def on_epoch_end(self, epoch, logs=None): send = {} send['epoch'] = epoch for k, v in logs.items(): - send[k] = v - if isinstance(v, (np.float16, np.float32)): - send[k] = np.float64(v) - elif isinstance(v, (np.int16, np.int32)): - send[k] = np.int64(v) + if isinstance(v, (np.ndarray, np.generic)): + send[k] = v.item() + else: + send[k] = v try: requests.post(self.root + self.path, {self.field: json.dumps(send)},