Exemplo n.º 1
0
def benchmark_times(plays, iterations=3):
    times = defaultdict(lambda: defaultdict(list))

    def store_time(model, name):
        def inner(iteration, elapsed):
            print(name, model.factors, iteration, elapsed)
            times[name][model.factors].append(elapsed)
        return inner

    output = defaultdict(list)
    for factors in range(32, 257, 32):
        for steps in [2, 3, 4]:
            model = AlternatingLeastSquares(factors=factors, use_native=True, use_cg=True,
                                            regularization=0, iterations=iterations)
            model.fit_callback = store_time(model, 'cg%i' % steps)
            model.cg_steps = steps
            model.fit(plays)

        model = AlternatingLeastSquares(factors=factors, use_native=True, use_cg=False,
                                        regularization=0, iterations=iterations)
        model.fit_callback = store_time(model, 'cholesky')
        model.fit(plays)

        if has_cuda:
            model = AlternatingLeastSquares(factors=factors, use_native=True, use_gpu=True,
                                            regularization=0, iterations=iterations)
            model.fit_callback = store_time(model, 'gpu')
            model.fit(plays)

        # take the min time for the output
        output['factors'].append(factors)
        for name, stats in times.items():
            output[name].append(min(stats[factors]))

    return output
Exemplo n.º 2
0
def benchmark_accuracy(plays):
    output = defaultdict(list)

    def store_loss(model, name):
        def inner(iteration, elapsed):
            loss = calculate_loss(plays, model.item_factors, model.user_factors, 0)
            print("model %s iteration %i loss %.5f" % (name, iteration, loss))
            output[name].append(loss)
        return inner

    for steps in [2, 3, 4]:
        model = AlternatingLeastSquares(factors=100, use_native=True, use_cg=True, regularization=0,
                                        iterations=25)
        model.cg_steps = steps
        model.fit_callback = store_loss(model, 'cg%i' % steps)
        model.fit(plays)

    if has_cuda:
        model = AlternatingLeastSquares(factors=100, use_native=True, use_gpu=True,
                                        regularization=0, iterations=25)
        model.fit_callback = store_loss(model, 'gpu')
        model.use_gpu = True
        model.fit(plays)

    model = AlternatingLeastSquares(factors=100, use_native=True, use_cg=False, regularization=0,
                                    iterations=25)
    model.fit_callback = store_loss(model, 'cholesky')
    model.fit(plays)

    return output
Exemplo n.º 3
0
def benchmark_times(plays, iterations=3):
    times = defaultdict(lambda: defaultdict(list))

    def store_time(model, name):
        def inner(iteration, elapsed):
            print(name, model.factors, iteration, elapsed)
            times[name][model.factors].append(elapsed)

        return inner

    output = defaultdict(list)
    for factors in range(32, 257, 32):
        for steps in [2, 3, 4]:
            model = AlternatingLeastSquares(
                factors=factors,
                use_native=True,
                use_cg=True,
                regularization=0,
                iterations=iterations,
            )
            model.fit_callback = store_time(model, "cg%i" % steps)
            model.cg_steps = steps
            model.fit(plays)

        model = AlternatingLeastSquares(factors=factors,
                                        use_native=True,
                                        use_cg=False,
                                        regularization=0,
                                        iterations=iterations)
        model.fit_callback = store_time(model, "cholesky")
        model.fit(plays)

        if has_cuda:
            model = AlternatingLeastSquares(
                factors=factors,
                use_native=True,
                use_gpu=True,
                regularization=0,
                iterations=iterations,
            )
            model.fit_callback = store_time(model, "gpu")
            model.fit(plays)

        # take the min time for the output
        output["factors"].append(factors)
        for name, stats in times.items():
            output[name].append(min(stats[factors]))

    return output
Exemplo n.º 4
0
def benchmark_accuracy(plays):
    output = defaultdict(list)

    def store_loss(model, name):
        def inner(iteration, elapsed):
            loss = calculate_loss(plays, model.item_factors,
                                  model.user_factors, 0)
            print("model %s iteration %i loss %.5f" % (name, iteration, loss))
            output[name].append(loss)

        return inner

    for steps in [2, 3, 4]:
        model = AlternatingLeastSquares(factors=100,
                                        use_native=True,
                                        use_cg=True,
                                        regularization=0,
                                        iterations=25)
        model.cg_steps = steps
        model.fit_callback = store_loss(model, 'cg%i' % steps)
        model.fit(plays)

    if has_cuda:
        model = AlternatingLeastSquares(factors=100,
                                        use_native=True,
                                        use_gpu=True,
                                        regularization=0,
                                        iterations=25)
        model.fit_callback = store_loss(model, 'gpu')
        model.use_gpu = True
        model.fit(plays)

    model = AlternatingLeastSquares(factors=100,
                                    use_native=True,
                                    use_cg=False,
                                    regularization=0,
                                    iterations=25)
    model.fit_callback = store_loss(model, 'cholesky')
    model.fit(plays)

    return output