def plot_gmm(params, ax, num_points=100): angles = np.expand_dims(np.linspace(0, 2*np.pi, num_points), 1) xs, ys = np.cos(angles), np.sin(angles) circle_pts = np.concatenate([xs, ys], axis=1) * 2.0 for log_proportion, mean, chol in zip(*unpack_params(params)): cur_pts = mean + np.dot(circle_pts, chol) ax.plot(cur_pts[:, 0], cur_pts[:, 1], '-')
def make_pinwheel_data(num_classes, num_per_class, rate=2.0, noise_std=0.001): spoke_angles = np.linspace(0, 2*np.pi, num_classes+1)[:-1] rs = npr.RandomState(0) x = np.linspace(0.1, 1, num_per_class) xs = np.concatenate([rate *x * np.cos(angle + x * rate) + noise_std * rs.randn(num_per_class) for angle in spoke_angles]) ys = np.concatenate([rate *x * np.sin(angle + x * rate) + noise_std * rs.randn(num_per_class) for angle in spoke_angles]) return np.concatenate([np.expand_dims(xs, 1), np.expand_dims(ys,1)], axis=1)
def make_pinwheel_data(num_spokes=5, points_per_spoke=40, rate=1.0, noise_std=0.005): """Make synthetic data in the shape of a pinwheel.""" spoke_angles = np.linspace(0, 2 * np.pi, num_spokes + 1)[:-1] rs = npr.RandomState(0) x = np.linspace(0.1, 1, points_per_spoke) xs = np.concatenate([x * np.cos(angle + x * rate) + noise_std * rs.randn(len(x)) for angle in spoke_angles]) ys = np.concatenate([x * np.sin(angle + x * rate) + noise_std * rs.randn(len(x)) for angle in spoke_angles]) return np.concatenate([np.expand_dims(xs, 1), np.expand_dims(ys,1)], axis=1)
self.terminal = sys.stdout self.log = open(filename, "a") def write(self, message): self.terminal.write(message) self.log.write(message) if __name__ == '__main__': sys.stdout = Logger("experiment.txt") # Specify inference problem by its unnor # Specify inference problem by its unnormalized log-posterior. rbf = lambda x: norm.pdf(x, 0, 1) sq = lambda x: np.sin(x) num_weights, predictions, logprob = \ make_nn_funs(layer_sizes=[1, 10, 10, 1], L2_reg=0.01, noise_variance = 0.01, nonlinearity=rbf) inputs, targets = build_toy_dataset() log_posterior = lambda weights, t: logprob(weights, inputs, targets) # Build variational objective. objective, gradient, unpack_params = \ black_box_variational_inference(log_posterior, num_weights, num_samples=20) # Set up figure. fig = plt.figure(figsize=(8,8), facecolor='white') ax = fig.add_subplot(111, frameon=False)
def fun(x): return np.sin(x)
def to_scalar(x): if isinstance(x, list) or isinstance(x, ListNode) or \ isinstance(x, tuple) or isinstance(x, TupleNode): return sum([to_scalar(item) for item in x]) return np.sum(np.real(np.sin(x)))