In [1]:
import numpy as np
import holoviews as hv
hv.extension("bokeh")
In [2]:
def BCE(y, y_hat):
return y_hat, -y * np.log(y_hat) - (1 - y) * np.log(1 - y_hat)
def wBCE(y, y_hat):
return y_hat, -y * np.log(y_hat) + y_hat
In [3]:
def d_dyhat(f):
def d_dyhat_f(y, y_hat):
f_x, f_y = f(y, y_hat)
return f_x, np.diff(f_y)/np.diff(f_x)
return d_dyhat_f
In [4]:
def comparison_plot(y, y_hat):
return (
hv.Curve(BCE(y, y_hat), label="BCE", kdims=["y_hat"], vdims=["loss"])
* hv.Curve(wBCE(y, y_hat), label="wBCE", kdims=["y_hat"], vdims=["loss"])
* hv.VLine(y).opts(color="k", line_dash="dashed")
).opts(
show_grid=True,
title=f"Functions (y={np.round(y, 3)})",
xlim=(-0.1, 1.1),
ylim=(0, 2),
width=420
)
def derivative_comparison_plot(y, y_hat):
return (
hv.Curve(d_dyhat(BCE)(y, y_hat), label="BCE", kdims="y_hat", vdims="gradient")
* hv.Curve(d_dyhat(wBCE)(y, y_hat), label="wBCE", kdims="y_hat", vdims="gradient")
* hv.VLine(y).opts(color="k", line_dash="dashed")
* hv.HLine(0).opts(color="k", line_dash="dashed")
).opts(
show_grid=True,
title=f"Derivatives (y={np.round(y, 3)})",
xlim=(-0.1, 1.1),
ylim=(-3, 3),
width=420
)
In [5]:
y_hat = np.linspace(1e-9, 1 - 1e-09, 1000)
plots = 21
holomap = hv.HoloMap(
{
y: (comparison_plot(y, y_hat) + derivative_comparison_plot(y, y_hat)).cols(1)
for y in np.linspace(0, 1, plots)
},
kdims=hv.Dimension("y", default=0.5),
).collate()
hv.output(holomap, widget_location="top")