def advect(f, vx, vy): """Move field f according to x and y velocities (u and v) using an implicit Euler integrator.""" rows, cols = f.shape cell_xs, cell_ys = np.meshgrid(np.arange(cols), np.arange(rows)) center_xs = (cell_xs - vx).ravel() center_ys = (cell_ys - vy).ravel() # Compute indices of source cells. left_ix = np.floor(center_ys).astype(np.int) top_ix = np.floor(center_xs).astype(np.int) rw = center_ys - left_ix # Relative weight of right-hand cells. bw = center_xs - top_ix # Relative weight of bottom cells. left_ix = np.mod(left_ix, rows) # Wrap around edges of simulation. right_ix = np.mod(left_ix + 1, rows) top_ix = np.mod(top_ix, cols) bot_ix = np.mod(top_ix + 1, cols) # A linearly-weighted sum of the 4 surrounding cells. flat_f = (1 - rw) * ((1 - bw)*f[left_ix, top_ix] + bw*f[left_ix, bot_ix]) \ + rw * ((1 - bw)*f[right_ix, top_ix] + bw*f[right_ix, bot_ix]) return np.reshape(flat_f, (rows, cols))
def load_mnist(): print("Loading training data...") import imp, urllib partial_flatten = lambda x : np.reshape(x, (x.shape[0], np.prod(x.shape[1:]))) one_hot = lambda x, K: np.array(x[:,None] == np.arange(K)[None, :], dtype=int) source, _ = urllib.urlretrieve( 'https://raw.githubusercontent.com/HIPS/Kayak/master/examples/data.py') data = imp.load_source('data', source).mnist() train_images, train_labels, test_images, test_labels = data train_images = partial_flatten(train_images) / 255.0 test_images = partial_flatten(test_images) / 255.0 train_labels = one_hot(train_labels, 10) test_labels = one_hot(test_labels, 10) N_data = train_images.shape[0] return N_data, train_images, train_labels, test_images, test_labels
return r, p(r) if __name__ == "__main__": # generate data npr.seed(0) data = negbin_sample(r=5, p=0.5, size=1000) # fit likelihood-extremizing parameters r, p = fit_maxlike(data, r_guess=1) # report fit print('Fit parameters:') print('r={r}, p={p}'.format(r=r, p=p)) print('Check that we are at a local stationary point:') loglike = lambda r, p: np.sum(negbin_loglike(r, p, data)) grad_both = multigrad(loglike, argnums=[0,1]) print(grad_both(r, p)) import matplotlib.pyplot as plt xm = data.max() plt.figure() plt.hist(data, bins=np.arange(xm+1)-0.5, normed=True, label='normed data counts') plt.xlim(0,xm) plt.plot(np.arange(xm), np.exp(negbin_loglike(r, p, np.arange(xm))), label='maxlike fit') plt.xlabel('k') plt.ylabel('p(k)') plt.legend(loc='best') plt.show()
tanh_layer(120), tanh_layer(84), softmax_layer(10)] # Training parameters param_scale = 0.1 learning_rate = 1e-3 momentum = 0.9 batch_size = 256 num_epochs = 50 # Load and process MNIST data (borrowing from Kayak) print("Loading training data...") import imp, urllib add_color_channel = lambda x : x.reshape((x.shape[0], 1, x.shape[1], x.shape[2])) one_hot = lambda x, K : np.array(x[:,None] == np.arange(K)[None, :], dtype=int) source, _ = urllib.urlretrieve( 'https://raw.githubusercontent.com/HIPS/Kayak/master/examples/data.py') data = imp.load_source('data', source).mnist() train_images, train_labels, test_images, test_labels = data train_images = add_color_channel(train_images) / 255.0 test_images = add_color_channel(test_images) / 255.0 train_labels = one_hot(train_labels, 10) test_labels = one_hot(test_labels, 10) N_data = train_images.shape[0] # Make neural net functions N_weights, pred_fun, loss_fun, frac_err = make_nn_funs(input_shape, layer_specs, L2_reg) loss_grad = grad(loss_fun) # Initialize weights
def string_to_one_hot(string, maxchar): """Converts an ASCII string to a one-of-k encoding.""" ascii = np.array([ord(c) for c in string]).T return np.array(ascii[:,None] == np.arange(maxchar)[None, :], dtype=int)
gamma = primitive(scipy.special.gamma) gammaln = primitive(scipy.special.gammaln) gammasgn = primitive(scipy.special.gammasgn) rgamma = primitive(scipy.special.rgamma) multigammaln = primitive(scipy.special.multigammaln) gammasgn.defgrad_is_zero() polygamma.defgrad_is_zero(argnums=(0,)) polygamma.defgrad(lambda ans, n, x: lambda g: g * polygamma(n + 1, x), argnum=1) psi.defgrad( lambda ans, x: lambda g: g * polygamma(1, x)) digamma.defgrad( lambda ans, x: lambda g: g * polygamma(1, x)) gamma.defgrad( lambda ans, x: lambda g: g * ans * psi(x)) gammaln.defgrad( lambda ans, x: lambda g: g * psi(x)) rgamma.defgrad( lambda ans, x: lambda g: g * psi(x) / -gamma(x)) multigammaln.defgrad(lambda ans, a, d: lambda g: g * np.sum(digamma(np.expand_dims(a, -1) - np.arange(d)/2.), -1)) multigammaln.defgrad_is_zero(argnums=(1,)) ### Bessel functions ### j0 = primitive(scipy.special.j0) y0 = primitive(scipy.special.y0) j1 = primitive(scipy.special.j1) y1 = primitive(scipy.special.y1) jn = primitive(scipy.special.jn) yn = primitive(scipy.special.yn) j0.defgrad(lambda ans, x: lambda g: -g * j1(x)) y0.defgrad(lambda ans, x: lambda g: -g * y1(x)) j1.defgrad(lambda ans, x: lambda g: g * (j0(x) - jn(2, x)) / 2.0)