How to use sparse - 10 common examples

To help you get started, we’ve selected a few sparse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MaestroGraph / sparse-hyper / experiments / attention.py View on Github external
Model with a convolution head. More powerful classification, but more difficult to train on top of a hyperlayer.
        """

        hyperlayer = BoxAttentionLayer(
            glimpses=arg.num_glimpses,
            in_size=shape, k=arg.k,
            gadditional=arg.gadditional, radditional=arg.radditional, region=(arg.region, arg.region),
            min_sigma=arg.min_sigma
        )

        ch1, ch2, ch3 = 16, 32, 64
        h = (arg.k // 8) ** 2 * 64

        model = nn.Sequential(
            hyperlayer,
            util.Reshape((arg.num_glimpses * shape[0], arg.k, arg.k)), # Fold glimpses into channels
            nn.Conv2d(arg.num_glimpses * shape[0], ch1, kernel_size=5, padding=2),
            activation,
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(ch1, ch2, kernel_size=5, padding=2),
            activation,
            nn.Conv2d(ch2, ch2, kernel_size=5, padding=2),
            activation,
            nn.MaxPool2d(kernel_size=2),
            nn.Conv2d(ch2, ch3, kernel_size=5, padding=2),
            activation,
            nn.Conv2d(ch3, ch3, kernel_size=5, padding=2),
            activation,
            nn.MaxPool2d(kernel_size=2),
            util.Flatten(),
            nn.Linear(h, 128),
            activation,
github MaestroGraph / sparse-hyper / experiments / identity.py View on Github external
iterations = arg.iterations if arg.iterations is not None else arg.size * 3000
    additional = arg.additional if arg.additional is not None else int(np.floor(np.log2(arg.size)) * arg.size)

    torch.manual_seed(arg.seed)

    ndots = iterations // arg.dot_every

    results = np.zeros((arg.reps, ndots))

    print('Starting size {} with {} additional samples (reinforce={})'.format(arg.size, additional, arg.reinforce))
    w = None
    for r in range(arg.reps):
        print('repeat {} of {}'.format(r, arg.reps))

        util.makedirs('./identity/{}'.format(r))
        util.makedirs('./runs/identity/{}'.format(r))

        if w is not None:
            w.close()
        w = SummaryWriter(log_dir='./runs/identity/{}/'.format(r))

        SHAPE = (arg.size,)

        if not arg.reinforce:
            model = sparse.NASLayer(
                SHAPE, SHAPE,
                k=arg.size,
                gadditional=additional,
                sigma_scale=arg.sigma_scale,
                has_bias=False,
                fix_values=arg.fix_values,
github MaestroGraph / sparse-hyper / experiments / identity.py View on Github external
iterations = arg.iterations if arg.iterations is not None else arg.size * 3000
    additional = arg.additional if arg.additional is not None else int(np.floor(np.log2(arg.size)) * arg.size)

    torch.manual_seed(arg.seed)

    ndots = iterations // arg.dot_every

    results = np.zeros((arg.reps, ndots))

    print('Starting size {} with {} additional samples (reinforce={})'.format(arg.size, additional, arg.reinforce))
    w = None
    for r in range(arg.reps):
        print('repeat {} of {}'.format(r, arg.reps))

        util.makedirs('./identity/{}'.format(r))
        util.makedirs('./runs/identity/{}'.format(r))

        if w is not None:
            w.close()
        w = SummaryWriter(log_dir='./runs/identity/{}/'.format(r))

        SHAPE = (arg.size,)

        if not arg.reinforce:
            model = sparse.NASLayer(
                SHAPE, SHAPE,
                k=arg.size,
                gadditional=additional,
                sigma_scale=arg.sigma_scale,
                has_bias=False,
                fix_values=arg.fix_values,
                min_sigma=arg.min_sigma,
github ahwillia / affinewarp / tests / test_transform.py View on Github external
def test_sparse_transform():

    # data should not be altered by transform before fitting the model.
    model = AffineWarping()
    data = sparse.random((10, 11, 12), density=.1)

    for dtype in (np.float64, np.float32, np.int64, np.int32):
        X = data.astype(dtype)
        model.fit(X, iterations=0, verbose=False)
        assert_array_equal(X.coords, model.transform(X).coords)
github pydata / sparse / tests / test_compressed.py View on Github external
def test_slicing_errors(index):
    s = sparse.random((2, 3, 4), density=0.5, format='gxcs')

    with pytest.raises(IndexError):
        s[index]
github pydata / sparse / tests / test_coo.py View on Github external
def test_sparsearray_elemwise(format):
    xs = sparse.random((3, 4), density=0.5, format=format)
    ys = sparse.random((3, 4), density=0.5, format=format)

    x = xs.todense()
    y = ys.todense()

    fs = sparse.elemwise(operator.add, xs, ys)
    assert isinstance(fs, COO)

    assert_eq(fs, x + y)
github pydata / sparse / tests / test_coo.py View on Github external
def test_binary_broadcasting(func, shape1, shape2):
    density1 = 1 if np.prod(shape1) == 1 else 0.5
    density2 = 1 if np.prod(shape2) == 1 else 0.5

    xs = sparse.random(shape1, density=density1)
    x = xs.todense()

    ys = sparse.random(shape2, density=density2)
    y = ys.todense()

    expected = func(x, y)
    actual = func(xs, ys)

    assert isinstance(actual, COO)
    assert_eq(expected, actual)

    assert np.count_nonzero(expected) == actual.nnz
github pydata / sparse / tests / test_compressed.py View on Github external
def test_reshape(a, b):
    s = sparse.random(a, density=0.5, format='gxcs')
    x = s.todense()

    assert_eq(x.reshape(b), s.reshape(b))
github pydata / sparse / tests / test_coo.py View on Github external
def test_failed_densification():
    import os
    from importlib import reload

    os.environ['SPARSE_AUTO_DENSIFY'] = '1'
    reload(sparse._settings)

    s = sparse.random((3, 4, 5), density=0.5)
    x = np.array(s)

    assert isinstance(x, np.ndarray)
    assert_eq(s, x)

    del os.environ['SPARSE_AUTO_DENSIFY']
    reload(sparse._settings)
github pydata / sparse / tests / test_coo.py View on Github external
def test_reshape_function():
    s = sparse.random((5, 3), density=0.5)
    x = s.todense()
    shape = (3, 5)

    s2 = np.reshape(s, shape)
    assert isinstance(s2, COO)
    assert_eq(s2, x.reshape(shape))