#!/usr/bin/env python # coding: utf-8 # 这段程序帮我们理解什么是期望更新。 # In[1]: ####################################################################### # Copyright (C) # # 2018 Shangtong Zhang(zhangshangtong.cpp@gmail.com) # # Permission given to modify the code as long as you keep this # # declaration at the top # ####################################################################### import numpy as np import matplotlib get_ipython().run_line_magic('matplotlib', 'inline') import matplotlib.pyplot as plt from tqdm import tqdm # for figure 8.7, run a simulation of 2 * @b steps def b_steps(b): # set the value of the next b states # it is not clear how to set this distribution = np.random.randn(b) # true value of the current state true_v = np.mean(distribution) samples = [] errors = [] # sample 2b steps for t in range(2 * b): v = np.random.choice(distribution) samples.append(v) errors.append(np.abs(np.mean(samples) - true_v)) return errors # In[2]: runs = 100 branch = [2, 10, 100, 1000] for b in branch: errors = np.zeros((runs, 2 * b)) for r in tqdm(np.arange(runs)): errors[r] = b_steps(b) errors = errors.mean(axis=0) x_axis = (np.arange(len(errors)) + 1) / float(b) """ 为什么要 np.zeros((runs, 2 * b)) ? 为了将不同的 b 的数画在 0, 1b, 2b 的坐标上 x_axis 将其横坐标都归一化到了 (0,2b) 的范围 """ plt.plot(x_axis, errors, label='b = %d' % (b)) plt.xlabel('number of computations') plt.xticks([0, 1.0, 2.0], ['0', 'b', '2b']) plt.ylabel('RMS error') plt.legend() plt.show() # - 对于期望更新来讲,在进行了 b 次运算可以将误差降为 0 ; # - 但是如果 b 很大,使用采样更新,无需 b 次运算,就可让误差贴近 0 。 # In[ ]: