Giter VIP home page Giter VIP logo

sparsebm's Introduction

Getting started with SparseBM

SparseBM is a python module for handling sparse graphs with Block Models. The module is an implementation of the variational inference algorithm for the Stochastic Block Model (SBM) and the Latent Block Model (LBM) for sparse graphs, which leverages the sparsity of edges to scale to very large numbers of nodes. The module can use Cupy to take advantage of the hardware acceleration provided by graphics processing units (GPU).

Installing

The SparseBM module is distributed through the PyPI repository and the documentation is available at sparsebm.readthedocs.io.

With GPU acceleration (recommended if GPUs are available)

This option is recommended if GPUs are available to speedup computation.

With the package installer pip:

pip3 install sparsebm[gpu]

The Cupy module will be installed as a dependency.

Alternatively Cupy can be installed separately, and will be used by sparsebm if available.

pip3 install sparsebm
pip3 install cupy

Without GPU acceleration

Without GPU acceleration, only CPUs are used. The infererence process still uses sparsity, but no GPU linear algebra operations.

pip3 install sparsebm

For users who do not have GPU, we recommend the free serverless Jupyter notebook environment provided by Google Colab where the Cupy module is already installed and ready to be used with a GPU.

Example with the Stochastic Block Model

  • Generate a synthetic graph for analysis with SBM:

    from sparsebm import generate_SBM_dataset
    
    dataset = generate_SBM_dataset(symmetric=True)
    graph = dataset["data"]
    cluster_indicator = dataset["cluster_indicator"]
  • Infer with the Bernoulli Stochastic Bloc Model:

    from sparsebm import SBM
    
    number_of_clusters = cluster_indicator.shape[1]
    
    # A number of classes must be specified. Otherwise see model selection.
    model = SBM(number_of_clusters)
    model.fit(graph, symmetric=True)
    print("Labels:", model.labels)
  • Compute performance:

    from sparsebm.utils import ARI
    ari = ARI(cluster_indicator.argmax(1), model.labels)
    print("Adjusted Rand index is {:.2f}".format(ari))

Example with the Latent Block Model

  • Generate a synthetic graph for analysis with LBM:

    from sparsebm import generate_LBM_dataset
    
    dataset = generate_LBM_dataset()
    graph = dataset["data"]
    row_cluster_indicator = dataset["row_cluster_indicator"]
    column_cluster_indicator = dataset["column_cluster_indicator"]
  • Use the Bernoulli Latent Bloc Model:

    from sparsebm import LBM
    
    number_of_row_clusters = row_cluster_indicator.shape[1]
    number_of_columns_clusters = column_cluster_indicator.shape[1]
    
    # A number of classes must be specified. Otherwise see model selection.
    model = LBM(
        number_of_row_clusters,
        number_of_columns_clusters,
        n_init_total_run=1,
    )
    model.fit(graph)
    print("Row Labels:", model.row_labels)
    print("Column Labels:", model.column_labels)
  • Compute performance:

    from sparsebm.utils import CARI
    cari = CARI(
        row_cluster_indicator.argmax(1),
        column_cluster_indicator.argmax(1),
        model.row_labels,
        model.column_labels,
    )
    print("Co-Adjusted Rand index is {:.2f}".format(cari))

sparsebm's People

Contributors

jb-leger avatar yvesgrandvalet avatar remi-boutin avatar

Forkers

yvesgrandvalet

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.