The author | Ram Sagar compile | sources of vitamin k | Analytics In Diamag
“Library developers no longer have to choose between frameworks.”
Researchers from the Tubingen Center for Artificial Intelligence in Germany introduced EagerPy, a new Python framework that allows developers to write code independent of popular frameworks such as PyTorch and TensorFlow.
In a recently published article on EagerPy, the researchers wrote that library developers no longer focus on framework dependencies. Their new Python framework eagerly addresses their reimplementation and code duplication hurdles.
For example, Foolbox is a Python library built on top of EagerPy. The library was rewritten in EagerPy instead of NumPy to implement development in PyTorch and TensorFlow, and there is only one code base with no code duplication. Foolbox is a library for adversarial attacks on machine learning models.
The importance of frame independence
To address the differences between frameworks, the authors explore syntactic biases. In the case of PyTorch, using in-place gradients requires using **_grad_(), while backpropagation is called using backward**().
However, TensorFlow provides a high-level manager and functions like tape.gradient to query gradients. Even at the syntactic level, the two frameworks are quite different. For example, for parameters, dim vs Axis; For functions, sum vs reduce_sum.
This is where EagerPy comes in. It addresses the differences between PyTorch and TensorFlow by providing a unified API that maps transparently to the various underlying frameworks with no computational overhead.
“EagerPy allows you to write code that automatically uses PyTorch, TensorFlow, JAX and NumPy.”
EagerPy focuses on Eager execution and, in addition, its approach is transparent, allowing users to combine EagerPy code that is framework-independent with framework-specific code, the researchers wrote.
TensorFlow’s introduction of the Eager execution module and PyTorch’s similar features have made the eager execution mainstream and the framework more similar. However, despite these similarities between PyTorch and TensorFlow2, writing framework-independent code is not simple. Syntactically, the apis for automatic differentiation are different in these frameworks.
Automatic differentiation is the use of algorithms to solve differential equations. It works by the chain rule, which means that solving the derivative of a function boils down to basic math (addition, subtraction, multiplication and division). These arithmetic operations can be represented in a graphical format. EagerPy specifically uses a functional method for automatic differentiation.
Here is a code snippet from the documentation:
import eagerpy as ep
x = ep.astensor(x)
def loss_fn(x) :
This function takes and returns an eager tensor
return x.square().sum(a)print(loss_fn(x))
# PyTorchTensor(tensor(14.))
print(ep.value_and_grad(loss_fn, x))
Copy the code
You define the first function and then differentiate it based on its input. Value_and_grad is then passed to ep.value_and_grad to get the value of the function and its gradient.
In addition, the Norm function can now be used with native tensors and arrays in PyTorch, TensorFlow, JAX, and NumPy, with almost no overhead compared to native code. It also applies to GPU tensors.
import torch
norm(torch.tensor([1..2..3.]))
import tensorflow as tf
norm(tf.constant([1..2..3.]))
Copy the code
In summary, EagerPy aims to provide the following features:
-
Provides a unified API for fast execution
-
Maintain native performance of the framework
-
Fully linkable API
-
Full type checking support
The researchers claim that these properties make it easier and safer to use them than the underlying framework-specific apis. Despite these changes and improvements, the team behind EagerPy ensured that the EagerPy API followed the standards set by NumPy, PyTorch, and JAX.
An introduction toEagerPy:
Install the latest version from PyPI using PIP:
python3 -m pip install eagerpy
Copy the code
import eagerpy as ep
def norm(x) :
x = ep.astensor(x)
result = x.square().sum().sqrt()
return result.raw
Copy the code
To learn more information about “eagerpy” : eagerpy. Jonasrauber. DE/guide/autod…
The original link: analyticsindiamag.com/eagerpy-pyt…
Welcome to panchuangai blog: panchuang.net/
Sklearn123.com/
Welcome to docs.panchuang.net/