Пример #1
0
def test_overloaded_operators():
    param_a = ComposedParameter('param_ao', 5 * 4)
    param_b = ComposedParameter('param_bo', 3)
    param_c = param_a * param_b
    assert not isinstance(param_c, zfit.Parameter)
    param_d = ComposedParameter("param_do", param_a + param_a * param_b ** 2)
    param_d_val = zfit.run(param_d)
    assert param_d_val == zfit.run(param_a + param_a * param_b ** 2)
Пример #2
0
def test_overloaded_operators():
    param1 = zfit.Parameter('param1', 5)
    param2 = zfit.Parameter('param2', 3)
    param_a = ComposedParameter('param_a', lambda p1: p1 * 4, params=param1)
    param_b = ComposedParameter('param_b', lambda p2: p2, params=param2)
    param_c = param_a * param_b
    assert not isinstance(param_c, zfit.Parameter)
    param_d = ComposedParameter("param_d", lambda pa, pb: pa + pa * pb ** 2, params=[param_a, param_b])
    param_d_val = param_d.numpy()
    assert param_d_val == (param_a + param_a * param_b ** 2).numpy()
Пример #3
0
def test_overloaded_operators():
    param_a = ComposedParameter('param_a', lambda: 5 * 4, dependents=None)
    param_b = ComposedParameter('param_b', lambda: 3, dependents=None)
    param_c = param_a * param_b
    assert not isinstance(param_c, zfit.Parameter)
    param_d = ComposedParameter("param_d",
                                lambda: param_a + param_a * param_b**2,
                                dependents=[param_a, param_b])
    param_d_val = param_d.numpy()
    assert param_d_val == (param_a + param_a * param_b**2).numpy()
Пример #4
0
def test_composed_param():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter(
        'param4',
        4.)  # needed to make sure it does not only take all params as deps
    a = ztf.log(3. * param1) * tf.square(param2) - param3
    param_a = ComposedParameter('param_as', tensor=a)
    assert isinstance(param_a.get_dependents(only_floating=True), OrderedSet)
    assert param_a.get_dependents(only_floating=True) == {param1, param2}
    assert param_a.get_dependents(only_floating=False) == {
        param1, param2, param3
    }
    a_unchanged = zfit.run(a)
    assert a_unchanged == zfit.run(param_a)
    assert zfit.run(param2.assign(3.5))
    a_changed = zfit.run(a)
    assert a_changed == zfit.run(param_a)
    assert a_changed != a_unchanged

    with pytest.raises(LogicalUndefinedOperationError):
        param_a.assign(value=5.)
    with pytest.raises(LogicalUndefinedOperationError):
        param_a.load(value=5., session=zfit.run.sess)
Пример #5
0
def test_composed_param():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter(
        'param4', 4.
    )  # noqa Needed to make sure it does not only take all params as deps

    def value_fn(p1, p2, p3):
        return z.math.log(3. * p1) * tf.square(p2) - p3

    param_a = ComposedParameter('param_as',
                                value_fn=value_fn,
                                params=(param1, param2, param3))
    param_a2 = ComposedParameter(
        'param_as2',
        value_fn=value_fn,
        params={f'p{i}': p
                for i, p in enumerate((param1, param2, param3))})
    assert param_a2.params['p1'] == param2
    assert isinstance(param_a.get_cache_deps(only_floating=True), OrderedSet)
    assert param_a.get_cache_deps(only_floating=True) == {param1, param2}
    assert param_a.get_cache_deps(only_floating=False) == {
        param1, param2, param3
    }
    a_unchanged = value_fn(param1, param2, param3).numpy()
    assert a_unchanged == param_a.numpy()
    assert param2.assign(3.5).numpy()
    a_changed = value_fn(param1, param2, param3).numpy()
    assert a_changed == param_a.numpy()
    assert a_changed != a_unchanged
Пример #6
0
def test_shape_composed_parameter():
    a = Parameter(name='a', value=1)
    b = Parameter(name='b', value=2)

    def compose():
        return tf.square(a) - b
    c = ComposedParameter(name='c', value_fn=compose, dependents=[a, b])
    assert c.shape.rank == 0
Пример #7
0
def test_shape_composed_parameter():
    a = Parameter(name="a", value=1)
    b = Parameter(name="b", value=2)

    def compose():
        return tf.square(a) - b

    c = ComposedParameter(name="c", value_fn=compose, params=[a, b])
    assert c.shape.rank == 0
Пример #8
0
def test_composed_param():
    param1 = Parameter("param1", 1.0)
    param2 = Parameter("param2", 2.0)
    param3 = Parameter("param3", 3.0, floating=False)
    param4 = Parameter(
        "param4", 4.0
    )  # noqa Needed to make sure it does not only take all params as deps

    def value_fn(p1, p2, p3):
        return z.math.log(3.0 * p1) * tf.square(p2) - p3

    param_a = ComposedParameter("param_as",
                                value_fn=value_fn,
                                params=(param1, param2, param3))
    param_a2 = ComposedParameter(
        "param_as2",
        value_fn=value_fn,
        params={f"p{i}": p
                for i, p in enumerate((param1, param2, param3))},
    )
    assert param_a2.params["p1"] == param2
    assert isinstance(param_a.get_cache_deps(only_floating=True), OrderedSet)
    assert param_a.get_cache_deps(only_floating=True) == {param1, param2}
    assert param_a.get_cache_deps(only_floating=False) == {
        param1, param2, param3
    }
    a_unchanged = value_fn(param1, param2, param3).numpy()
    assert a_unchanged == param_a.numpy()
    param2.assign(3.5)
    assert param2.numpy()
    a_changed = value_fn(param1, param2, param3).numpy()
    assert a_changed == param_a.numpy()
    assert a_changed != a_unchanged

    print(param_a)

    @z.function
    def print_param(p):
        print(p)

    print_param(param_a)
Пример #9
0
def test_composed_param():
    param1 = Parameter('param1', 1.)
    param2 = Parameter('param2', 2.)
    param3 = Parameter('param3', 3., floating=False)
    param4 = Parameter(
        'param4',
        4.)  # needed to make sure it does not only take all params as deps

    def a():
        return z.log(3. * param1) * tf.square(param2) - param3

    param_a = ComposedParameter('param_as',
                                value_fn=a,
                                dependents=(param1, param2, param3))
    assert isinstance(param_a.get_dependents(only_floating=True), OrderedSet)
    assert param_a.get_dependents(only_floating=True) == {param1, param2}
    assert param_a.get_dependents(only_floating=False) == {
        param1, param2, param3
    }
    a_unchanged = a().numpy()
    assert a_unchanged == param_a.numpy()
    assert param2.assign(3.5).numpy()
    a_changed = a().numpy()
    assert a_changed == param_a.numpy()
    assert a_changed != a_unchanged

    with pytest.raises(LogicalUndefinedOperationError):
        param_a.assign(value=5.)
    with pytest.raises(LogicalUndefinedOperationError):
        param_a.assign(value=5.)