machine-learningwandb

wandb - Access logged values during runtime


How can I retrieve a logged value from wandb before the run was finished?

import os
import wandb
wandb.init(project='someproject')


def loss_a():
    # do_stuff and log:
    wandb.log({"loss_a": 1.0})
    
def loss_b():
    # do_stuff and log:
    wandb.log({"loss_b": 2.0})

for epoch in range(2):
    loss_a()
    loss_b()
    
    # somehow retrieve loss_a and loss_b and print them here:
    print(f'loss_a={??}, loss_b={??}')

After run was finished I can find it with wandb.Api to get run.history. But it seems that before run was fininshed, accessing run.history doesn't work.


Solution

  • You can retrieve a logged value from wandb before the run was finished by using wandb.run.summary. It's a dictionary that holds the last value being logged for a specific key name. Check out this link.

    You can also check out this colab notebook to try it out yourself.

    import os
    import wandb
    wandb.init(project='someproject')
    
    
    def loss_a():
        # do_stuff and log:
        wandb.log({"loss_a": 1.0})
        
    def loss_b():
        # do_stuff and log:
        wandb.log({"loss_b": 2.0})
    
    for epoch in range(2):
        loss_a()
        loss_b()
        
        # somehow retrieve loss_a and loss_b and print them here:
        print(f'loss_a={wandb.run.summary["loss_a"]}, loss_b={wandb.run.summary["loss_b"]}')