PNTensor¶
danling.tensor.tensor
¶
PNTensor
¶
Bases: Tensor
A tensor wrapper that enables automatic collation into NestedTensor with PyTorch DataLoader.
PNTensor
(Potential Nested Tensor) seamlessly bridges the gap between individual tensors
and batched NestedTensor
objects in PyTorch workflows. It’s designed specifically to work
with PyTorch’s DataLoader collation mechanism, allowing datasets to return variable-length
tensors that will automatically be combined into a NestedTensor
when batched.
The class provides three properties that mirror those of NestedTensor:
- .tensor
: The tensor itself (self)
- .mask
: A tensor of ones with the same shape as self
- .concat
: The tensor itself (self)
Examples:
Basic usage with PyTorch DataLoader:
Using PNTensor directly:
Python Console Session | |
---|---|
Source code in danling/tensor/tensor.py
Python | |
---|---|
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
tensor
¶
tensor(data: Any, dtype=None, device=None, requires_grad: bool = False, pin_memory: bool = False) -> PNTensor
Create a PNTensor from data, similar to torch.tensor() but returning a PNTensor.
This function is a convenient way to create PNTensor objects that will be automatically collated into NestedTensor when used with PyTorch DataLoader. The interface mirrors torch.tensor() to make it easy to switch between regular tensors and PNTensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Any
|
Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, etc. |
required |
|
Desired data type of the returned tensor. |
None
|
|
|
Device on which to place the tensor. |
None
|
|
|
bool
|
If autograd should record operations on the returned tensor. |
False
|
|
bool
|
If True, the tensor will be allocated in pinned memory. |
False
|
Returns:
Name | Type | Description |
---|---|---|
PNTensor |
PNTensor
|
A tensor wrapper that will be automatically collated into NestedTensor |
Examples:
Python Console Session | |
---|---|