All contributions are welcome! So if you have a cool tensor method you want to add, if you spot a bug or even a typo or mistake in the documentation, please report it, and even better, open a Pull-Request!
To add code of fix issues in TensorLy, you will want to open a Pull-Request on the Github repository of the project.
For each function or class, we expect helpful docstrings in the NumPy format, as well as unit-tests to make sure it is working as expected (especially helpful for future refactoring to make sure no exising code is broken!)
Check the existing code for examples, and don’t hesitate to contact the developers if you are unsure!
To contribute code to the TensorLy code-base, you must ensure compatibility with all the backends.
We want algorithms to run transparently with all the TensorLy backends (NumPy, MXNet, PyTorch, TensorLy, JAX, CuPy) and any other backend added later on!
This means you should only use TensorLy functions, never directly a function from the backend
To do so, we only use functions wrapped in
tensorly.backend, such as
If the function you need doesn’t exist, either try using other existing ones,
or, if you cannot do otherwise, add the required function to all backends.
In general, you should not use backend specific code, by testing for the backend.
e.g. Do not include statements such as
if tensorly.get_backend() == 'pytorch' in your code.
Practically, use the wrapped functions. For instance:
import tensorly as tl import numpy as np tensor = tl.tensor(np.random.random((10, 10, 10))) # DO THIS: min_value = tl.min(tensor) # DO NOT DO THIS: min_value = np.min(tensor) # Don't do it!
The reason is that you do not want your code to be restricted to any of the backends. You might be using NumPy but another user might be using MXNet and calling a NumPy function on an MXNet NDArray will most likely fail.
Context of a tensor¶
An other aspect, when developing a new function or algorithm, is to make sure you perform the computation on the correct context specified by the user. To do so, always get the context from tensors you get as input, and use it for the tensors you create.
context = tl.context(tensor) # when creating a new tensor we use these as parameters new_tensor = tl.tensor(tensor + 2, **context)
Check-out the page on TensorLy’s backend system for more on this.
Index assignment (“NumPy style”)¶
In NumPy, PyTorch and MXNet, you can combined indexing and assignment in a convenient way,
e.g. if you have a tensor t, you can update its values for given indices using the expression
t[indices] = values.
Unfortunately, this is not supported by TensorFlow or JAX. As a result, if you want to do this,
you should use
For instance, the previous statement becomes, in TensorLy:
t = tensorly.index_update(t, tensorly.index[indices], values).