# tinker.types.TensorData

## _class_ [**tinker.types.TensorData**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L25)()

**Fields:**

- [**dtype**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L26) ( _[TensorDtype](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_dtype.py#L5)_)  
- [**shape**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L28) ( _Optional\[List\[int\]\]_ ) – Optional.

The shape of the tensor (see PyTorch tensor.shape). The shape of a one-dimensional list of length N is `(N,)`. Can usually be inferred if not provided, and is generally inferred as a 1D tensor.

- [**sparse_crow_indices**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L36) ( _Optional\[List\[int\]\]_ ) – Optional CSR compressed row pointers. When set, this tensor is sparse CSR:
  - data contains only the non-zero values (flattened)
  - sparse_crow_indices contains the row pointers (length = nrows + 1)
  - sparse_col_indices contains the column indices (length = nnz)
  - shape is required and specifies the dense shape
- [**sparse_col_indices**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L44) ( _Optional\[List\[int\]\_ ) – Optional CSR column indices. Must be set together with sparse_crow_indices.

### _property_ [**data**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L82)

Flattened tensor data as array of numbers.

**Returns:** _Union\[List\[int\], List\[float\]\]_

### [**from_torch_sparse**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L104)( _tensor_ )

Create a sparse CSR TensorData from a dense 2-D torch tensor.

Automatically detects sparsity and encodes as CSR when it saves space. Falls back to dense if the tensor is 1-D or mostly non-zero.

**Parameters:**

- [**tensor**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L104) ( _torch.Tensor_ )

**Returns:** _[TensorData](https://tinker-docs.thinking-machines.ai/tinker/api-reference/types/tensordata/)_

### [**to_numpy**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L134)()

Convert TensorData to numpy array.

**Returns:** _npt.NDArray\[Any\]_

### [**to_torch**](https://github.com/thinking-machines-lab/tinker/blob/main/src/tinker/types/tensor_data.py#L140)()

Convert TensorData to torch tensor.

**Returns:** _torch.Tensor_

## Referenced by

- [tinker.types.ForwardBackwardOutput](https://tinker-docs.thinking-machines.ai/tinker/api-reference/types/forwardbackwardoutput/)
