def measure(): def noisy_mapping(mapping): def _(x): y = mapping(x) y += np.random.normal(0, 2, size=y.size) return y return _ funcs = {} funcs['sin'] = noisy_mapping(np.sin) funcs['log'] = noisy_mapping(np.log) funcs['pow1.5'] = noisy_mapping(lambda x: x**(3 / 2)) X = np.arange(0.1, 10, step=0.001) import pennpaper as pp from collections import defaultdict metrics = defaultdict(list) for i in range(90): for name, f in funcs.items(): m = pp.Metric(name) m.add_arrays(X, f(X)) metrics[m.name].append(m) metrics = [sum(v) for v in metrics.values()] pp.plot_group(metrics) pp.plot_group(metrics, smoothen=True)
def test_all_empty_no_plot(tmpdir): files_before = len(os.listdir(tmpdir)) m1 = Metric('empty') m2 = Metric('empty2') plot_group([m1, m2], folder=tmpdir) assert len(os.listdir(tmpdir)) == files_before
def test_some_empty_plots(tmpdir): files_before = len(os.listdir(tmpdir)) m1 = Metric('empty') m2 = Metric('empty2') m2.add_record(1, 1) plot_group([m1, m2], folder=tmpdir) assert len(os.listdir(tmpdir)) > files_before
def many_plots(): def one_series(shift): metrics = [Metric('x', 'y') for x in range(10)] for m in metrics: for i in range(40000): m.add_record(i, random.random() + shift + 0.4 * math.sin(i / 200)) m._sort() return sum(metrics) many = {f"random line {i}": one_series(i) for i in range(5)} plot_group(many, 'temp', name="many_series")
def test_plot_group(tmpdir): m = Metric('x', 'y') m.add_ys(1, [1, 2, 3]) m.add_record(2, 4) m.add_record(2, 4) m.add_record(2, 4) m.add_ys(4, [5, 6, 12]) m2 = Metric('x', 'y') m2.add_ys(1, [4, 3, 5]) m2.add_record(2, 5) m2.add_record(2, 9) m2.add_record(2, 9) m2.add_ys(4, [5, 6, -1]) files_before = len(os.listdir(tmpdir)) plot_group([m, m2], folder=tmpdir, name="bla3") files_after = len(os.listdir(tmpdir)) assert files_after > files_before
pow2 = noisy_mapping(lambda x: x ** 2) """ lets record the pairs (x, f(x)) in a metric and make a plot: """ from pennpaper import Metric, plot_group, plot m1 = Metric("pow2") for x in X: m1.add_record(x, pow2(x)) plot(m1) m2 = Metric("pow2_2") for x in X: m2.add_record(x, pow2(x)) plot_group([m1, m2]) # Actually, m1 and m2 are metrics of the same process. # What if we create a new metric tracking the mean and stddev of this process? m3 = m1 + m2 plot(m3) # the plot is too noisy to understand. We can smoothen it! plot(m3, smoothen=True)
obs, rew, done, _ = env.step(action) total_reward += rew history.append((obs, action, rew)) return history, total_reward def apply_grad(G, a, agent, lr): prob_1 = sigmoid(agent.param) prob = prob_1 if a == 1 else 1 - prob_1 update = lr * G * agent.grad(a) / (prob + 1e-10) agent.param += update m1 = sum(reinforce(2**-13) for _ in range(100)) m2 = sum(reinforce(2**-12) for _ in range(100)) m3 = sum(reinforce(2**-14) for _ in range(100)) m4 = sum(reinforce(2**-15) for _ in range(100)) m5 = sum(reinforce(2**-11) for _ in range(100)) # m6 = sum( reinforce(2 ** -10) for _ in range(100) ) plot_group([ m1, m2, m3, m4, m5, ], smoothen=True, stdev_factor=0.3, name="reinforce_coridor")
import numpy as np from pennpaper import Metric, plot_group xs = np.arange(0.1, 5, step=0.01) uni_noise = lambda x: np.random.uniform(size=x.shape) + x funcs = {} funcs['uniform'] = lambda x: np.random.uniform(size=x.shape) + x funcs['weibull'] = lambda x: np.random.weibull(a=1, size=x.shape) + x funcs['beta'] = lambda x: np.random.beta(a=1, b=1, size=x.shape) + x metrics = [] for name, f in funcs.items(): m = Metric(name=name) for i in range(100): m.add_arrays(uni_noise(xs), f(xs), new_sample=True) metrics.append(m) plot_group(metrics) plot_group(metrics, name='true', smoothen=False)