Giter VIP home page Giter VIP logo

semiring_torch's Introduction

semiring_torch

Run your pytorch code on any semiring with a single line of code! Semiring_torch is built on top of autoray.

Warning: this is a proof of concept. Expect bugs and missing features.

Installation

pip install semiring-torch

Example

By using the logarithmic semiring, you can easily write numerically stable code. In the following example, we compute a matrix product in log-space.

Regular torch semiring_torch
import torch

x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
x1 = x1.log()
x2 = x2.log()
result = x1[:, :, None] + x2[None, :, :]
result = torch.logsumexp(result, dim=1)
result = result.exp()
from autoray import numpy as torch
from semiring_torch import logarithmic_semiring

with logarithmic_semiring:
    x1 = torch.tensor([[0.1, 0.6], [0.1, 0.4]])
    x2 = torch.tensor([[0.5, 0.3], [0.2, 0.1]])
    result = x1 @ x2

Usage

Semiring_torch only supports a subset of torch operations. In general, methods that only rely on additions and multiplications are supported (e.g. torch.trace or torch.dot), as well as methods that only affect the shape of the tensor (e.g. x.reshape or x.transpose). Functionality that goes beyond a semiring (e.g. torch.sin) will raise a runtime error.

Currently only the logarithmic semiring is supported, but more semirings can be added easily.

semiring_torch's People

Contributors

jjcmoon avatar

Stargazers

bouna Nabgha avatar Jeff Carpenter avatar elucida avatar Adrián Javaloy avatar Yu Zhang avatar Felipe Menegazzi avatar Hanlin Zhang avatar Songlin Yang avatar antonio vergari avatar Lorenzo Loconte avatar

Watchers

 avatar Lorenzo Loconte 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.