MCPcopy Create free account
hub / github.com/NVIDIA/DALI / format_tensor

Function format_tensor

dali/python/nvidia/dali/_tensor_formatting.py:242–285  ·  view source on GitHub ↗

Format a single tensor for display. Parameters ---------- obj : Any The tensor object to format. show_data : bool, optional Whether to include the actual data values, by default True. adapter : TensorAdapter, optional Adapter for accessing tensor properti

(obj, show_data: bool = True, adapter: Optional[TensorAdapter] = None)

Source from the content-addressed store, hash-verified

240
241
242def format_tensor(obj, show_data: bool = True, adapter: Optional[TensorAdapter] = None) -> str:
243 """Format a single tensor for display.
244
245 Parameters
246 ----------
247 obj : Any
248 The tensor object to format.
249 show_data : bool, optional
250 Whether to include the actual data values, by default True.
251 adapter : TensorAdapter, optional
252 Adapter for accessing tensor properties. If None, uses PipelineTensorAdapter.
253
254 Returns
255 -------
256 str
257 Formatted string representation of the tensor.
258 """
259
260 if adapter is None:
261 adapter = PipelineTensorAdapter()
262
263 indent = " " * 4
264 edgeitems = 2
265 type_name = adapter.get_type_name(obj)
266 layout = adapter.get_layout(obj)
267 device = adapter.get_device(obj).lower()
268
269 if show_data:
270 data = adapter.to_numpy(obj)
271 data_str = np.array2string(data, prefix=indent, edgeitems=edgeitems)
272 else:
273 data_str = None
274
275 shape = tuple(adapter.get_shape(obj))
276
277 params = (
278 ([f"{data_str}"] if show_data else [])
279 + [f"dtype={adapter.get_dtype(obj)}"]
280 + ([f'layout="{layout}"'] if layout else [])
281 + [f'device="{device}"']
282 + [f"shape={shape})"]
283 )
284
285 return f"{type_name}(\n{indent}" + _join_string(params, sep=",\n" + indent)
286
287
288def format_batch(

Callers 1

_tensor_to_stringFunction · 0.90

Calls 8

to_numpyMethod · 0.95
_join_stringFunction · 0.85
get_type_nameMethod · 0.45
get_layoutMethod · 0.45
get_deviceMethod · 0.45
get_shapeMethod · 0.45
get_dtypeMethod · 0.45

Tested by

no test coverage detected