MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / _format_tensor

Function _format_tensor

python/paddle/tensor/to_string.py:181–252  ·  view source on GitHub ↗

Format a tensor Args: var(Tensor): The tensor to be formatted. summary(bool): Do summary or not. If true, some elements will not be printed, and be replaced with "...". indent(int): The indent of each line. max_width(int): The max width of each elements in v

(var, summary, indent=0, max_width=0, signed=False)

Source from the content-addressed store, hash-verified

179
180
181def _format_tensor(var, summary, indent=0, max_width=0, signed=False):
182 """
183 Format a tensor
184
185 Args:
186 var(Tensor): The tensor to be formatted.
187 summary(bool): Do summary or not. If true, some elements will not be printed, and be replaced with "...".
188 indent(int): The indent of each line.
189 max_width(int): The max width of each elements in var.
190 signed(bool): Print +/- or not.
191 """
192 edgeitems = DEFAULT_PRINT_OPTIONS.edgeitems
193 linewidth = DEFAULT_PRINT_OPTIONS.linewidth
194
195 if len(var.shape) == 0:
196 # 0-D Tensor, whose shape = [], should be formatted like this.
197 return _format_item(var, max_width, signed)
198 elif len(var.shape) == 1:
199 item_length = max_width + 2
200 items_per_line = max(1, (linewidth - indent) // item_length)
201
202 if summary and var.shape[0] > 2 * edgeitems:
203 items = (
204 [
205 _format_item(var[i], max_width, signed)
206 for i in range(edgeitems)
207 ]
208 + ['...']
209 + [
210 _format_item(var[i], max_width, signed)
211 for i in range(var.shape[0] - edgeitems, var.shape[0])
212 ]
213 )
214 else:
215 items = [
216 _format_item(var[i], max_width, signed)
217 for i in range(var.shape[0])
218 ]
219 lines = [
220 items[i : i + items_per_line]
221 for i in range(0, len(items), items_per_line)
222 ]
223 s = (',\n' + ' ' * (indent + 1)).join(
224 [', '.join(line) for line in lines]
225 )
226 return '[' + s + ']'
227 else:
228 # recursively handle all dimensions
229 if summary and var.shape[0] > 2 * edgeitems:
230 vars = (
231 [
232 _format_tensor(
233 var[i], summary, indent + 1, max_width, signed
234 )
235 for i in range(edgeitems)
236 ]
237 + ['...']
238 + [

Callers 2

to_stringFunction · 0.85
_format_dense_tensorFunction · 0.85

Calls 4

_format_itemFunction · 0.85
rangeFunction · 0.85
maxFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected