You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to propose prettier printing of tensors. Here is an example of the code that I would suggest for a dense tensor. Ideally, this would be the default print. It allows the user to indicate the formatting and the name. It always prints the frontal slices, regardless of the underlying storage format.
def pretty_print_tensor(X, fmt="10.4f", name="Slice"):
if not isinstance(X, ttb.tensor):
raise ValueError("Input must be a pyttb tensor")
# Get the shape of the tensor
shape = X.shape
if name == "Slice":
print("Tensor is of shape "+ " x ".join(map(str, shape)))
else:
print(f"{name} is a tensor of shape " + " x ".join(map(str, shape)))
# Iterate over all possible slices (in Fortran order)
for index in np.ndindex(shape[2:][::-1]): # Skip the first two dimensions and reverse the order
index = index[::-1] # Reverse the order back to the original
# Construct the slice indices
slice_indices = (slice(None), slice(None)) + index
slice_data = X[slice_indices]
print(f"{name}(:, :, {', '.join(map(str, index))}) =")
array = slice_data.data
for row in array:
print(" ".join(f"{val:{fmt}}" for val in row))
The text was updated successfully, but these errors were encountered:
I would like to propose prettier printing of tensors. Here is an example of the code that I would suggest for a dense tensor. Ideally, this would be the default print. It allows the user to indicate the formatting and the name. It always prints the frontal slices, regardless of the underlying storage format.
The text was updated successfully, but these errors were encountered: