def testTriangular2CyclicalLearningRate(self, serialize):
        self.skipTest("Failing. See https://github.com/tensorflow/addons/issues/1203")
        initial_learning_rate = 0.1
        maximal_learning_rate = 1
        step_size = 4000
        step = tf.resource_variable_ops.ResourceVariable(0)
        triangular2_cyclical_lr = cyclical_learning_rate.Triangular2CyclicalLearningRate(
            initial_learning_rate=initial_learning_rate,
            maximal_learning_rate=maximal_learning_rate,
            step_size=step_size,
        )
        triangular2_cyclical_lr = _maybe_serialized(triangular2_cyclical_lr, serialize)

        self.evaluate(tf.compat.v1.global_variables_initializer())
        middle_learning_rate = (maximal_learning_rate + initial_learning_rate) / 2
        expected = np.concatenate(
            [
                np.linspace(initial_learning_rate, maximal_learning_rate, num=2001)[1:],
                np.linspace(maximal_learning_rate, initial_learning_rate, num=2001)[1:],
                np.linspace(initial_learning_rate, middle_learning_rate, num=2001)[1:],
                np.linspace(middle_learning_rate, initial_learning_rate, num=2001)[1:],
            ]
        )

        for expected_value in expected:
            self.assertAllClose(
                self.evaluate(triangular2_cyclical_lr(step)), expected_value, 1e-6
            )
            self.evaluate(step.assign_add(1))
Exemple #2
0
def test_triangular2_cyclical_learning_rate(serialize):
    initial_lr = 0.1
    maximal_lr = 1
    step_size = 30
    triangular2_lr = cyclical_learning_rate.Triangular2CyclicalLearningRate(
        initial_learning_rate=initial_lr,
        maximal_learning_rate=maximal_lr,
        step_size=step_size,
    )
    triangular2_lr = _maybe_serialized(triangular2_lr, serialize)

    middle_lr = (maximal_lr + initial_lr) / 2
    expected = np.concatenate([
        np.linspace(initial_lr, maximal_lr, num=step_size + 1),
        np.linspace(maximal_lr, initial_lr, num=step_size + 1)[1:],
        np.linspace(initial_lr, middle_lr, num=step_size + 1)[1:],
        np.linspace(middle_lr, initial_lr, num=step_size + 1)[1:],
    ])

    for step, expected_value in enumerate(expected):
        np.testing.assert_allclose(
            triangular2_lr(step).numpy(), expected_value, 1e-6)