import torch
import matplotlib.pyplot as plt
import random
Foundations
Foundations
Callbacks as GUI events
import ipywidgets as widgets
From the ipywidget docs:
- the button widget is used to handle mouse clicks. The on_click method of the Button can be used to register function to be called when the button is clicked
= widgets.Button(description='Click me') w
w
def f(o): print('hi')
w.on_click(f)
NB: When callbacks are used in this way they are often called βeventsβ.
Creating your own callback
from time import sleep
from tqdm import trange
def slow_calculation():
= 0
res for i in trange(5):
+= i*i
res 1)
sleep(return res
slow_calculation()
100%|βββββββββββββββββββββββββ| 5/5 [00:05<00:00, 1.00s/it]
30
def slow_calculation(cb=None):
= 0
res for i in range(5):
+= i*i
res 1)
sleep(if cb: cb(i)
return res
def show_progress(epoch): print(f"Awesome! We've finished epoch {epoch}!")
slow_calculation(show_progress)
Awesome! We've finished epoch 0!
Awesome! We've finished epoch 1!
Awesome! We've finished epoch 2!
Awesome! We've finished epoch 3!
Awesome! We've finished epoch 4!
30
Lambdas and partials
lambda o: print(f"Awesome! We've finished epoch {o}!")) slow_calculation(
Awesome! We've finished epoch 0!
Awesome! We've finished epoch 1!
Awesome! We've finished epoch 2!
Awesome! We've finished epoch 3!
Awesome! We've finished epoch 4!
30
def show_progress(exclamation, epoch): print(f"{exclamation}! We've finished epoch {epoch}!")
lambda o: show_progress("OK I guess", o)) slow_calculation(
OK I guess! We've finished epoch 0!
OK I guess! We've finished epoch 1!
OK I guess! We've finished epoch 2!
OK I guess! We've finished epoch 3!
OK I guess! We've finished epoch 4!
30
def make_show_progress(exclamation):
def _inner(epoch): print(f"{exclamation}! We've finished epoch {epoch}!")
return _inner
"Nice!")) slow_calculation(make_show_progress(
Nice!! We've finished epoch 0!
Nice!! We've finished epoch 1!
Nice!! We've finished epoch 2!
Nice!! We've finished epoch 3!
Nice!! We've finished epoch 4!
30
from functools import partial
"OK I guess")) slow_calculation(partial(show_progress,
OK I guess! We've finished epoch 0!
OK I guess! We've finished epoch 1!
OK I guess! We've finished epoch 2!
OK I guess! We've finished epoch 3!
OK I guess! We've finished epoch 4!
30
= partial(show_progress, "OK I guess") f2
Callbacks as callable classes
class ProgressShowingCallback():
def __init__(self, exclamation="Awesome"): self.exclamation = exclamation
def __call__(self, epoch): print(f"{self.exclamation}! We've finished epoch {epoch}!")
= ProgressShowingCallback("Just super") cb
slow_calculation(cb)
Just super! We've finished epoch 0!
Just super! We've finished epoch 1!
Just super! We've finished epoch 2!
Just super! We've finished epoch 3!
Just super! We've finished epoch 4!
30
Multiple callback funcs; *args
and **kwargs
def f(*a, **b): print(f"args: {a}, {type(a)}; kwargs: {b}")
3,'a', 1, thing2 = "asd", thing1="hello") f(
args: (3, 'a', 1), <class 'tuple'>; kwargs: {'thing2': 'asd', 'thing1': 'hello'}
def g(a,b,c=0): print(a,b,c)
= [1,2]
args = {'c':3}
kwargs *args, **kwargs) g(
1 2 3
def slow_calculation(cb=None):
= 0
res for i in range(5):
if cb: cb.before_calc(i)
+= i*i
res 1)
sleep(if cb: cb.after_calc(i, val=res)
return res
class PrintStepCallback():
def before_calc(self, *args, **kwargs): print(f"About to start")
def after_calc (self, *args, **kwargs): print(f"Done step")
slow_calculation(PrintStepCallback())
About to start
Done step
About to start
Done step
About to start
Done step
About to start
Done step
About to start
Done step
30
class PrintStatusCallback():
def __init__(self): pass
def before_calc(self, epoch, **kwargs): print(f"About to start: {epoch}")
def after_calc (self, epoch, val, **kwargs): print(f"After {epoch}: {val}")
slow_calculation(PrintStatusCallback())
About to start: 0
After 0: 0
About to start: 1
After 1: 1
About to start: 2
After 2: 5
About to start: 3
After 3: 14
About to start: 4
After 4: 30
30
Modifying behavior
def slow_calculation(cb=None):
= 0
res for i in range(5):
if cb and hasattr(cb,'before_calc'): cb.before_calc(i)
+= i*i
res 1)
sleep(if cb and hasattr(cb,'after_calc'):
if cb.after_calc(i, res):
print("stopping early")
break
return res
class PrintAfterCallback():
def after_calc (self, epoch, val):
print(f"After {epoch}: {val}")
if val>10: return True
slow_calculation(PrintAfterCallback())
After 0: 0
After 1: 1
After 2: 5
After 3: 14
stopping early
14
class SlowCalculator():
def __init__(self, cb=None): self.cb,self.res = cb,0
def callback(self, cb_name, *args):
if not self.cb: return
= getattr(self.cb,cb_name, None)
cb if cb: return cb(self, *args)
def calc(self):
for i in range(5):
self.callback('before_calc', i)
self.res += i*i
1)
sleep(if self.callback('after_calc', i):
print("stopping early")
break
class ModifyingCallback():
def after_calc (self, calc, epoch):
print(f"After {epoch}: {calc.res}")
if calc.res>10: return True
if calc.res<3: calc.res = calc.res*2
= SlowCalculator(ModifyingCallback()) calculator
calculator.calc() calculator.res
After 0: 0
After 1: 1
After 2: 6
After 3: 15
stopping early
15
__dunder__
thingies
Anything that looks like __this__
is, in some way, special. Python, or some library, can define some functions that they will call at certain documented times. For instance, when your class is setting up a new object, python will call __init__
. These are defined as part of the python data model.
For instance, if python sees +
, then it will call the special method __add__
. If you try to display an object in Jupyter (or lots of other places in Python) it will call __repr__
.
class SloppyAdder():
def __init__(self,o): self.o=o
def __add__(self,b): return SloppyAdder(self.o + b.o + 0.01)
def __repr__(self): return str(self.o)
= SloppyAdder(1)
a = SloppyAdder(2)
b +b+a a
4.02
Special methods you should probably know about (see data model link above) are:
__getitem__
__getattr__
__setattr__
__del__
__init__
__new__
__enter__
__exit__
__len__
__repr__
__str__
__getattr__
and getattr
class A:
=1,2 a,b
= A() a
a.b
2
getattr(a, 'b')
2
getattr(a, 'b' if random.random()>0.5 else 'a')
1
class B:
=1,2
a,bdef __getattr__(self, k):
if k[0]=='_': raise AttributeError(k)
return f'Hello from {k}'
= B() b
b.a
1
b.foo
'Hello from foo'