question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Progress calss out of context manager

See original GitHub issue

I’m trying to print the progress of a deep model training. Since I cannot directly modify the training loop I need to use a class CustomLogger method which is called at each iteration. This class stores the total length train_data_len of the loop and the called method on_batch_end. I would like to define a progress class attribute and update it everytime the method is called, like the following:


class CustomLogger(Callback):
    def __init__(
        self, train_data_len=None, eval_data_len=None, logging_step=50
    ):

        self.train_data_len = train_data_len
        self.eval_data_len = eval_data_len
        # define the progress class and add train and eval
        self.progress = Progress()
        self.train_p = self.progress.add_task("Train", total=self.train_data_len)
        self.eval_p = self.progress.add_task("Eval", total=self.eval_data_len)


    def on_epoch_end(self, loss: float, logs: Interaction, epoch: int):
        # at the end of the epoch reset the tasks progress
        self.train_p.reset(total=self.train_data_len)
        self.eval_p.reset(total=self.eval_data_len)

     # called on every loop iteration
    def on_batch_end(
        self, logs: Interaction, loss: float, batch_id: int, is_training: bool
    ): 
        # update the relative task 
            if is_training:
                self.progress.update(self.train_p, advance=1)
            else:
                self.progress.update(self.eval_p, advance=1)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10

github_iconTop GitHub Comments

1reaction
nicofirst1commented, Dec 12, 2020

Indeed it was a Pycharm problem and the solution reported in a previous issue

0reactions
willmcgugancommented, Dec 12, 2020

Ah. There is a note here about a potential fix. But you will probably get better results with a terminal.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context Managers and Python's with Statement
In this step-by-step tutorial, you'll learn what the Python with statement is and how to use it with existing context managers.
Read more >
context manager exit called before code in body
Here is my simple context manager class: class ProgressBar(object): """Progress bar that normalizes progress to [0 - 100] scale" ...
Read more >
A Gentle Introduction to Context Managers: The Pythonic Way ...
Explore with statements and the context manager protocol; Implement context manager class to query MongoDB; Convert try...finally block to ...
Read more >
Context manager in Python - python coding challenges
So the first question that I wanna talk about is what is the "with" statement actually good for? The "with" statement is great...
Read more >
Using python context managers instead of passing arguments
This PyCon 2012 talk summarized several patterns that you'd consider context manager: Open - Close; Lock - Release; Change - Reset ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found