GithubHelp home page GithubHelp logo

leiloong / knowledge-propagation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pminervini/knowledge-propagation

0.0 2.0 0.0 5.59 MB

A framework implementing several techniques for adaptively propagating label information in knowledge graphs.

Python 100.00%

knowledge-propagation's Introduction

knowledge propagation

A framework implementing several techniques for propagating label information in graphs.

Installing

knowledge-propagation$ python3 setup.py install --user

Usage

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np

import theano
import theano.tensor as T

from kprop.gaussianfields.model import GaussianFields


def make_minimizer():
    L, y = T.ivector('L'), T.dvector('y')
    mu, W, eps = T.dscalar('mu'), T.dmatrix('W'), T.dscalar('eps')
    return theano.function([L, y, mu, W, eps], GaussianFields(L, y, mu, W, eps).minimize())

if __name__ == '__main__':
    # Number of instances
    N = 6

    # Similarity relations between instances (nodes in an undirected graph)
    edges = [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]

    # Labeled examples
    L = np.array([1, 0, 0, 0, 0, 1], dtype='int8')

    # Labels
    y = np.array([1, 0, 0, 0, 0, -1], dtype='float32')

    # Let's build the similarity matrix: W[i, j] = 1 iff x_i and x_j are similar, 0 otherwise.
    W = np.zeros((N, N))
    for (xi, xj) in edges:
        W[xi, xj] = W[xj, xi] = 1

    # Let's instantiate the function that takes care of the propagation process
    minimizer = make_minimizer()

    print(minimizer(L, y, 1.0, W, 1e-8))

Result:

$ python3 hello.py
[ 0.71428571  0.42857142  0.14285714 -0.14285714 -0.42857142 -0.71428571]

Example

In the following example, the graph is structured as a grid: there's a positive example on the upper left corner of the grid, and a negative example on the lower right corner. The higher the bar associated to the node, the higher the confidence in the classification.

knowledge-propagation$ python3 -uB examples/grid.py
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ █  ▇  ▆  ▅  ▅  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▇  ▆  ▆  ▅  ▅  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▆  ▆  ▅  ▅  ▅  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▅  ▅  ▅  ▅  ▄  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▅  ▅  ▅  ▄  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▄  ▄  ▄  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▄  ▄  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▄  ▄  ▄  ▄  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▃  ▃  ▃  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                      │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                            │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                               │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                     │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                        │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                           │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                              │
│ ▂  ▂  ▂  ▂  ▂  ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                 │
│ ▂  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                    │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                       │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                          │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                             │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                                │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                                   │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                                   │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                                      │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                              ▁  ▁  ▁  ▁ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                           ▁  ▁  ▁  ▁  ▁ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                        ▁  ▁  ▁  ▁  ▁  ▁  ▁ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                        ▁  ▁  ▁  ▁  ▁  ▁  ▂ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                        ▁  ▁  ▁  ▁  ▁  ▂  ▂  ▂ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                     ▁  ▁  ▁  ▁  ▁  ▂  ▂  ▂  ▂ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                     ▁  ▁  ▁  ▁  ▂  ▂  ▂  ▃  ▃ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                     ▁  ▁  ▁  ▁  ▂  ▂  ▃  ▃  ▄ │
│ ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁  ▁                                                     ▁  ▁  ▁  ▂  ▂  ▂  ▃  ▄  ▆ │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

knowledge-propagation's People

Watchers

 avatar  avatar

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.