0

I came across this code in PyTorch's example code:

if batch % 100 == 0:
     loss, current = loss.item(), (batch + 1) * len(X)
     print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")

and

print(f"Test Error: \n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \n")  

Assuming it may have something to do with decimal places or the number of digits to display, I tried some sample code like:

size=46323656
current=3
loss=4.6362635675
print(f"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]")

and the output is:

loss: 4.636264  [    3/46323656]

But the output makes no sense. Does anyone know what the ":>" is meant for? I've heard of the Walrus operator.

Nav
  • 19,885
  • 27
  • 92
  • 135
  • 2
    It's not an operator, `:` is just a separator and `>` is the `align` part of the `format_spec`: https://docs.python.org/3/library/string.html#format-string-syntax – jonrsharpe Mar 28 '23 at 16:29
  • It's not an operator. It's special syntax used within a template string, in order to describe how a value will be formatted into a placeholder. Just like how, with old-fashioned `%`-style formatting, the `%` outside the string was an operator, but not the `%s` patterns inside the string. – Karl Knechtel Mar 28 '23 at 16:30
  • Thanks. I marked the question as a duplicate. It'll be helpful for my question to be here, since when people get puzzled and search the internet, they'd typically use the words I did. – Nav Mar 28 '23 at 16:30
  • 1
    Yes, it looks like a reasonable signpost. – Karl Knechtel Mar 28 '23 at 16:31
  • "it is a penguin". (not really) – jsbueno Mar 28 '23 at 16:45

0 Answers0