def test_get_params(teardown): dummy1 = DummyEstimator(name="dummy1") dummy2 = DummyEstimator(x=456, y="def", name="dummy2") concat = Concatenate(name="concat") # a step without get_params/set_params # a meaningless pipeline that contains shared steps x1 = Input() x2 = Input() h = dummy1(x1) c = concat([x1, h]) y1 = dummy2(c) y2 = dummy2(x2, compute_func=lambda X: X * 2, trainable=False) model = Model([x1, x2], [y1, y2]) expected = { "dummy1": dummy1, "dummy2": dummy2, "concat": concat, "dummy1__x": 123, "dummy1__y": "abc", "dummy2__x": 456, "dummy2__y": "def", } params = model.get_params() assert params == expected
def test_fit_compute(self, teardown): dummy_estimator_1 = DummyEstimator() dummy_estimator_2 = DummyEstimator() x = Input() y_t = Input() y_p1 = dummy_estimator_1(x, y_t, fit_compute_func=None) y_p2 = dummy_estimator_2(x, y_t) model = Model(x, [y_p1, y_p2], y_t) model.fit(iris.data, iris.target) assert dummy_estimator_1.fit_calls == 1 assert dummy_estimator_1.fit_predict_calls == 0 assert dummy_estimator_2.fit_calls == 0 assert dummy_estimator_2.fit_predict_calls == 1
def test_fit_compute_func(self, simple_step, shared_step, dataplaceholders, teardown): assert simple_step.fit_compute_func == simple_step.fit_predict simple_step.fit_compute_func = simple_step.fit_predict_proba assert simple_step.fit_compute_func == simple_step.fit_predict_proba with pytest.raises(AttributeError): shared_step.fit_compute_func with pytest.raises(AttributeError): shared_step.fit_compute_func = shared_step.fit_predict_proba with pytest.raises(AttributeError): # because the step hasn't been called DummyEstimator().fit_compute_func with pytest.raises(AttributeError): # because the step hasn't been called DummyEstimator().fit_compute_func = lambda x: x
def test_set_params(self, teardown): step = DummyEstimator() new_params_wrong = {"non_existent_param": 42} with pytest.raises(ValueError): step.set_params(**new_params_wrong) new_params = {"x": 456} step.set_params(**new_params) params = step.get_params() expected = {"x": 456, "y": "abc"} assert params == expected
def test_set_params(teardown): dummy1 = DummyEstimator(name="dummy1") dummy2 = DummyEstimator(x=456, y="def", name="dummy2") concat = Concatenate(name="concat") # a step without get_params/set_params # a meaningless pipeline that contains shared steps x1 = Input() x2 = Input() h = dummy1(x1) c = concat([x1, h]) y1 = dummy2(c) y2 = dummy2(x2, compute_func=lambda X: X * 2, trainable=False) model = Model([x1, x2], [y1, y2]) # Fails when setting params on step that does not implement set_params new_params_wrong = {"concat__axis": 2} with pytest.raises(AttributeError): model.set_params(**new_params_wrong) # Fails when setting params on step that does not exist new_params_wrong = {"non_existent_step__param": 42} with pytest.raises(ValueError): model.set_params(**new_params_wrong) # Fails when setting a non-existent param in a step new_params_wrong = {"dummy1__non_existent_param": 42} with pytest.raises(ValueError): model.set_params(**new_params_wrong) new_dummy = DummyEstimator() new_params = { "dummy2": new_dummy, "dummy1__x": 100, "dummy1__y": "pqr", "dummy2__x": 789, "dummy2__y": "ijk", } model.set_params(**new_params) params = model.get_params() expected = { "dummy1": dummy1, "dummy2": new_dummy, "concat": concat, "dummy1__x": 100, "dummy1__y": "pqr", "dummy2__x": 789, "dummy2__y": "ijk", } assert params == expected # Connectivity of the new step should be the same as the old step assert new_dummy.name is dummy2.name for port in range(2): assert new_dummy.get_inputs_at(port) is dummy2.get_inputs_at(port) assert new_dummy.get_outputs_at(port) is dummy2.get_outputs_at(port) assert new_dummy.get_targets_at(port) is dummy2.get_targets_at(port) assert new_dummy.get_trainable_at(port) is dummy2.get_trainable_at(port) assert new_dummy.get_compute_func_at(port) is dummy2.get_compute_func_at(port)
def shared_step(self): return DummyEstimator()
def simple_step(self): return DummyEstimator()
def test_get_params(self, teardown): step = DummyEstimator() params = step.get_params() expected = {"x": 123, "y": "abc"} assert params == expected
def test_repr(self, print_changed_only, expected, teardown): set_config(print_changed_only=print_changed_only) step = DummyEstimator(x=456, name="DE") assert repr(step) == expected