0

对于测试机器学习算法/存储库,我看到了三件重要的事情:

  1. 会不会崩溃
  2. 是否有最低测试精度
  3. 速度够快吗

虽然(1)和(2)可能是标准单元测试,但我不太确定如何处理(3)。我可以用 pytest / tox 测试一下吗?

我找到pytest-benchmark了,但我将如何做到这一点lidtk

在伪代码中,我想做以下事情:

def classifier_predict(input_features):
    # do something smart, but maybe too time-consuming
    return result

def input_generator():
    # Generate something random which classifier_predict
    # can work on - don't measure this time!
    return input_features

class Agents(unittest.TestCase):
    def test_classifier_predict():
        self.assertMaxTime(classifier_predict,
                           input_generator,
                           max_time_in_ms=100)
4

1 回答 1

1

Handcraft-solution

Here is the pseudo-code of a rather hand-crafted solution:

def classifier_predict(input_features):
    # do something smart, but maybe too time-consuming
    return result

def input_generator():
    # Generate something random which classifier_predict
    # can work on - don't measure this time!
    return input_features

class Agents(unittest.TestCase):
    def test_classifier_predict():
        nb_tests = 1000
        total_time = 0.0
        for _ in range(nb_tests):
            input_ = input_generator()
            t0 = time.time()
            classifier_predict(input_)
            t1 = time.time()
            total_time += t1 - t0
        self.assertLessEqual(total_time / nb_tests, 100)

Drawbacks

  • No nice graphs (like pytest-benchmark seems to generate)
  • In general, hard limits might be difficult due to different hardware and also due to different external work load
于 2018-01-15T14:41:04.070 回答