• 🌐 ΦML • 📖 Documentation • 🔗 API • ▶ Videos • Examples
Need to differentiate but your input is an int
tensor?
Need an int64
tensor but got int32
?
Need a tensor
but got an ndarray
?
Want an ndarray
but your tensor is bound in a computational graph on the GPU?
Worry no longer for ΦML has you covered!
%%capture
!pip install phiml
A major difference between ΦML and its backends is the handling of floating point (FP) precision.
NumPy automatically casts arrays to the highest precision and other ML libraries will raise errors if data types do not match.
Instead, ΦML lets you set the FP precision globally using set_global_precision(64)
or by context and automatically casts tensors to that precision when needed.
The default is FP32 (single precision).
Let's set the global precision to FP64 (double precision)!
from phiml import math
math.set_global_precision(64) # double precision
From now on, all created float tensors will be of type float64
.
math.zeros().dtype
float64
We can run parts of our code with a different precision by executing them within a precision
block:
with math.precision(32):
print(math.zeros().dtype)
float32
ΦML automatically casts tensors to the current precision level during operations.
Say we have a float64
tensor but want to run 32-bit operations.
tensor64 = math.ones()
with math.precision(32):
print(math.sin(tensor64).dtype)
float32
Here, the tensor was cast to float32
before applying the sin
function.
If you want to explicitly cast a tensor to the current precision, use math.to_float()
This system precludes any precision conflicts and you will never accidentally execute code with the wrong precision!
from phiml.math import DType
math.zeros(dtype=DType(float, 16))
float16 0.0
Note that there are no global variables for data types. Simplify specify the kind and bit-length in the DType
constructor.
Actually, the explicit constructor call to DType()
is not necessary. You can also pass the kind and bit-length as a tuple
.
math.zeros(dtype=(float, 16))
float16 0.0
In most cases, you want the bit-length to match the current floating point precision.
Then, just specify the kind of data type (bool
, int
, float
, or complex
).
math.zeros(dtype=int)
0
math.zeros(dtype=complex)
complex128 0j
math.zeros(dtype=bool)
False
Advantages of Precision Management with examples.
🌐 ΦML • 📖 Documentation • 🔗 API • ▶ Videos • Examples