Git Product home page Git Product logo

latorch's Introduction

LaTorch

LaTorch is a simple-to-use library to convert LaTeX-like tensor expressions into PyTorch models. It was developed with numerical relativity in mind and, as such, also handles automatic index highering and lowering.

How to Use

Let us start with a simple example: the product between two $3 \times 3$ matrices

$$ A^i_{ㅤj}ㅤB^j_{ㅤi} $$

which, in LaTeX, would be written as

A^{i}_{j} B^{j}_{i}

We can use LaTorch to translate this expression into a PyTorch model with the following code

import latorch as lt

latex = u'#^{i}_{k} = A^{i}_{j} B^{j}_{k}'
symbols = [
    'A__',
    'B__',
    '@g__'
    ]

Here, we define two new variables that we will use as the input to create the PyTorch model. The string latex stores the latex expression, while the string symbols defines which latex symbols we want to use as arguments for our model. More specifically, this variable also indicates the form in which we will pass the arguments. For example, in this case, A__ and B__ specify that both the A and B tensors are going to be passed as arguments with both indices down. This is indicated by the two '_' after the name of the variable. In case we want to pass the tensor with an up index we can use '^' instead of '_'. Finally, the '@' before g indicates that g is the metric and can be used for index highering and lowering.

model = lt.LaModel(latex, symbols)

In this line, we create the PyTorch model for the above expression.

result = model.forward({
    'A': torch.randn(3,3),
    'B': torch.randn(3,3),
    'g': torch.eye(3, 3)
    })

By calling the .forward(...) method we can evaluate the model. In particular, this method accepts a dictionary as an argument, where the keys are the symbol names chosen in the symbols variable above (without possible indices) and the values are those that these variables should assume for the computation.

Let us now showcase a more complex example

latex = u'#^{i}_{j} = e^(\phi - 2) \cdot A^{i}_{m}^{m}_{n} (\Gamma^{n}_{j} + \Gamma_{j}^{n})'

symbols = [
    'A^_^^',
    '\Gamma__',
    'e',
    '\phi',
    '@g__'
]

model = LaModel(latex, symbols)

result = model.forward({
    'A': torch.randn(3,3,3,3),
    '\Gamma': torch.randn(3,3),
    'e': torch.tensor(2.71),
    '\phi': torch.randn(1),
    'g': torch.eye(3, 3)
})

As it can be seen by this code snippet, to define a scalar argument we simply don't put any '_' or '^' to its right. We can also see that LaTorch can handle Greek letters for variables as well. In fact, it can handle any type of complex LaTeX expression used to represent a variable, like '\bar{\tensor[^{(3)}]{\Delta}{_{\text{TF}}}'.

Limitations

latorch's People

Contributors

lbarazza avatar

Watchers

 avatar Kostas Georgiou 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.