API Reference¶
Tensor¶
- class jamgrad.tensor.Tensor(data, requires_grad=False, grad_fn=None)¶
Bases:
object
A tensor class with automatic differentiation capabilities.
This class wraps numpy arrays and provides automatic gradient computation for backpropagation in neural networks and optimization algorithms.
- Parameters:
data – Input data as array-like object
requires_grad (bool) – Whether to compute gradients for this tensor
grad_fn (callable, optional) – Function to compute gradients during backprop
- data¶
The underlying numpy array data
- Type:
np.ndarray
- requires_grad¶
Whether gradients are computed for this tensor
- Type:
bool
- grad¶
Accumulated gradients, None until backward() is called
- Type:
np.ndarray
- grad_fn¶
Gradient function for backpropagation
- Type:
callable
Examples
>>> x = Tensor([1.0, 2.0], requires_grad=True) >>> y = x ** 2 >>> y.backward(np.ones_like(y.data)) >>> print(x.grad) # [2.0, 4.0]
- backward(gradient=None)¶
Compute gradients via backpropagation.
- Parameters:
gradient (np.ndarray, optional) – Gradient from upstream computation. If None, assumes gradient of ones (for scalar outputs).
Note
This method accumulates gradients in the .grad attribute and propagates gradients backward through the computation graph.
- exp()¶
Element-wise exponential function.
- Returns:
e^x for each element x in the tensor
- Return type:
- log()¶
Element-wise natural logarithm.
- Returns:
ln(x) for each element x in the tensor
- Return type:
- property ndim¶
Number of dimensions of the tensor data.
- set_label(label)¶
Set a human-readable label for this tensor.
- Parameters:
label (str) – Label for the tensor (e.g., ‘x’, ‘weight’, ‘loss’)
- Returns:
Self for method chaining
- Return type:
- property shape¶
- sum(axis=None)¶
Sum reduction along specified axis.
- Parameters:
axis (int or tuple, optional) – Axis or axes along which to sum. If None, sum all elements.
- Returns:
Sum of tensor elements with gradient support
- Return type:
- to_dot()¶
Generate a DOT graph representation of the computation graph (left-to-right).
- Returns:
DOT format string representing the computation graph
- Return type:
str
- jamgrad.tensor.get_op_id(tensor)¶
- jamgrad.tensor.get_tensor_id(tensor)¶
- jamgrad.tensor.get_tensor_label(tensor)¶
Build readable label for a tensor node.
- jamgrad.tensor.traverse(tensor, visited_tensors, visited_ops, edges, tensor_nodes, op_nodes)¶