0
# Mathematical Operations
1
2
Backend-agnostic mathematical operations providing NumPy-compatible APIs, neural network specific functions, and core tensor operations that work across JAX, TensorFlow, PyTorch, and OpenVINO backends.
3
4
## Capabilities
5
6
### Core Operations
7
8
Essential tensor operations and control flow functions.
9
10
```python { .api }
11
def cast(x, dtype):
12
"""Cast tensor to specified data type."""
13
14
def convert_to_tensor(x, dtype=None, sparse=None):
15
"""Convert input to backend tensor."""
16
17
def convert_to_numpy(x):
18
"""Convert tensor to NumPy array."""
19
20
def shape(x):
21
"""Get tensor shape as tuple."""
22
23
def dtype(x):
24
"""Get tensor data type."""
25
26
def stop_gradient(x):
27
"""Stop gradient computation through tensor."""
28
29
def cond(pred, true_fn, false_fn):
30
"""Conditional execution based on predicate."""
31
32
def scan(fn, init, xs, length=None, reverse=False, unroll=1):
33
"""Apply function across sequence elements."""
34
35
def map(fn, xs):
36
"""Map function over tensor elements."""
37
```
38
39
### NumPy-Compatible Operations
40
41
Mathematical functions matching NumPy API for cross-backend compatibility.
42
43
```python { .api }
44
# Array creation
45
def zeros(shape, dtype='float32'): ...
46
def ones(shape, dtype='float32'): ...
47
def eye(N, M=None, k=0, dtype='float32'): ...
48
def arange(start, stop=None, step=1, dtype=None): ...
49
50
# Mathematical operations
51
def add(x1, x2): ...
52
def subtract(x1, x2): ...
53
def multiply(x1, x2): ...
54
def divide(x1, x2): ...
55
def power(x1, x2): ...
56
def sqrt(x): ...
57
def exp(x): ...
58
def log(x): ...
59
def sin(x): ...
60
def cos(x): ...
61
def tanh(x): ...
62
63
# Reduction operations
64
def sum(x, axis=None, keepdims=False): ...
65
def mean(x, axis=None, keepdims=False): ...
66
def max(x, axis=None, keepdims=False): ...
67
def min(x, axis=None, keepdims=False): ...
68
69
# Linear algebra
70
def matmul(x1, x2): ...
71
def dot(x1, x2): ...
72
def transpose(x, axes=None): ...
73
def reshape(x, shape): ...
74
75
# Indexing and manipulation
76
def concatenate(arrays, axis=0): ...
77
def stack(arrays, axis=0): ...
78
def split(x, indices_or_sections, axis=0): ...
79
def squeeze(x, axis=None): ...
80
def expand_dims(x, axis): ...
81
```
82
83
### Neural Network Operations
84
85
Specialized operations for neural networks and deep learning.
86
87
```python { .api }
88
# Activation functions
89
def relu(x): ...
90
def sigmoid(x): ...
91
def softmax(x, axis=-1): ...
92
def gelu(x, approximate=False): ...
93
def silu(x): ... # Also available as swish
94
95
# Convolution operations
96
def conv(inputs, kernel, strides=1, padding='valid', data_format=None,
97
dilation_rate=1, groups=1): ...
98
def conv_transpose(inputs, kernel, strides=1, padding='valid'): ...
99
def depthwise_conv(inputs, kernel, strides=1, padding='valid'): ...
100
101
# Pooling operations
102
def max_pool(inputs, pool_size, strides=None, padding='valid'): ...
103
def average_pool(inputs, pool_size, strides=None, padding='valid'): ...
104
105
# Normalization
106
def batch_normalization(x, mean, variance, offset, scale, epsilon=1e-3): ...
107
def layer_normalization(x, axis=-1, epsilon=1e-3): ...
108
109
# Loss functions
110
def binary_crossentropy(target, output, from_logits=False): ...
111
def categorical_crossentropy(target, output, from_logits=False, axis=-1): ...
112
def sparse_categorical_crossentropy(target, output, from_logits=False, axis=-1): ...
113
114
# Utility functions
115
def one_hot(x, num_classes, axis=-1, dtype='float32'): ...
116
def top_k(x, k, sorted=True): ...
117
def in_top_k(targets, predictions, k): ...
118
```
119
120
### Linear Algebra Operations
121
122
Advanced linear algebra operations for machine learning algorithms.
123
124
```python { .api }
125
def cholesky(x): ...
126
def det(x): ...
127
def eig(x): ...
128
def eigh(x): ...
129
def inv(x): ...
130
def lstsq(a, b, rcond=None): ...
131
def norm(x, ord=None, axis=None, keepdims=False): ...
132
def qr(x, mode='reduced'): ...
133
def solve(a, b): ...
134
def svd(x, full_matrices=True): ...
135
```
136
137
### FFT Operations
138
139
Fast Fourier Transform operations for signal processing.
140
141
```python { .api }
142
def fft(x): ...
143
def fft2(x): ...
144
def rfft(x): ...
145
def irfft(x): ...
146
def stft(x, frame_length, frame_step, fft_length=None): ...
147
def istft(stfts, frame_length, frame_step, fft_length=None): ...
148
```
149
150
## Usage Examples
151
152
### Basic Tensor Operations
153
154
```python
155
import keras
156
from keras import ops
157
158
# Create tensors
159
x = ops.ones((3, 4))
160
y = ops.zeros((3, 4))
161
162
# Mathematical operations
163
z = ops.add(x, y)
164
w = ops.matmul(x, ops.transpose(y))
165
166
# Reductions
167
mean_val = ops.mean(x)
168
sum_val = ops.sum(x, axis=1)
169
```
170
171
### Neural Network Forward Pass
172
173
```python
174
import keras
175
from keras import ops
176
177
def forward_pass(x, weights, bias):
178
# Linear transformation
179
x = ops.matmul(x, weights) + bias
180
181
# Activation
182
x = ops.relu(x)
183
184
# Normalization
185
mean = ops.mean(x, axis=0, keepdims=True)
186
variance = ops.var(x, axis=0, keepdims=True)
187
x = (x - mean) / ops.sqrt(variance + 1e-7)
188
189
return x
190
```