tensor(data: object, dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False)Construct a new Tensor from Python data, a NumPy array, or another Tensor.
Always allocates a fresh storage and copies the source bytes into
Lucid-owned memory. This is the canonical entry point for creating
tensors from heterogeneous Python inputs: scalars (int / float /
bool), nested lists, NumPy ndarray\s, and existing Lucid
Tensor\s. Dtype is inferred from the source unless dtype is
given; device defaults to the global default (typically "cpu") unless
overridden.
Parameters
dataobject- Python scalar (
int,float,bool) — produces a 0-d tensor. - Nested
list/tuple— recursively converted; element type must be uniform. numpy.ndarray— bridge boundary (seelucid._factories.converters); the data is copied regardless of the source array's contiguity.- Existing
Tensor— copied to a new buffer and detached from its autograd graph, so the result is a leaf of its own (useas_tensorto avoid the copy when dtype/device match).
dtypedtype | str | NoneNone (default) infers from data:
integers → int64, floats → float32, complex → complex64;
a Tensor keeps its own dtype.devicedevice | str | None"cpu" or "metal"). None uses
lucid.get_default_device, except that a Tensor stays on its
own device.requires_gradboolFalse.Returns
TensorA freshly-allocated Lucid tensor.
Notes
This factory is one of the six "bridge" entry points in the H4 rule — the only places where external libraries (NumPy here) may legitimately cross into Lucid's compute path. Outside the bridges, Lucid composites must use engine primitives directly.
For zero-copy conversion when the source is already an ndarray on
CPU and shares dtype, prefer as_tensor.
Examples
>>> import lucid
>>> lucid.tensor([1.0, 2.0, 3.0])
tensor([1., 2., 3.])
>>> lucid.tensor([[1, 2], [3, 4]], dtype=lucid.float32)
tensor([[1., 2.],
[3., 4.]])
>>> import numpy as np
>>> lucid.tensor(np.arange(6).reshape(2, 3))
tensor([[0, 1, 2],
[3, 4, 5]], dtype=lucid.int64)
>>> src = lucid.tensor([1.0, 2.0], requires_grad=True)
>>> copy = lucid.tensor(src, dtype=lucid.float64)
>>> copy.dtype, copy.is_leaf, copy.requires_grad
(lucid.float64, True, False)