Commit d3e11a9a authored by lucas_miranda's avatar lucas_miranda
Browse files

Added tests for 1_cycle_scheduler in model_utils.py

parent d806e3ed
......@@ -142,6 +142,7 @@ class one_cycle_scheduler(tf.keras.callbacks.Callback):
self.half_iteration = (iterations - self.last_iterations) // 2
self.last_rate = last_rate or self.start_rate / 1000
self.iteration = 0
self.history = {}
def _interpolate(self, iter1: int, iter2: int, rate1: float, rate2: float) -> float:
return (rate2 - rate1) * (self.iteration - iter1) / (iter2 - iter1) + rate1
......@@ -149,6 +150,9 @@ class one_cycle_scheduler(tf.keras.callbacks.Callback):
# noinspection PyMethodOverriding,PyTypeChecker
def on_batch_begin(self, batch: int, logs):
""" Defines computations to perform for each batch """
self.history.setdefault("lr", []).append(K.get_value(self.model.optimizer.lr))
if self.iteration < self.half_iteration:
rate = self._interpolate(
0, self.half_iteration, self.start_rate, self.max_rate
......@@ -175,7 +179,7 @@ class one_cycle_scheduler(tf.keras.callbacks.Callback):
class uncorrelated_features_constraint(Constraint):
"""
Tensorflow Constraint subclass that forces a layer to have uncorrelated features.
tf.keras.constraints.Constraint subclass that forces a layer to have uncorrelated features.
Useful, among others, for auto encoder bottleneck layers
"""
......
......@@ -13,6 +13,7 @@ from hypothesis import settings
from hypothesis import strategies as st
from hypothesis.extra.numpy import arrays
import deepof.model_utils
import numpy as np
import tensorflow as tf
from tensorflow.python.framework.ops import EagerTensor
......@@ -65,11 +66,30 @@ def test_one_cycle_scheduler():
)
assert type(cycle1._interpolate(1, 2, 0.2, 0.5)) == float
X = np.random.uniform(0, 10, [1500, 5])
y = np.random.randint(0, 2, [1500, 1])
test_model = tf.keras.Sequential()
test_model.add(tf.keras.layers.Dense(1))
test_model.compile(
loss=tf.keras.losses.binary_crossentropy, optimizer=tf.keras.optimizers.SGD(),
)
onecycle = deepof.model_utils.one_cycle_scheduler(
X.shape[0] // 100 * 10, max_rate=0.005,
)
fit = test_model.fit(X, y, callbacks=[onecycle], epochs=10, batch_size=100)
assert type(fit) == tf.python.keras.callbacks.History
assert onecycle.history["lr"][4] > onecycle.history["lr"][1]
assert onecycle.history["lr"][4] > onecycle.history["lr"][-1]
# @settings(deadline=None)
# @given()
# def test_uncorrelated_features_constraint():
# pass
def test_uncorrelated_features_constraint():
pass
# @settings(deadline=None)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment