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

Function _format_item

python/paddle/tensor/to_string.py:127–165  ·  view source on GitHub ↗
(np_var, max_width=0, signed=False)

Source from the content-addressed store, hash-verified

125
126
127def _format_item(np_var, max_width=0, signed=False):
128 if (
129 np_var.dtype == np.float32
130 or np_var.dtype == np.float64
131 or np_var.dtype == np.float16
132 ):
133 if DEFAULT_PRINT_OPTIONS.sci_mode:
134 item_str = f'{np_var:.{DEFAULT_PRINT_OPTIONS.precision}e}'
135 elif np.ceil(np_var) == np_var:
136 item_str = f'{np_var:.0f}.'
137 else:
138 item_str = f'{np_var:.{DEFAULT_PRINT_OPTIONS.precision}f}'
139 elif np_var.dtype == np.complex64 or np_var.dtype == np.complex128:
140 re = np.real(np_var)
141 im = np.imag(np_var)
142 prec = DEFAULT_PRINT_OPTIONS.precision
143 if DEFAULT_PRINT_OPTIONS.sci_mode:
144 if im >= 0:
145 item_str = f'({re:.{prec}e}+{im:.{prec}e}j)'
146 else:
147 item_str = f'({re:.{prec}e}{im:.{prec}e}j)'
148 else:
149 if im >= 0:
150 item_str = f'({re:.{prec}f}+{im:.{prec}f}j)'
151 else:
152 item_str = f'({re:.{prec}f}{im:.{prec}f}j)'
153 else:
154 item_str = f'{np_var}'
155
156 if max_width > len(item_str):
157 if signed: # handle sign character for tensor with negative item
158 if np_var < 0:
159 return item_str.ljust(max_width)
160 else:
161 return ' ' + item_str.ljust(max_width - 1)
162 else:
163 return item_str.ljust(max_width)
164 else: # used for _get_max_width
165 return item_str
166
167
168def _get_max_width(var):

Calls

no outgoing calls