def GetVideoTimeSeries(): TS = [] VIS_RY = [] VIS_RX = [] t0 = None lasthsum = None lastvsum = None hprior, vprior = 0, 0 for msg in parse.ParseLog(open('../rustlerlog-BMPauR')): if msg[0] == 'img': _, ts, im = msg im = GammaCorrect(im) hsum = np.sum(im, axis=0) hsum -= np.mean(hsum) vsum = np.sum(im, axis=1) vsum -= np.mean(vsum) if t0 is None: t0 = ts lasthsum = hsum lastvsum = vsum hoffset = np.argmax( -2*np.arange(-80 - hprior, 81 - hprior)**2 + np.correlate(lasthsum, hsum[80:-80], mode='valid')) - 80 voffset = np.argmax( -2*np.arange(-60 - vprior, 61 - vprior)**2 + np.correlate(lastvsum, vsum[60:-60], mode='valid')) - 60 TS.append(ts - t0) VIS_RY.append(hoffset) VIS_RX.append(voffset) hprior, vprior = hoffset, voffset lasthsum = hsum lastvsum = vsum return TS, VIS_RY, VIS_RX
def projectParam_vec(param, N, D, G, M, K, lb=1e-6): # unpack the input parameter vector tp_1 = [0, M, 2*M, 3*M, 4*M, 4*M+M*G, 4*M+M*G+G, 4*M+M*G+2*G, 4*M+M*G+2*G+G*N*K, 4*M+M*G+2*G+G*(N+D)*K, 4*M+M*G+2*G+G*(N+2*D)*K, 4*M+M*G+2*G+G*(N+2*D+1)*K, 4*M+M*G+2*G+G*(N+2*D+2)*K] tp_2 = [] for i in np.arange(len(tp_1)-1): tp_2.append(param[tp_1[i] : tp_1[i+1]]) [tau_a1, tau_a2, tau_b1, tau_b2, phi, tau_v1, tau_v2, eta, mu_w, sigma_w,\ mu_b, sigma_b] = tp_2 phi = np.reshape(phi, (M,G)) eta = np.reshape(eta, (G,N,K)) # apply projections w_tau_ab = projectLB(np.concatenate((tau_a1,tau_a2,tau_b1,tau_b2)), lb) w_phi = np.zeros((M,G)) for m in np.arange(M): w_phi[m] = projectSimplex_vec(phi[m]) w_tau_v = projectLB(np.concatenate((tau_v1,tau_v2)), lb) w_eta = np.zeros((G,N,K)) for g in np.arange(G): for n in np.arange(N): w_eta[g,n] = projectSimplex_vec(eta[g,n]) w = np.concatenate((w_tau_ab, w_phi.reshape(M*G), w_tau_v, \ w_eta.reshape(G*N*K), mu_w, projectLB(sigma_w,lb), mu_b, \ projectLB(sigma_b,lb))) return w
def init(): offset = 2.0 #if optimum[0] < np.inf: # xmin = min(results['ADAM'][0][0], optimum[0]) - offset # xmax = max(results['ADAM'][0][0], optimum[0]) + offset #else: xmin = domain[0, 0] xmax = domain[0, 1] #if optimum[1] < np.inf: # ymin = min(results['ADAM'][1][0], optimum[1]) - offset # ymax = max(results['ADAM'][1][0], optimum[1]) + offset #else: ymin = domain[1, 0] ymax = domain[1, 1] x = np.arange(xmin, xmax, 0.01) y = np.arange(ymin, ymax, 0.01) X, Y = np.meshgrid(x, y) Z = np.zeros(np.shape(Y)) for a, _ in np.ndenumerate(Y): Z[a] = func(X[a], Y[a]) level = fdict['level'] if level is None: level = np.linspace(Z.min(), Z.max(), 20) else: if level[0] == 'normal': level = np.linspace(Z.min(), Z.max(), level[1]) if level[0] == 'log': level = np.logspace(np.log(Z.min()), np.log(Z.max()), level[1]) CF = ax[0].contour(X,Y,Z, levels=level) #plt.colorbar(CF, orientation='horizontal', format='%.2f') ax[0].grid() ax[0].plot(results['ADAM'][0][0], results['ADAM'][1][0], 'h', markersize=15, color = '0.75') if optimum[0] < np.inf and optimum[1] < np.inf: ax[0].plot(optimum[0], optimum[1], '*', markersize=40, markeredgewidth = 2, alpha = 0.5, color = '0.75') ax[0].legend(loc='upper center', ncol=3, bbox_to_anchor=(0.5, 1.15)) ax[1].plot(0, results['ADAM'][2][0], 'o') ax[1].axis([0, T, -0.5, max_err + 0.5]) ax[1].set_xlabel('num. iteration') ax[1].set_ylabel('loss') line1.set_data([], []) line2.set_data([], []) line3.set_data([], []) line4.set_data([], []) line5.set_data([], []) err1.set_data([], []) err2.set_data([], []) err3.set_data([], []) err4.set_data([], []) err5.set_data([], []) return line1, line2, line3, line4, line5, \ err1, err2, err3, err4, err5,
def make_pixel_grid(self): """ makes a stack of points corresponding to each point in a pixel grid with input shape """ y_grid = np.arange(self.nelec.shape[0], dtype=np.float) + 1 x_grid = np.arange(self.nelec.shape[1], dtype=np.float) + 1 xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy') # whenever we flatten and reshape use C ordering... return np.column_stack((xx.ravel(order='C'), yy.ravel(order='C')))
def plot_results(data, r, p): 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')
def create_pixel_grid(image, loc): v_s = image.equa2pixel(loc) bound = image.R minx_b, maxx_b = max(0, int(v_s[0] - bound)), min(int(v_s[0] + bound + 1), image.nelec.shape[1]) miny_b, maxy_b = max(0, int(v_s[1] - bound)), min(int(v_s[1] + bound + 1), image.nelec.shape[0]) y_grid = np.arange(miny_b, maxy_b, dtype=np.float) x_grid = np.arange(minx_b, maxx_b, dtype=np.float) xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy') pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C'))) return xx.astype(int), yy.astype(int),pixel_grid
def convolve_with_basis(self, signal): """ Convolve each column of the event count matrix with this basis :param S: signal: an array-like data, each series is (1, T) shape :return: TxB of inputs convolved with bases """ (T,_) = signal.shape (R,B) = self.basis.shape # Initialize array for filtered stimulus F = np.empty((T,B)) # Compute convolutions fo each basis vector, one at a time for b in np.arange(B): F[:,b] = sig.fftconvolve(signal, np.reshape(self.basis[:,b],(R,1)), 'full')[:T,:] # Check for positivity if np.amin(self.basis) >= 0 and np.amin(signal) >= 0: np.clip(F, 0, np.inf, out=F) assert np.amin(F) >= 0, "convolution should be >= 0" return F
def unpackParam(param, N, D, G, M, K): """ This function unpack the vector-shaped parameter to separate variables, including those described in objective.py 1) tau_a1: len(M), first parameter of q(alpha_m) 2) tau_a2: len(M), second parameter of q(alpha_m) 3) tau_b1: len(M), first parameter of q(beta_m) 4) tau_b2: len(M), second parameter of q(beta_m) 5) phi: shape(M, G), phi[m,:] is the paramter vector of q(c_m) 6) tau_v1: len(G), first parameter of q(nu_g) 7) tau_v2: len(G), second parameter of q(nu_g) 8) mu_w: shape(G, D, K), mu_w[g,d,k] is the mean parameter of q(W^g_{dk}) 9) sigma_w: shape(G, D, K), sigma_w[g,d,k] is the std parameter of q(W^g_{dk}) 10) mu_b: shape(G, K), mu_b[g,k] is the mean parameter of q(b^g_k) 11) sigma_b: shape(G, K), sigma_b[g,k] is the std parameter of q(b^g_k) """ tp_1 = [0, M, 2*M, 3*M, 4*M, 4*M+M*G, 4*M+M*G+G, 4*M+M*G+2*G, 4*M+M*G+2*G+G*D*K, 4*M+M*G+2*G+G*(2*D)*K, 4*M+M*G+2*G+G*(2*D+1)*K, 4*M+M*G+2*G+G*(2*D+2)*K] tp_2 = [] for i in np.arange(len(tp_1)-1): tp_2.append(param[tp_1[i] : tp_1[i+1]]) [tau_a1, tau_a2, tau_b1, tau_b2, phi, tau_v1, tau_v2, mu_w, sigma_w,\ mu_b, sigma_b] = tp_2 phi = np.reshape(phi, (M,G)) mu_w = np.reshape(mu_w, (G,D,K)) sigma_w = np.reshape(sigma_w, (G,D,K)) mu_b = np.reshape(mu_b, (G,K)) sigma_b = np.reshape(sigma_b, (G,K)) return(tau_a1, tau_a2, tau_b1, tau_b2, phi, tau_v1, tau_v2, mu_w, \ sigma_w, mu_b, sigma_b)
def fit_mog(data, max_comps = 20, mog_class = MixtureOfGaussians): from sklearn import mixture N = data.shape[0] if len(data.shape) == 1: train = data[:int(.75 * N)] test = data[int(.75 * N):] else: train = data[:int(.75*N), :] test = data[int(.75*N):, :] # do train/val GMM fit num_comps = np.arange(1, max_comps+1) scores = np.zeros(len(num_comps)) for i, num_comp in enumerate(num_comps): g = mixture.GMM(n_components=num_comp, covariance_type='full') g.fit(train) logprobs, res = g.score_samples(test) scores[i] = np.mean(logprobs) print "num_comp = %d (of %d) score = %2.4f"%(num_comp, max_comps, scores[i]) print "best validation, num_comps = %d"%num_comps[scores.argmax()] # fit final model to all data g = mixture.GMM(n_components = num_comps[scores.argmax()], covariance_type='full') g.fit(data) # create my own GMM object - it's better! return mog_class(g.means_, g.covars_, g.weights_)
def time_decay(i, c): """ Time decay part for series (tau + c) :param i: tau value :param c: c value :return: abbreviated presentation """ return np.arange(1, i+1)[::-1]+c
def sample(self, n_samples=2000, observed_states=None, random_state=None): """Generate random samples from the self. Parameters ---------- n : int Number of samples to generate. observed_states : array If provided, states are not sampled. random_state: RandomState or an int seed A random number generator instance. If None is given, the object's random_state is used Returns ------- samples : array_like, length (``n_samples``) List of samples states : array_like, shape (``n_samples``) List of hidden states (accounting for tied states by giving them the same index) """ if random_state is None: random_state = self.random_state random_state = check_random_state(random_state) samples = np.zeros(n_samples) states = np.zeros(n_samples) if observed_states is None: startprob_pdf = np.exp(np.copy(self._log_startprob)) startdist = stats.rv_discrete(name='custm', values=(np.arange(startprob_pdf.shape[0]), startprob_pdf), seed=random_state) states[0] = startdist.rvs(size=1)[0] transmat_pdf = np.exp(np.copy(self._log_transmat)) transmat_cdf = np.cumsum(transmat_pdf, 1) nrand = random_state.rand(n_samples) for idx in range(1,n_samples): newstate = (transmat_cdf[states[idx-1]] > nrand[idx-1]).argmax() states[idx] = newstate else: states = observed_states mu = np.copy(self._mu_) precision = np.copy(self._precision_) for idx in range(n_samples): mean_ = self._mu_[states[idx]] var_ = np.sqrt(1/precision[states[idx]]) samples[idx] = norm.rvs(loc=mean_, scale=var_, size=1, random_state=random_state) states = self._process_sequence(states) return samples, states
def backward_pass(self, delta): delta = delta.transpose(0, 2, 3, 1) pool_size = self.pool_shape[0] * self.pool_shape[1] y_max = np.zeros((delta.size, pool_size)) y_max[np.arange(self.arg_max.size), self.arg_max.flatten()] = delta.flatten() y_max = y_max.reshape(delta.shape + (pool_size,)) dcol = y_max.reshape(y_max.shape[0] * y_max.shape[1] * y_max.shape[2], -1) return column_to_image(dcol, self.last_input.shape, self.pool_shape, self.stride, self.padding)
def projectSimplex(mat): """ project each row vector to the simplex """ nPoints, nVars = mat.shape mu = np.fliplr(np.sort(mat, axis=1)) sum_hist = np.cumsum(mu, axis=1) flag = (mu - 1./np.tile(np.arange(1,nVars+1),(nPoints,1))*(sum_hist-1) > 0) f_flag = lambda flagPoint: len(flagPoint) - 1 - \ flagPoint[::-1].argmax() lastTrue = map(f_flag, flag) sm_row = sum_hist[np.arange(nPoints), lastTrue] theta = (sm_row - 1)*1./(np.array(lastTrue)+1.) w = np.maximum(mat - np.tile(theta, (nVars,1)).T, 0.) return w
def load_mnist(): 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) train_images, train_labels, test_images, test_labels = data_mnist.mnist() 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
def expectedstats(natparam, fudge=1e-8): S, m, kappa, nu = natural_to_standard(natparam) d = m.shape[-1] E_J = nu[...,None,None] * symmetrize(np.linalg.inv(S)) + fudge * np.eye(d) E_h = np.matmul(E_J, m[...,None])[...,0] E_hTJinvh = d/kappa + np.matmul(m[...,None,:], E_h[...,None])[...,0,0] E_logdetJ = (np.sum(digamma((nu[...,None] - np.arange(d)[None,...])/2.), -1) \ + d*np.log(2.)) - np.linalg.slogdet(S)[1] return pack_dense(-1./2 * E_J, E_h, -1./2 * E_hTJinvh, 1./2 * E_logdetJ)
def sample_invwishart(S, nu): n = S.shape[0] chol = np.linalg.cholesky(S) if (nu <= 81 + n) and (nu == np.round(nu)): x = npr.randn(nu, n) else: x = np.diag(np.sqrt(np.atleast_1d(chi2.rvs(nu - np.arange(n))))) x[np.triu_indices_from(x, 1)] = npr.randn(n*(n-1)//2) R = np.linalg.qr(x, 'r') T = solve_triangular(R.T, chol.T, lower=True).T return np.dot(T, T.T)
def test_predict(params, x, y, title, idx, init_idx=None, pred_params=None): """ Test predict function :param params: model parameters, mu, theta, C, c, gamma, eta :param x: observed sharecount :param y: observed viewcount :param title: figure title, YoutubeID :param idx: subplot index :param init_idx: best initial set index :param pred_params: fitted parameters :return: """ # visualise sample data ax1 = fig.add_subplot(221+idx) ax2 = ax1.twinx() ax1.plot(np.arange(1, 121), y, 'k--', label='observed #views') ax2.plot(np.arange(1, 121), x, 'r-', label='#share') ax1.set_ylim(ymin=max(0, ax1.get_ylim()[0])) ax2.set_ylim(ymax=3*max(x)) ax1.set_xlabel('video age (day)') ax1.set_ylabel('Number of views', color='k') ax1.tick_params('y', colors='k') ax2.set_ylabel('Number of shares', color='r') ax2.tick_params('y', colors='r') mu, theta, C, c, gamma, eta = params ax2.text(0.03, 0.75, 'WWW\n$\mu$={0:.2e}, $\\theta$={1:.2e}\nC={2:.2e}, c={3:.2e}\n$\gamma$={4:.2e}, $\eta$={5:.2e}\nobj value={6:.2e}' .format(mu, theta, C, c, gamma, eta, cost_function(params, x, y)), transform=ax1.transAxes) x_www = predict(params, x) ax1.plot(np.arange(1, 121), x_www, 'b-', label='WWW popularity') ax1.set_title(title, fontdict={'fontsize': 15}) if pred_params is not None: pred_mu, pred_theta, pred_C, pred_c, pred_gamma, pred_eta = pred_params ax2.text(0.55, 0.75, 'HIP\n$\mu$={0:.2e}, $\\theta$={1:.2e}\nC={2:.2e}, c={3:.2e}\n$\gamma$={4:.2e}, $\eta$={5:.2e}\nobj value={6:.2e} @init{7}' .format(pred_mu, pred_theta, pred_C, pred_c, pred_gamma, pred_eta, cost_function(pred_params, x, y), init_idx), transform=ax1.transAxes) x_predict = predict(pred_params, x) ax1.plot(np.arange(1, 121), x_predict, 'g-', label='HIP popularity')
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_ys, cell_xs = np.meshgrid(np.arange(rows), np.arange(cols)) center_xs = (cell_xs - vx).ravel() center_ys = (cell_ys - vy).ravel() # Compute indices of source cells. left_ix = np.floor(center_xs).astype(np.int) top_ix = np.floor(center_ys).astype(np.int) rw = center_xs - left_ix # Relative weight of right-hand cells. bw = center_ys - 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 resid(self, th, overflowprotect = True): """ Pearson residual """ #d is the data #k is the max value of the support out = kernden(self.d, self.k) / self.modellikelihood(self.k, th) - 1 delta = np.zeros(self.k) if overflowprotect: for i in np.arange(self.k): if out[i] == -1: delta[i] = self.overflow return(out + delta)
def make_pinwheel_data(radial_std, tangential_std, num_classes, num_per_class, rate): rads = np.linspace(0, 2*np.pi, num_classes, endpoint=False) features = npr.randn(num_classes*num_per_class, 2) \ * np.array([radial_std, tangential_std]) features[:,0] += 1. labels = np.repeat(np.arange(num_classes), num_per_class) angles = rads[labels] + rate * np.exp(features[:,0]) rotations = np.stack([np.cos(angles), -np.sin(angles), np.sin(angles), np.cos(angles)]) rotations = np.reshape(rotations.T, (-1, 2, 2)) return 10*npr.permutation(np.einsum('ti,tij->tj', features, rotations))
def projectParam(param, N, D, G, M, K, lb=1e-6): """ project variational parameter vector onto the constraint set, including positive constraints for parameters of Beta distributions, simplex constraints for parameters of Categorical distributions Parameters ---------- param: length (2M + 2M + MG + 2G + GDK + GDK + GK + GK) variational parameters, including: 1) tau_a1: len(M), first parameter of q(alpha_m) 2) tau_a2: len(M), second parameter of q(alpha_m) 3) tau_b1: len(M), first parameter of q(beta_m) 4) tau_b2: len(M), second parameter of q(beta_m) 5) phi: shape(M, G), phi[m,:] is the paramter vector of q(c_m) 6) tau_v1: len(G), first parameter of q(nu_g) 7) tau_v2: len(G), second parameter of q(nu_g) 8) mu_w: shape(G, D, K), mu_w[g,d,k] is the mean parameter of q(W^g_{dk}) 9) sigma_w: shape(G, D, K), sigma_w[g,d,k] is the std parameter of q(W^g_{dk}) 10) mu_b: shape(G, K), mu_b[g,k] is the mean parameter of q(b^g_k) 11) sigma_b: shape(G, K), sigma_b[g,k] is the std parameter of q(b^g_k) N,D,G,M,K: number of samples (N), features(D), groups(G), experts(M), clusters(K) lb: float, lower bound of positive constraints Returns ------- w: length (2M + 2M + MG + 2G + GNK + GDK + GDK + GK + GK) """ # unpack the input parameter vector tp_1 = [0, M, 2*M, 3*M, 4*M, 4*M+M*G, 4*M+M*G+G, 4*M+M*G+2*G, 4*M+M*G+2*G+G*D*K, 4*M+M*G+2*G+G*(2*D)*K, 4*M+M*G+2*G+G*(2*D+1)*K, 4*M+M*G+2*G+G*(2*D+2)*K] tp_2 = [] for i in np.arange(len(tp_1)-1): tp_2.append(param[tp_1[i] : tp_1[i+1]]) [tau_a1, tau_a2, tau_b1, tau_b2, phi, tau_v1, tau_v2, mu_w, sigma_w,\ mu_b, sigma_b] = tp_2 phi = np.reshape(phi, (M,G)) # apply projections w_tau_ab = projectLB(np.concatenate((tau_a1,tau_a2,tau_b1,tau_b2)), lb) w_phi_vec = np.reshape(projectSimplex(phi), M*G) w_tau_v = projectLB(np.concatenate((tau_v1,tau_v2)), lb) w = np.concatenate((w_tau_ab, w_phi_vec, w_tau_v, \ mu_w, projectLB(sigma_w,lb), mu_b, projectLB(sigma_b,lb))) return w
def resample_photons(self, srcs, verbose=False): """resample photons - store source-specific images""" # first, clear out old sample images for src in srcs: src.clear_sample_images() # generate per-source sample image patch for each fits image in # this field. keep track of photons due to noise noise_sums = {} for band, img in self.img_dict.iteritems(): if verbose: print " ... resampling band %s " % band samp_imgs, noise_sum = \ cel_mcmc.sample_source_photons_single_image_cython( img, [s.params for s in srcs] ) # tell each source to keep track of it's source-specific sampled # images (and the image it was stripped out of) for src, samp_img in zip(srcs, samp_imgs): if samp_img is not None: # cache pixel grid for each sample image y_grid = np.arange(samp_img.y0, samp_img.y1, dtype=np.float) x_grid = np.arange(samp_img.x0, samp_img.x1, dtype=np.float) xx, yy = np.meshgrid(x_grid, y_grid, indexing='xy') pixel_grid = np.column_stack((xx.ravel(order='C'), yy.ravel(order='C'))) src.sample_image_list.append((samp_img, img, pixel_grid)) # keep track of noise sums noise_sums[band] = noise_sum # resample noise parameter in each fits image for band, img in self.img_dict.iteritems(): a_n = self.a_0 + noise_sums[band] b_n = self.b_0 + img.nelec.size eps_tmp = img.epsilon img.epsilon = np.random.gamma(a_n, 1./b_n)
def load_mnist(): 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
def interpolate_basis(self, basis, dt, dt_max, norm=True): # Interpolate basis at the resolution of the data L,B = basis.shape t_int = np.arange(0.0, dt_max, step=dt) t_bas = np.linspace(0.0, dt_max, L) ibasis = np.zeros((len(t_int), B)) for b in np.arange(B): ibasis[:,b] = np.interp(t_int, t_bas, basis[:,b]) # Normalize so that the interpolated basis has volume 1 if norm: # ibasis /= np.trapz(ibasis,t_int,axis=0) ibasis /= (dt * np.sum(ibasis, axis=0)) if not self.allow_instantaneous: # Typically, the impulse responses are applied to times # (t+1:t+R). That means we need to prepend a row of zeros to make # sure the basis remains causal ibasis = np.vstack((np.zeros((1,B)), ibasis)) return ibasis
def plot_trace(ps, ttl): x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z = rosen(np.vstack([X.ravel(), Y.ravel()])).reshape((100,100)) ps = np.array(ps) plt.figure(figsize=(12,4)) plt.subplot(121) plt.contour(X, Y, Z, np.arange(10)**5) plt.plot(ps[:, 0], ps[:, 1], '-o') plt.plot(1, 1, 'r*', markersize=12) # global minimum plt.subplot(122) plt.semilogy(range(len(ps)), rosen(ps.T)) plt.title(ttl)
def expectedstats_standard(nu, S, M, K, fudge=1e-8): m = M.shape[0] E_Sigmainv = nu*symmetrize(np.linalg.inv(S)) + fudge*np.eye(S.shape[0]) E_Sigmainv_A = nu*np.linalg.solve(S, M) E_AT_Sigmainv_A = m*K + nu*symmetrize(np.dot(M.T, np.linalg.solve(S, M))) \ + fudge*np.eye(K.shape[0]) E_logdetSigmainv = digamma((nu-np.arange(m))/2.).sum() \ + m*np.log(2) - np.linalg.slogdet(S)[1] assert is_posdef(E_Sigmainv) assert is_posdef(E_AT_Sigmainv_A) return make_tuple( -1./2*E_AT_Sigmainv_A, E_Sigmainv_A.T, -1./2*E_Sigmainv, 1./2*E_logdetSigmainv)
def im2col(img, block_size = (5, 5), skip = 1): """ stretches block_size size'd patches centered skip distance away in both row/column space, stacks into columns (and stacks) bands into rows Use-case is for storing images for quick matrix multiplies - blows up memory usage by quite a bit (factor of 10!) motivated by implementation discussion here: http://cs231n.github.io/convolutional-networks/ edited from snippet here: http://stackoverflow.com/questions/30109068/implement-matlabs-im2col-sliding-in-python """ # stack depth bands (colors) if len(img.shape) == 3: return np.vstack([ im2col(img[:,:,k], block_size, skip) for k in xrange(img.shape[2]) ]) # input array and block size A = img B = block_size # Parameters M,N = A.shape col_extent = N - B[1] + 1 row_extent = M - B[0] + 1 # Get Starting block indices start_idx = np.arange(B[0])[:,None]*N + np.arange(B[1]) # Get offsetted indices across the height and width of input array offset_idx = np.arange(0, row_extent, skip)[:,None]*N + np.arange(0, col_extent, skip) # Get all actual indices & index into input array for final output out = np.take(A,start_idx.ravel()[:,None] + offset_idx.ravel()) return out
def QuadFitCurvatureMap(x): curv = [] for i in range(len(x)): # do a look-ahead quadratic fit, just like the car would do pts = x[(np.arange(6) + i) % len(x)] / 100 # convert to meters basis = (pts[1] - pts[0]) / np.abs(pts[1] - pts[0]) # project onto forward direction pts = (np.conj(basis) * (pts - pts[0])) p = np.polyfit(np.real(pts), np.imag(pts), 2) curv.append(p[0] / 2) return np.float32(curv)
def plot_func(params, x, y, title, idx): """ Plot trend from R-HIP, PY-HIP and AUTO-HIP parameters :param params: model parameters, mu, theta, C, c, gamma, eta :param x: observed sharecount :param y: observed viewcount :param title: figure title, YoutubeID :param idx: subplot index :return: """ # visualise sample data ax1 = fig.add_subplot(121+idx) # ax1 = fig.add_subplot(111) ax2 = ax1.twinx() ax1.plot(np.arange(1, age+1), y, 'k--', label='observed watch time') ax2.plot(np.arange(1, age+1), x, 'r-', label='#share') ax1.plot((num_train, num_train), (ax1.get_ylim()[0], ax1.get_ylim()[1]), 'k--') ax1.set_ylim(ymin=max(0, ax1.get_ylim()[0])) ax2.set_ylim(ymax=3*max(x)) ax1.set_xlabel('video age (day)') ax1.set_ylabel('Time of watches', color='k') ax1.tick_params('y', colors='k') ax2.set_ylabel('Number of shares', color='r') ax2.tick_params('y', colors='r') mu, theta, C, c, gamma, eta = params ax2.text(0.03, 0.85, '$\mu$={0:.2f}, $\\theta$={1:.2f}\nC={2:.2f}, c={3:.2f}\n$\gamma$={4:.2f}, $\eta$={5:.2f}' .format(mu, theta, C, c, gamma, eta), transform=ax1.transAxes) ax1.set_title(title) predidt_x = predict(params, x) ax1.plot(np.arange(1, num_train+1), predidt_x[:num_train], 'b-', label='HIP fit') ax1.plot(np.arange(num_train+1, age+1), predidt_x[num_train:age], 'm-', label='HIP forecast') if vid in mlr_predict_dict: ax1.plot(np.arange(90+1, age+1), mlr_predict_dict[vid], 'g-', label='MLR forecast')
def make_pinwheel(radial_std, tangential_std, num_classes, num_per_class, rate, rs=npr.RandomState(0)): """Based on code by Ryan P. Adams.""" rads = np.linspace(0, 2*np.pi, num_classes, endpoint=False) features = rs.randn(num_classes*num_per_class, 2) \ * np.array([radial_std, tangential_std]) features[:, 0] += 1 labels = np.repeat(np.arange(num_classes), num_per_class) angles = rads[labels] + rate * np.exp(features[:,0]) rotations = np.stack([np.cos(angles), -np.sin(angles), np.sin(angles), np.cos(angles)]) rotations = np.reshape(rotations.T, (-1, 2, 2)) return np.einsum('ti,tij->tj', features, rotations)
def perm_to_P(perm): K = len(perm) P = np.zeros((K, K)) P[np.arange(K), perm] = 1 return P
plot_trial(tr) plot_trial_particles(tr) sim_ys_1 = simulate_accumulator(test_acc, us, num_repeats=3) sim_ys_2 = simulate_accumulator(test_acc_pem, us, num_repeats=3) true_psths = plot_psths(ys, us, 1, N); sim_psths_1 = plot_psths(sim_ys_1, us+us+us, 1, N); sim_psths_2 = plot_psths(sim_ys_2, us+us+us, 1, N); r2_lem = compute_r2(true_psths, sim_psths_1) r2_mf = compute_r2(true_psths, sim_psths_2) psth_list=[true_psths, sim_psths_1, sim_psths_2] plot_neurons2 = npr.permutation(np.arange(N))[:3] plot_multiple_psths(psth_list, plot_neurons2) plt.gcf().axes[3].set_ylim(plt.gcf().axes[0].get_ylim()) plt.gcf().axes[6].set_ylim(plt.gcf().axes[0].get_ylim()) plt.gcf().axes[4].set_ylim(plt.gcf().axes[1].get_ylim()) plt.gcf().axes[7].set_ylim(plt.gcf().axes[1].get_ylim()) plt.gcf().axes[5].set_ylim(plt.gcf().axes[2].get_ylim()) plt.gcf().axes[8].set_ylim(plt.gcf().axes[2].get_ylim()) # compare z z_dir = [] z_t = [] z_dir_bbvi = [] z_t_bbvi = [] true_z_dir = []
def sun_blockdiag(x, N, o = 0): ret = lie_eye(N) for i in np.arange(x.size): ret[(2*i):2*(i+1),(2*i):2*(i+1)] = get_su2(x[i]) return np.roll(ret, (o,o), axis = (0,1))
def make_model_funs(crime=1., precinct_type=0): """ crime: 1=violent, 2=weapons, 3=property, 4=drug eth : 1=black, 2 = hispanic, 3=white precincts: 1-75 precinct_type = (0, .1], (.1, .4], (.4, 1.] """ # subselect crime/precinct, set up design matrix sdf = df[(df['crime'] == crime) & (df['precinct_type'] == precinct_type)] # make dummies for precincts, etc one_hot = lambda x, k: np.array(x[:, None] == np.arange(k)[None, :], dtype=int) precincts = np.sort(np.unique(sdf['precinct'])) Xprecinct = one_hot(sdf['precinct'], 76)[:, precincts] Xeth = one_hot(sdf['eth'], 4)[:, 1:-1] yep = sdf['stops'].values lnep = np.log(sdf['past.arrests'].values) + np.log(15. / 12) num_eth = Xeth.shape[1] num_precinct = Xprecinct.shape[1] # unpack a flat param vector aslice = slice(0, num_eth) bslice = slice(num_eth, num_eth + num_precinct) mslice = slice(bslice.stop, bslice.stop + 1) lnsa_slice = slice(mslice.stop, mslice.stop + 1) lnsb_slice = slice(lnsa_slice.stop, lnsa_slice.stop + 1) num_params = lnsb_slice.stop pname = lambda s, stub: [ '%s_%d' % (stub, i) for i in xrange(s.stop - s.start) ] param_names = [ pname(s, stub) for s, stub in zip([aslice, bslice, mslice, lnsa_slice, lnsb_slice], ['alpha', 'beta', 'mu', 'lnsigma_a', 'lnsigma_b']) ] param_names = [s for pn in param_names for s in pn] def unpack(th): """ unpack vectorized lndf """ th = np.atleast_2d(th) alpha_eth, beta_prec, mu, lnsigma_alpha, lnsigma_beta = \ th[:, aslice], th[:, bslice], th[:, mslice], \ th[:, lnsa_slice], th[:, lnsb_slice] return alpha_eth, beta_prec, mu, lnsigma_alpha, lnsigma_beta hyper_lnstd = np.array([[np.log(10.)]]) def lnpdf(th): # params alpha, beta, mu, lns_alpha, lns_beta = unpack(th) # priors ll_alpha = normal_lnpdf(alpha, 0, lns_alpha) ll_beta = normal_lnpdf(beta, 0, lns_beta) ll_mu = normal_lnpdf(mu, 0, hyper_lnstd) ll_salpha = normal_lnpdf(np.exp(lns_alpha), 0, hyper_lnstd) ll_sbeta = normal_lnpdf(np.exp(lns_beta), 0, hyper_lnstd) logprior = ll_alpha + ll_beta + ll_mu + ll_salpha + ll_sbeta # likelihood lnlam = (mu + lnep[None,:]) + \ np.dot(alpha, Xeth.T) + np.dot(beta, Xprecinct.T) loglike = np.sum(lnpoiss(yep, lnlam), 1) return (loglike + logprior).squeeze() return lnpdf, unpack, num_params, sdf, param_names
def scatter_pts(self, ax): if np.shape(self.x)[1] == 1: # set plotting limits xmax = copy.deepcopy(max(self.x)) xmin = copy.deepcopy(min(self.x)) xgap = (xmax - xmin) * 0.4 xmin -= xgap xmax += xgap ymax = max(self.y) ymin = min(self.y) ygap = (ymax - ymin) * 0.4 ymin -= ygap ymax += ygap # initialize points ax.scatter(self.x, self.y, color='k', edgecolor='w', linewidth=0.9, s=40) # clean up panel ax.set_xlim([xmin, xmax]) ax.set_ylim([ymin, ymax]) # label axes ax.set_xlabel(r'$x$', fontsize=12) ax.set_ylabel(r'$y$', rotation=0, fontsize=12) ax.axhline(y=0, color='k', zorder=0, linewidth=0.5) ax.axvline(x=0, color='k', zorder=0, linewidth=0.5) if np.shape(self.x)[1] == 2: # set plotting limits xmax1 = copy.deepcopy(max(self.x[:, 0])) xmin1 = copy.deepcopy(min(self.x[:, 0])) xgap1 = (xmax1 - xmin1) * 0.35 xmin1 -= xgap1 xmax1 += xgap1 xmax2 = copy.deepcopy(max(self.x[:, 0])) xmin2 = copy.deepcopy(min(self.x[:, 0])) xgap2 = (xmax2 - xmin2) * 0.35 xmin2 -= xgap2 xmax2 += xgap2 ymax = max(self.y) ymin = min(self.y) ygap = (ymax - ymin) * 0.2 ymin -= ygap ymax += ygap # initialize points ax.scatter(self.x[:, 0], self.x[:, 1], self.y, s=40, color='k', edgecolor='w', linewidth=0.9) # clean up panel ax.set_xlim([xmin1, xmax1]) ax.set_ylim([xmin2, xmax2]) ax.set_zlim([ymin, ymax]) ax.set_xticks(np.arange(round(xmin1) + 1, round(xmax1), 1.0)) ax.set_yticks(np.arange(round(xmin2) + 1, round(xmax2), 1.0)) # label axes ax.set_xlabel(r'$x_1$', fontsize=12, labelpad=5) ax.set_ylabel(r'$x_2$', rotation=0, fontsize=12, labelpad=5) ax.set_zlabel(r'$y$', rotation=0, fontsize=12, labelpad=-3) # clean up panel ax.xaxis.pane.fill = False ax.yaxis.pane.fill = False ax.zaxis.pane.fill = False ax.xaxis.pane.set_edgecolor('white') ax.yaxis.pane.set_edgecolor('white') ax.zaxis.pane.set_edgecolor('white') ax.xaxis._axinfo["grid"]['color'] = (1, 1, 1, 0) ax.yaxis._axinfo["grid"]['color'] = (1, 1, 1, 0) ax.zaxis._axinfo["grid"]['color'] = (1, 1, 1, 0)
def scatter_pts(self,ax): if np.shape(self.x)[1] == 1: # set plotting limits xmax = copy.deepcopy(max(self.x)) xmin = copy.deepcopy(min(self.x)) xgap = (xmax - xmin)*0.2 xmin -= xgap xmax += xgap ymax = max(self.y) ymin = min(self.y) ygap = (ymax - ymin)*0.2 ymin -= ygap ymax += ygap # initialize points ax.scatter(self.x,self.y,color = 'k', edgecolor = 'w',linewidth = 0.9,s = 40) # clean up panel ax.set_xlim([xmin,xmax]) ax.set_ylim([ymin,ymax]) # label axes ax.set_xlabel(r'$x$', fontsize = 12) ax.set_ylabel(r'$y$', rotation = 0,fontsize = 12) ax.set_title('data', fontsize = 13) ax.axhline(y=0, color='k',zorder = 0,linewidth = 0.5) ax.axvline(x=0, color='k',zorder = 0,linewidth = 0.5) if np.shape(self.x)[1] == 2: # set plotting limits xmax1 = copy.deepcopy(max(self.x[:,0])) xmin1 = copy.deepcopy(min(self.x[:,0])) xgap1 = (xmax1 - xmin1)*0.35 xmin1 -= xgap1 xmax1 += xgap1 xmax2 = copy.deepcopy(max(self.x[:,0])) xmin2 = copy.deepcopy(min(self.x[:,0])) xgap2 = (xmax2 - xmin2)*0.35 xmin2 -= xgap2 xmax2 += xgap2 ymax = max(self.y) ymin = min(self.y) ygap = (ymax - ymin)*0.2 ymin -= ygap ymax += ygap # scatter points in both panels class_nums = np.unique(self.y) C = len(class_nums) for c in range(C): ind = np.argwhere(self.y == class_nums[c]) ind = [v[0] for v in ind] ax.scatter(self.x[ind,0],self.x[ind,1],self.y[ind],s = 80,color = self.colors[c],edgecolor = 'k',linewidth = 1.5) # clean up panel ax.set_xlim([xmin1,xmax1]) ax.set_ylim([xmin2,xmax2]) ax.set_zlim([ymin,ymax]) ax.set_xticks(np.arange(round(xmin1) +1, round(xmax1), 1.0)) ax.set_yticks(np.arange(round(xmin2) +1, round(xmax2), 1.0)) ax.set_zticks([-1,0,1]) # label axes ax.set_xlabel(r'$x_1$', fontsize = 12,labelpad = 5) ax.set_ylabel(r'$x_2$', rotation = 0,fontsize = 12,labelpad = 5) ax.set_zlabel(r'$y$', rotation = 0,fontsize = 12,labelpad = -3) # clean up panel ax.xaxis.pane.fill = False ax.yaxis.pane.fill = False ax.zaxis.pane.fill = False ax.xaxis.pane.set_edgecolor('white') ax.yaxis.pane.set_edgecolor('white') ax.zaxis.pane.set_edgecolor('white') ax.xaxis._axinfo["grid"]['color'] = (1,1,1,0) ax.yaxis._axinfo["grid"]['color'] = (1,1,1,0) ax.zaxis._axinfo["grid"]['color'] = (1,1,1,0)
def animate(t): ax.cla() k = math.floor((t + 1) / float(2)) # print rendering update if np.mod(k + 1, 25) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(num_frames)) if t == num_frames - 1: print('animation rendering complete!') time.sleep(1.5) clear_output() # plot function ax.plot(w_plot, g_plot, color='k', zorder=1) # plot function # plot initial point and evaluation if k == 0: w_val = w_hist[0] g_val = g(w_val) ax.scatter(w_val, g_val, s=100, c=colorspec[k], edgecolor='k', linewidth=0.7, marker='X', zorder=2) # plot point of tangency ax.scatter(w_val, 0, s=100, c=colorspec[k], edgecolor='k', linewidth=0.7, zorder=2) # draw dashed line connecting w axis to point on cost function s = np.linspace(0, g_val) o = np.ones((len(s))) ax.plot(o * w_val, s, 'k--', linewidth=1, zorder=0) # plot all input/output pairs generated by algorithm thus far if k > 0: # plot all points up to this point for j in range(min(k - 1, len(w_hist))): w_val = w_hist[j] g_val = g(w_val) ax.scatter(w_val, g_val, s=90, c=colorspec[j], edgecolor='k', marker='X', linewidth=0.7, zorder=3) # plot point of tangency ax.scatter(w_val, 0, s=90, facecolor=colorspec[j], edgecolor='k', linewidth=0.7, zorder=2) # plot surrogate function and travel-to point if k > 0 and k < len(w_hist) + 1: # grab historical weight, compute function and derivative evaluations w_eval = w_hist[k - 1] if type(w_eval) != float: w_eval = float(w_eval) # plug in value into func and derivative g_eval = g(w_eval) g_grad_eval = grad(w_eval) g_hess_eval = hess(w_eval) # determine width of plotting area for second order approximator width = 0.5 if g_hess_eval < 0: width = -width # setup quadratic formula params a = 0.5 * g_hess_eval b = g_grad_eval - 2 * 0.5 * g_hess_eval * w_eval c = 0.5 * g_hess_eval * w_eval**2 - g_grad_eval * w_eval - width # solve for zero points w1 = (-b + math.sqrt(b**2 - 4 * a * c)) / float(2 * a + 0.00001) w2 = (-b - math.sqrt(b**2 - 4 * a * c)) / float(2 * a + 0.00001) # compute second order approximation wrange = np.linspace(w1, w2, 100) h = g_eval + g_grad_eval * ( wrange - w_eval) + 0.5 * g_hess_eval * (wrange - w_eval)**2 # plot tangent curve ax.plot(wrange, h, color=colorspec[k - 1], linewidth=2, zorder=2) # plot approx # plot tangent point ax.scatter(w_eval, g_eval, s=100, c='m', edgecolor='k', marker='X', linewidth=0.7, zorder=3) # plot point of tangency # plot next point learned from surrogate if np.mod(t, 2) == 0: # create next point information w_zero = w_eval - g_grad_eval / (g_hess_eval + 10**-5) g_zero = g(w_zero) h_zero = g_eval + g_grad_eval * ( w_zero - w_eval) + 0.5 * g_hess_eval * (w_zero - w_eval)**2 # draw dashed line connecting the three vals = [0, h_zero, g_zero] vals = np.sort(vals) s = np.linspace(vals[0], vals[2]) o = np.ones((len(s))) ax.plot(o * w_zero, s, 'k--', linewidth=1) # draw intersection at zero and associated point on cost function you hop back too ax.scatter(w_zero, h_zero, s=100, c='b', linewidth=0.7, marker='X', edgecolor='k', zorder=3) ax.scatter(w_zero, 0, s=100, c='m', edgecolor='k', linewidth=0.7, zorder=3) ax.scatter(w_zero, g_zero, s=100, c='m', edgecolor='k', linewidth=0.7, marker='X', zorder=3) # plot point of tangency # fix viewing limits on panel ax.set_xlim([wmin, wmax]) ax.set_ylim( [min(-0.3, min(g_plot) - ggap), max(max(g_plot) + ggap, 0.3)]) # add horizontal axis ax.axhline(y=0, color='k', zorder=0, linewidth=0.5) # label axes ax.set_xlabel(r'$w$', fontsize=14) ax.set_ylabel(r'$g(w)$', fontsize=14, rotation=0, labelpad=25) # set tickmarks ax.set_xticks(np.arange(round(wmin), round(wmax) + 1, 1.0)) ax.set_yticks( np.arange(round(min(g_plot) - ggap), round(max(g_plot) + ggap) + 1, 1.0)) return artist,
from matplotlib.colors import LogNorm from matplotlib import animation from IPython.display import HTML from autograd import elementwise_grad, value_and_grad from scipy.optimize import minimize from collections import defaultdict from itertools import zip_longest from functools import partial f = lambda x, y: (1.5 - x + x*y)**2 + (2.25 - x + x*y**2)**2 + (2.625 - x + x*y**3)**2 xmin, xmax, xstep = -4.5, 4.5, .2 ymin, ymax, ystep = -4.5, 4.5, .2 x, y = np.meshgrid(np.arange(xmin, xmax + xstep, xstep), np.arange(ymin, ymax + ystep, ystep)) z = f(x, y) minima = np.array([3., .5]) # minima 인 곳을 별로 표시함 f(*minima) # minima value : 0 minima_ = minima.reshape(-1, 1) fig = plt.figure(figsize=(8, 5)) ax = plt.axes(projection='3d', elev=50, azim=-50) ax.plot(*minima_, f(*minima_), 'r*', markersize=10) ax.plot_surface(x, y, z, norm=LogNorm(), rstride=1, cstride=1, edgecolor='c', alpha=0.5, cmap=plt.cm.jet, linewidth=0.5)
tanh_layer(84), softmax_layer(10) ] # Training parameters param_scale = 0.1 learning_rate = 1e-3 momentum = 0.9 batch_size = 150 num_epochs = 50 init_rate = learning_rate 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) ############## train_batch, train_labels, test_batch, test_labels, _, coords = load_mesquare.load( ) #adj_mtx, _ = load_mesquare.create_mtx(28) adj_mtx = load_mesquare.load_csv(24) order = mesh_traversal.traverse_mtx(adj_mtx, coords, center, stride) # list of vertices, ordered rem = set(range(28 * 28)).difference(set(order)) order = order + list(rem) _, coords_2 = load_mesquare.create_mtx(8)
def predict_to_labels(prediction): pred_labels = np.argmax(prediction, axis=1) pred_labels_onehot = np.zeros((pred_labels.size, 3)) pred_labels_onehot[np.arange(pred_labels.size), pred_labels] = 1 return pred_labels_onehot
def transform_labels(labels): labels += 1 labels_onehot = np.zeros((labels.size, 3)) labels_onehot[np.arange(labels.size), labels] = 1 return labels_onehot
plt.imshow(hsmm_z[None, :1000], aspect="auto", cmap="cubehelix", vmin=0, vmax=K - 1) plt.xlim(0, 1000) plt.ylabel("$z_{\\mathrm{inferred}}$") plt.yticks([]) plt.xlabel("time") plt.tight_layout() # Plot the true and inferred duration distributions states, durations = rle(z) inf_states, inf_durations = rle(hsmm_z) max_duration = max(np.max(durations), np.max(inf_durations)) dd = np.arange(max_duration, step=1) plt.figure(figsize=(3 * K, 6)) for k in range(K): # Plot the durations of the true states plt.subplot(2, K, k + 1) plt.hist(durations[states == k] - 1, dd, density=True) plt.plot(dd, nbinom.pmf(dd, true_hsmm.transitions.rs[k], 1 - true_hsmm.transitions.ps[k]), '-k', lw=2, label='true') if k == K - 1: plt.legend(loc="lower right") plt.title("State {} (N={})".format(k + 1, np.sum(states == k)))
def animate_trainval_earlystop(self, savepath, run, frames, **kwargs): train_errors = run.train_count_histories[0] valid_errors = run.valid_count_histories[0] weight_history = run.weight_histories[0] num_units = len(weight_history) # select subset of runs inds = np.arange(0, len(weight_history), int(len(weight_history) / float(frames))) weight_history = [weight_history[v] for v in inds] train_errors = [train_errors[v] for v in inds] valid_errors = [valid_errors[v] for v in inds] # construct figure fig = plt.figure(figsize=(6, 6)) artist = fig # create subplot with 4 panels, plot input function in center plot gs = gridspec.GridSpec(2, 2) ax = plt.subplot(gs[0]) ax1 = plt.subplot(gs[2]) ax2 = plt.subplot(gs[3]) ax3 = plt.subplot(gs[1]) # start animation num_frames = len(inds) print('starting animation rendering...') def animate(k): # clear panels ax.cla() ax1.cla() ax2.cla() ax3.cla() # print rendering update if np.mod(k + 1, 25) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(num_frames)) if k == num_frames - 1: print('animation rendering complete!') time.sleep(1.5) clear_output() #### plot training, testing, and full data #### # pluck out current weights w_best = weight_history[k] # produce static img self.static_N2_simple(ax, w_best, run, train_valid='original') self.draw_boundary(ax, run, w_best, train_valid='train') self.static_N2_simple(ax1, w_best, run, train_valid='train') self.draw_boundary(ax1, run, w_best, train_valid='train') self.static_N2_simple(ax2, w_best, run, train_valid='validate') self.draw_boundary(ax2, run, w_best, train_valid='validate') #### plot training / validation errors #### self.plot_train_valid_errors(ax3, k, train_errors, valid_errors, num_units) return artist, anim = animation.FuncAnimation(fig, animate, frames=num_frames, interval=num_frames, blit=True) # produce animation and save fps = 50 if 'fps' in kwargs: fps = kwargs['fps'] anim.save(savepath, fps=fps, extra_args=['-vcodec', 'libx264']) clear_output()
def scan_policy(pos_1, pos_2=None, n=20, points=30, model=None, top=False, bot=False, nlp=None): x0 = np.zeros(4) x0 = random_init(x0) x0[0] = 0 if top == True: x0 = np.array([ random.normalvariate(0, 0.03), random.normalvariate(0, 0.03), random.normalvariate(0, 0.03), random.normalvariate(0, 0.03) ]) if bot: x0 = np.array([ random.normalvariate(0, 0.03), random.normalvariate(0, 0.03), np.pi - random.normalvariate(0, 0.03), random.normalvariate(0, 0.03) ]) p = np.zeros(4) # p[1]=5 # p[3]=-8 loop = False osc = False if loop == True: x0 = np.array([0, 0, np.pi, 15]) elif osc == True: x0 = np.array([0, 0, np.pi, 5]) scanned_x = np.arange(-p_range[pos_1] / 2, p_range[pos_1] / 2, p_range[pos_1] / points) ##scanning variable 1yield if pos_2 != None: ## 2d contour plots scanned_y = np.arange(-p_range[pos_2] / 2, p_range[pos_2] / 2, p_range[pos_2] / points) ##scanning variable 1yield X, Y = np.meshgrid(scanned_x, scanned_y) rav_X = np.ravel(X) rav_Y = np.ravel(Y) rav_Z = np.zeros(rav_X.shape[0]) for i in range(rav_X.shape[0]): p[pos_1] = rav_X[i] p[pos_2] = rav_Y[i] rav_Z[i] = loss_trajectory(x0, n, p, model, nlp) Z = rav_Z.reshape(X.shape) colour = 'inferno' fig = plt.figure() ax = fig.add_subplot(111, projection='3d') surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=colour, vmin=0, vmax=50) # Add a color bar which maps values to colors. cbar = fig.colorbar(surf, shrink=0.5, aspect=5) cbar.set_label('Loss') plt.xlabel('p[' + str(pos_1) + ']') plt.ylabel('p[' + str(pos_2) + ']') ax.set_zlabel('Loss') plt.title('Initial state: ' + str(np.round(x0, 2)) + '\n Scan through ' + 'p[' + str(pos_1) + '] and p[' + str(pos_2) + ']') plt.show() cont_plot = plt.tricontourf(rav_X, rav_Y, rav_Z, levels=14, cmap=colour, vmin=0, vmax=50) cbar = plt.colorbar(cont_plot, shrink=0.5, aspect=5) cbar.set_label('Loss') plt.xlabel('p[' + str(pos_1) + ']') plt.ylabel('p[' + str(pos_2) + ']') plt.title('Initial state: ' + str(np.round(x0, 2)) + '\n Scan through ' + 'p[' + str(pos_1) + '] and p[' + str(pos_2) + ']') plt.show()
def make_forecast(original_data): # fit ARIMA(1, 1, 0) model arima_model = ARIMA(np.log(original_data), (1, 1, 0)) fitted_model = arima_model.fit() # print summary print(fitted_model.summary()) # make forecast forecast_val, se, ci = fitted_model.forecast(90) forecast_series = pd.Series(np.exp(forecast_val), index = original_data.index[-1] + pd.to_timedelta(np.arange(90), 'D')) lower_bound = pd.Series(np.exp(ci[:, 0]), index = original_data.index[-1] + pd.to_timedelta(np.arange(90), 'D')) upper_bound = pd.Series(np.exp(ci[:, 1]), index = original_data.index[-1] + pd.to_timedelta(np.arange(90), 'D')) # combine results res_list = [forecast_series, lower_bound, upper_bound] return res_list
def state_map(self): return np.repeat(np.arange(self.K), self.rs)
lfp = np.array(lfp) # (n_elec, time, trials) lfp /= 100.0 # Rescale for numerical reasons lfp -= np.mean(lfp, 2, keepdims=True) # %% Subset to baseline period time = np.loadtxt( '%s/data/time.txt' % root_path) * 1000. # Time in seconds, convert to ms time_idx = time < 0 t = time[time_idx][:, None] lfp_baseline = lfp[:, time_idx, :] # %% Set up GPCSD and fit # Note: could speed up with lower n_restarts, or don't use all trials nx, nt, ntrials = lfp_baseline.shape if ntrials_fit is None: trials_sel = np.arange(ntrials) else: trials_sel = np.random.choice(ntrials, ntrials_fit, replace=False) spatial_cov = GPCSD1DSpatialCovSE(x, a=-200.0, b=2600.0) matern_cov = GPCSDTemporalCovMatern(t) matern_cov.params['ell']['prior'].set_params(1., 20.) SE_cov = GPCSDTemporalCovSE(t) SE_cov.params['ell']['prior'].set_params(30., 100.) sig2n_prior = [GPCSDHalfNormalPrior(0.1) for i in range(nx)] gpcsd_model = GPCSD1D(lfp_baseline[:, :, trials_sel], x, t, sig2n_prior=sig2n_prior, spatial_cov=spatial_cov, temporal_cov_list=[SE_cov, matern_cov],
def __init__(self, param_set, buffer_size=200000, buffer_type='Qnetwork', mem_priority=True, general=False): """Initialize the storage containers and parameters relevant for experience replay. Arguments: param_set -- dictionary of parameters which must contain: PER_alpha -- hyperparameter governing how much prioritization is used PER_beta_zero -- importance sampling parameter initial value bnn_start -- number of timesteps before sample will be drawn; i.e the minimum partition size (necessary if buffer_type=='BNN') dqn_start -- same as dqn_start (necessary if buffer_type=='Qnetwork') episode_count -- number of episodes instance_count -- number of instances max_task_examples -- maximum number of timesteps per episode ddqn_batch_size -- minibatch size for DQN updates (necessary if buffer_type=='Qnetwork') bnn_batch_size -- minibatch size for BNN updates (necessary if buffer_type=='BNN') num_strata_samples -- number of samples to be drawn from each strata in the prioritized replay buffer general_num_partitions -- number of partitions for general experience buffer instance_num_partitions -- number of partitions for instance experience buffer Keyword arguments: buffer_size -- maximum capacity of the experience buffer (default: 200000) buffer_type -- string indicating whether experience replay is for training a DQN or a BNN (either 'Qnetwork' or 'BNN'; default: 'Qnetwork') mem_priority -- boolean indicating whether the experience replay should be prioritized (default: True) general -- boolean indicating if the experience replay is for collecting experiences over multiple instances or a single (default: False) """ # Extract/Set relevant parameters self.mem_priority = mem_priority self.alpha = param_set['PER_alpha'] self.beta_zero = param_set['PER_beta_zero'] self.capacity = buffer_size self.is_full = False self.index = 0 # Index number in priority queue where next transition should be inserted self.size = 0 # Current size of experience replay buffer if buffer_type == 'Qnetwork': self.num_init_train = param_set['dqn_start'] self.tot_steps = param_set['episode_count'] * param_set[ 'max_task_examples'] self.batch_size = param_set['ddqn_batch_size'] elif buffer_type == 'BNN': self.num_init_train = param_set['bnn_start'] self.tot_steps = ( param_set['episode_count'] * param_set['instance_count']) * param_set['max_task_examples'] self.batch_size = param_set['bnn_batch_size'] self.beta_grad = (1 - self.beta_zero) / (self.tot_steps - self.num_init_train) self.num_strata_samples = param_set['num_strata_samples'] # Note: at least one partition must be completely filled in order for the sampling procedure to work self.num_partitions = self.capacity / (1.0 * self.num_init_train) # Initialize experience buffer self.exp_buffer = [] # Initialize rank priority distributions and stratified sampling cutoffs if needed if self.mem_priority: # Initialize Priority Queue (will be implemented as a binary heap) self.pq = PriorityQueue(capacity=buffer_size) self.distributions = {} partition_num = 1 partition_division = self.capacity / self.num_partitions for n in np.arange(partition_division, self.capacity + 0.1, partition_division): # Set up power-law PDF and CDF distribution = {} distribution['pdf'] = np.power(np.linspace(1, n, n), -1 * self.alpha) pdf_sum = np.sum(distribution['pdf']) distribution['pdf'] = distribution['pdf'] / float( pdf_sum) # Normalise PDF cdf = np.cumsum(distribution['pdf']) # Set up strata for stratified sampling (transitions will have varying TD-error magnitudes) distribution['strata_ends'] = np.zeros(self.batch_size + 1) distribution['strata_ends'][0] = 0 # First index is 0 (+1) distribution['strata_ends'][ self.batch_size] = n # Last index is n # Use linear search to find strata indices stratum = 1.0 / self.batch_size index = 0 for s in range(1, self.batch_size): if cdf[index] >= stratum: index += 1 while cdf[index] < stratum: index = index + 1 distribution['strata_ends'][s] = index stratum = stratum + 1.0 / self.batch_size # Set condition for next stratum # Store distribution self.distributions[partition_num] = distribution partition_num = partition_num + 1
def get_scale(n, scale_factor): return anp.power(anp.full(n, scale_factor), anp.arange(n))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ true_state_array = np.transpose(np.load('Burgulence_Coefficients.npy'))[:-1, :] gp_state_array = np.transpose( np.load('Burgulence_GP_Coefficients.npy'))[:-1, :] lstm_state_array = np.transpose( np.load('Burgulence_LSTM_Coefficients.npy'))[:-1, :] tsteps = np.shape(true_state_array)[0] state_len = np.shape(true_state_array)[1] batch_tsteps = 10 num_batches = 10 dt = 2.0 / tsteps # Time array - fixed time_array = dt * np.arange(tsteps) # DS definition init_state = true_state_array[0, :] # Visualization fluff here fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(8, 12)) mnum = 0 for i in range(3): tt = ax[i].plot(time_array[:], true_state_array[:, mnum], label='True') tt = ax[i].plot(time_array[:], gp_state_array[:, mnum], label='GP', color='green') tt = ax[i].plot(time_array[:],
mean = 1.4 x = np.random.normal(size=(Xsize, 1)) * std + mean bsize = 20 n_samples = 8 * 10**4 samples = [] for i in range(n_samples): start = (i * bsize) % (x.shape[0] - bsize) xmb = np.copy(x[start:start + bsize]) sample = sampler.step(xmb) samples.append(np.random.randn() * np.sqrt(np.exp(sample[1]) + 1e-16) + sample[0]) true_step = 0.001 xs = np.arange(-6, 6, true_step) nlls = np.zeros_like(xs) for i, x in enumerate(xs): nlls[i] = neg_log_like(np.array([mean, np.log(std**2)]), x) # compute likelihood lls = np.exp(-nlls) # approximately compute z via euler integration z = np.sum(lls) * true_step # approximate density from the samples step_sample = 0.1 xgrid = np.arange(-6, 6, step_sample) ygrid = np.asarray(np.histogram(samples, xgrid, density=True)[0]) plt.plot(xs, lls / z, color='red') plt.plot(xgrid[:len(ygrid)], ygrid)
def main(argv): del argv n_clusters = FLAGS.num_clusters n_dimensions = FLAGS.num_dimensions n_observations = FLAGS.num_observations alpha = 3.3 * np.ones(n_clusters) a = 1. b = 1. kappa = 0.1 npr.seed(10001) # generate true latents and data pi = npr.gamma(alpha) pi /= pi.sum() mu = npr.normal(0, 1.5, [n_clusters, n_dimensions]) z = npr.choice(np.arange(n_clusters), size=n_observations, p=pi) x = npr.normal(mu[z, :], 0.5**2) # points used for initialization pi_est = np.ones(n_clusters) / n_clusters z_est = npr.choice(np.arange(n_clusters), size=n_observations, p=pi_est) mu_est = npr.normal(0., 0.01, [n_clusters, n_dimensions]) tau_est = 1. init_vals = pi_est, z_est, mu_est, tau_est # instantiate the model log joint log_joint = make_log_joint(x, alpha, a, b, kappa) # run mean field on variational mean parameters def callback(meanparams): fig = plot(meanparams, x) plt.savefig('/tmp/gmm_{:04d}.png'.format(itr)) plt.close(fig.number) supports = (SIMPLEX, INTEGER, REAL, NONNEGATIVE) neg_energy, normalizers, _, initializers, _, _ = \ multilinear_representation(log_joint, init_vals, supports) np_natparams = [initializer(10.) for initializer in initializers] np_meanparams = [ grad(normalizer)(natparam) for normalizer, natparam in zip(normalizers, np_natparams) ] # TODO(trandustin) try using feed_dict's to debug def tf_get_variable(inputs): return tf.get_variable(str(id(inputs)), initializer=tf.constant_initializer(inputs), dtype=tf.float32, shape=inputs.shape) tf_meanparams = container_fmap(tf_get_variable, np_meanparams) tf_natparams = container_fmap(tf_get_variable, np_natparams) # Represent the set of natural/mean parameters for each coordinate update. all_tf_natparams = [None] * len(normalizers) all_tf_natparams_assign_ops = [[]] * len(normalizers) # all_tf_meanparams = [None] * len(normalizers) all_tf_meanparams_assign_ops = [[]] * len(normalizers) for i in range(len(normalizers)): cast = lambda inputs: tf.cast(inputs, dtype=tf.float32) tf_update = make_tffun(grad(neg_energy, i), *np_meanparams) values = container_fmap(cast, tf_update(*tf_meanparams)) for variable, value in zip(tf_meanparams[i], values): assign_op = variable.assign(value) all_tf_natparams_assign_ops[i].append(assign_op) all_tf_natparams[i] = values tf_update = make_tffun(grad(normalizers[i]), np_natparams[i]) values = container_fmap(cast, tf_update(all_tf_natparams[i])) # values = container_fmap(cast, tf_update(tf_natparams)) for variable, value in zip(tf_natparams[i], values): assign_op = variable.assign(value) all_tf_meanparams_assign_ops[i].append(assign_op) # all_tf_meanparams[i] = values # Set config for 1 CPU, 1 core, 1 thread(?). config = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1, device_count={'CPU': 1}) # Find out device placement. # config = tf.ConfigProto(log_device_placement=True) sess = tf.Session(config=config) sess.run(tf.global_variables_initializer()) natparams = sess.run(tf_natparams) print("ELBO ", elbo_fn(neg_energy, normalizers, natparams)) start = time.time() for _ in range(FLAGS.num_iterations): for i in range(len(normalizers)): _ = sess.run(all_tf_natparams_assign_ops[i]) _ = sess.run(all_tf_meanparams_assign_ops[i]) runtime = time.time() - start print("CAVI Runtime (s): ", runtime) natparams = sess.run(tf_natparams) print("ELBO ", elbo_fn(neg_energy, normalizers, natparams))
def animate(k): # clear the panel ax1.cla() ax2.cla() ax3.cla() # print rendering update if np.mod(k + 1, 5) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(len(num_elements))) if k == len(num_elements) - 1: print('animation rendering complete!') time.sleep(1) clear_output() # loop over panels, produce plots self.D = num_elements[k] cs = 0 for ax in [ax1, ax2, ax3]: # fit to data F = 0 predict = 0 w = 0 if ax == ax1: w = poly_weight_history[k] self.D = len(w) - 1 ax.set_title(str(self.D) + ' poly units', fontsize=14) self.predict = self.poly_predict elif ax == ax2: w = tanh_weight_history[k] self.D = len(w) - 1 ax.set_title(str(self.D) + ' tanh units', fontsize=14) self.predict = self.tanh_predict elif ax == ax3: item = min( len(self.y) - 1, num_elements[k] - 1, len(stump_weight_history) - 1) w = stump_weight_history[item] ax.set_title(str(item) + ' tree units', fontsize=14) self.predict = self.tree_predict ####### plot all and dress panel ###### # produce learned predictor s = np.linspace(xmin, xmax, 400) t = [self.predict(np.asarray([v]), w) for v in s] # plot approximation and data in panel if scatter == 'off': ax.plot(self.x, self.y, c='k', linewidth=2) elif scatter == 'on': ax.scatter(self.x, self.y, c='k', edgecolor='w', s=30, zorder=1) ax.plot(s, t, linewidth=2.75, color=self.colors[cs], zorder=3) cs += 1 # cleanup panel ax.set_xlim([xmin, xmax]) ax.set_ylim([ymin, ymax]) ax.set_xlabel(r'$x$', fontsize=14, labelpad=10) ax.set_ylabel(r'$y$', rotation=0, fontsize=14, labelpad=10) ax.set_xticks(np.arange(round(xmin), round(xmax) + 1, 1.0)) ax.set_yticks(np.arange(round(ymin), round(ymax) + 1, 1.0)) return artist,
(final_w1, final_w2, final_w3, final_w4) = adam.get_weights() #get trained weights test_data_x = get_test_data(all_data) #get test data test_data_x = normalize(test_data_x) #standardize data test_data_desired = get_test_data_result( all_data) #get exact outputs for test data test_data_predict = forward_pass( test_data_x, final_w1, final_w2, final_w3, final_w4) #run forward pass using trained weights test_data_size = np.size(test_data_predict, 0) for i in range(test_data_size): if test_data_predict[i] < 0.5: test_data_predict[i] = 0 else: test_data_predict[i] = 1 confuse = confusion_matrix(test_data_desired, test_data_predict) accuracy = confuse.trace() / confuse.sum() print(accuracy) plt.figure(1, figsize=(15, 6)) plt.subplot(1, 2, 1) x_axis = np.arange(np.size(adam.loss_array, 0)) plt.plot(x_axis, adam.loss_array, linewidth=3.0, label='loss ') plt.legend() plt.xlabel('$iterations$') plt.ylabel('$loss$') plt.axis('tight')
# In[2]: # Set the parameters of the HMM T = 200 # number of time bins K = 5 # number of discrete states D = 2 # number of latent dimensions N = 10 # number of observed dimensions # In[3]: # Make an LDS with the somewhat interesting dynamics parameters true_lds = ssm.LDS(N, D, emissions="gaussian") A0 = .99 * random_rotation(D, theta=np.pi / 20) # S = (1 + 3 * npr.rand(D)) S = np.arange(1, D + 1) R = np.linalg.svd(npr.randn(D, D))[0] * S A = R.dot(A0).dot(np.linalg.inv(R)) b = npr.randn(D) true_lds.dynamics.As[0] = A true_lds.dynamics.bs[0] = b _, x, y = true_lds.sample(T) # In[4]: # Plot the dynamics vector field xmins = x.min(axis=0) xmaxs = x.max(axis=0) npts = 20 true_lds.dynamics.As[0] = A XX, YY = np.meshgrid(np.linspace(xmins[0], xmaxs[0], npts),
def sca_ridge(NN, Flattened_Param, data_set, batch_size, sequence_length, input_size, low=0, high=50000, step_size=0.12, step_size_eps=0.0, rho=0.9, rho_eps=0.0, C=0, blocks=1, virtual_processors=1, tau=0.2, max_it=1000): """ Stochastic Convex Approximation """ message = 'Successful' if blocks < virtual_processors: blocks = virtual_processors err = anp.zeros(max_it) grad = anp.zeros(max_it) d = anp.zeros(len(Flattened_Param)) par_idx = array_split(anp.arange(len(Flattened_Param)), blocks) par_not_idx = anp.ones((blocks, len(Flattened_Param)), dtype=bool) for p in range(blocks): par_not_idx[p, par_idx[p]] = False for t in range(max_it): blocks_t = choice(blocks, virtual_processors, replace=False) images, labels = Utils.next_mini_batch(data_set, batch_size, sequence_length, input_size, low, high) labels = Utils.one_hot_encoding(labels) #predict and compute current error output = NN(images) J = get_Jacobian(NN, images) e = (labels.float() - output.float()).reshape(-1, 1) #compute loss on current iteration err[t] = Net.squared_loss(NN, images, labels, Flattened_Param, C) #err[t] = torch.mean(e**2) + C * torch.mean(param**2) #err[t] = torch.mean(torch.mul(e,e).float() + C * torch.mean(torch.mul(param,param))) #torch.mean((targets.float().reshape(-1,1)-Output.reshape(-1,1)) ** 2 #C * torch.mean(Flattened_Param**2) #compute gradient on current iteration g_loss = -2.0 * torch.mean( e.reshape(-1, 1).float() * torch.from_numpy(J).float(), dim=0) grad[t] = torch.sum((g_loss + (C * 2.0 * Flattened_Param))**2) #compute residuals r = e.reshape(-1, 1).float() + torch.mm( torch.from_numpy(J).float(), Flattened_Param.float()) for p in range(virtual_processors): # Get current block and indices block = blocks_t[p] idx = par_idx[block] # Compute current A block A_rowblock = anp.dot(J[:, idx].T, J) # Compute J.T times r Jr = anp.dot(J[:, idx].T, np.array(r.detach().numpy())).reshape(-1) #compute A,b matrices A = (rho / len(labels)) * A_rowblock[:, idx] + (C + tau) * anp.eye( len(idx)) b = ((rho/len(labels))*Jr - ((1-rho)*0.5)*d[idx] + (tau*Flattened_Param.detach().numpy()[idx]).reshape(len(Flattened_Param),)).reshape(-1,1) -\ (rho/len(labels))*np.dot(A_rowblock[:, par_not_idx[block]], Flattened_Param.detach().numpy()[par_not_idx[block]]) # Solve surrogate optimization try: par_hat = solve(A, b) # Update auxiliary variables d[idx] = (1 - rho) * d[idx] + rho * g_loss.detach().numpy()[idx] # Update variable Flattened_Param[idx] = ( (1 - step_size) * Flattened_Param[idx]).float() + torch.from_numpy( step_size * par_hat).float() except np.linalg.LinAlgError as e: if 'Singular matrix' in str(e): d[idx] = d[idx] Flattened_Param[idx] = Flattened_Param[idx] message = str(e) break # Update stepsize and rho rho = rho * (1 - rho_eps * rho) step_size = step_size * (1 - step_size_eps * step_size) #update the parameters in NN model NN = Utils.unflatten_param(Flattened_Param, NN) return NN, err, grad, message
def loss(self, y_pred, y_true): assert y_pred.shape[0] == y_true.shape[0] return -np.sum(np.log(y_pred[np.arange(y_pred.shape[0]), y_true.astype(int).ravel()]))
def delta(self, y_pred, y_true): y_pred[np.arange(y_pred.shape[0]), y_true.astype(int).ravel()] -= 1. return y_pred
def shifting_distribution(self, run, frames, x, **kwargs): self.colors = ['cyan', 'magenta', 'lime', 'orange'] # select inds of history to plot weight_history = run.weight_histories[0] cost_history = run.cost_histories[0] inds = np.arange(0, len(weight_history), int(len(weight_history) / float(frames))) weight_history_sample = [weight_history[v] for v in inds] cost_history_sample = [cost_history[v] for v in inds] start = inds[0] feature_transforms = run.feature_transforms # define activations self.activation = np.tanh if 'activation' in kwargs: self.activation = kwargs['activation'] self.normalize = False if 'normalize' in kwargs: self.normalize = kwargs['normalize'] xmin = 0 xmax = 1 ### set viewing limits for scatter plot ### xmins = [] xmaxs = [] ymins = [] ymaxs = [] for k in range(len(inds)): current_ind = inds[k] w_best = weight_history[current_ind] bl = self.feature_transforms(x, w_best[0]) # get all activations and look over each layer g = self.all_activations num_layers = len(g) for b in range(num_layers): f = g[b] xmin = np.min(copy.deepcopy(f[0, :])) xmax = np.max(copy.deepcopy(f[0, :])) ymin = np.min(copy.deepcopy(f[1, :])) ymax = np.max(copy.deepcopy(f[1, :])) xmins.append(xmin) xmaxs.append(xmax) ymins.append(ymin) ymaxs.append(ymax) xmin = min(xmins) xmax = max(xmaxs) xgap = (xmax - xmin) * 0.1 xmin -= xgap xmax += xgap ymin = min(ymins) ymax = max(ymaxs) ygap = (ymax - ymin) * 0.1 ymin -= ygap ymax += ygap # create figure fig = plt.figure(figsize=(9, 4)) artist = fig # create subplots show_history = False if 'show_history' in kwargs: show_history = kwargs['show_history'] gs = gridspec.GridSpec(1, 1) ax = plt.subplot(gs[0]) axs = [ax] if show_history == True: gs = gridspec.GridSpec(1, 2) ax = plt.subplot(gs[0]) axs = [ax] ax1 = plt.subplot(gs[1]) axs.append(ax1) c = 0 # remove whitespace from figure fig.subplots_adjust(left=0, right=1, bottom=0, top=1) # remove whitespace fig.subplots_adjust(wspace=0.01, hspace=0.01) # start animation num_frames = len(inds) print('starting animation rendering...') def animate(k): # get current index to plot current_ind = inds[k] if show_history == True: ax = axs[-1] ax.cla() ax.scatter(current_ind, cost_history[current_ind], s=60, color='r', edgecolor='k', zorder=3) self.plot_cost_history(ax, cost_history, start=0) # print rendering update if np.mod(k + 1, 25) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(num_frames)) if k == num_frames - 1: print('animation rendering complete!') time.sleep(1.5) clear_output() # pluck out current weights w_best = weight_history[current_ind] f = self.feature_transforms(x, w_best[0]) # produce static img ax = axs[0] ax.cla() self.scatter_activations(ax) ax.set_xlim([xmin, xmax]) ax.set_ylim([ymin, ymax]) #ax.legend(loc=0, borderaxespad=0.) return artist, anim = animation.FuncAnimation(fig, animate, frames=num_frames, interval=25, blit=False) return (anim)
def single_layer_animation(self, run, frames, x, **kwargs): # select inds of history to plot weight_history = run.weight_history cost_history = run.cost_history inds = np.arange(0, len(weight_history), int(len(weight_history) / float(frames))) weight_history_sample = [weight_history[v] for v in inds] cost_history_sample = [cost_history[v] for v in inds] start = inds[0] feature_transforms = run.feature_transforms # define activations self.activation = np.tanh if 'activation' in kwargs: self.activation = kwargs['activation'] self.normalize = False if 'normalize' in kwargs: self.normalize = kwargs['normalize'] # create figure fig = plt.figure(figsize=(9, 6)) artist = fig # create subplots N = np.shape(feature_transforms(x, weight_history[0][0]))[0] layer_sizes = [] self.feature_transforms(x, weight_history[0][0]) layer_sizes = [np.shape(v)[0] for v in self.all_activations] num_layers = len(layer_sizes) max_units = max(layer_sizes) show_history = False if 'show_history' in kwargs: show_history = kwargs['show_history'] gs = gridspec.GridSpec(num_layers, max_units) axs = [] for n in range(num_layers * max_units): ax = plt.subplot(gs[n]) axs.append(ax) if show_history == True: gs = gridspec.GridSpec(num_layers + 1, max_units) ax = plt.subplot(gs[0, :]) axs = [ax] c = 0 for n in range(num_layers): current_layer = layer_sizes[n] current_axs = [] for m in range(current_layer): ax = plt.subplot(gs[n + 1, m]) current_axs.append(ax) axs.append(current_axs) # remove whitespace from figure fig.subplots_adjust(left=0, right=1, bottom=0, top=1) # remove whitespace fig.subplots_adjust(wspace=0.01, hspace=0.01) # start animation num_frames = len(inds) print('starting animation rendering...') def animate(k): # get current index to plot current_ind = inds[k] if show_history == True: ax = axs[0] ax.cla() ax.scatter(current_ind, cost_history[current_ind], s=60, color='r', edgecolor='k', zorder=3) self.plot_cost_history(ax, cost_history, start=0) # print rendering update if np.mod(k + 1, 25) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(num_frames)) if k == num_frames - 1: print('animation rendering complete!') time.sleep(1.5) clear_output() # pluck out current weights w_best = weight_history[current_ind] f = self.feature_transforms(x, w_best[0]) # produce static img for u in range(num_layers): bl = self.all_activations[u] local_axs = axs[u + 1] for ax in local_axs: ax.cla() self.single_layer_distributions(u, bl, local_axs) return artist, anim = animation.FuncAnimation(fig, animate, frames=num_frames, interval=25, blit=False) return (anim)