def pbc_sep(p1, p2): arrow = sep(p1, p2) rem = np.mod(arrow, 18) # The image in the first cube mic_separation_vector = np.mod(rem+18/2, 18)-18/2 return np.array(mic_separation_vector)
def updatePlot(self, time, data): ''' Adds data to the plot. time is a list, data is a list of lists, each list corresponding to a line on the plot ''' if self.init == True: # Initialize the plot the first time routine is called for i in range(len(data)): # Instantiate line object and add it to the axes self.line.append( Line2D(time, data[i], color=self.colors[np.mod(i, len(self.colors) - 1)], ls=self.line_styles[np.mod( i, len(self.line_styles) - 1)], label=self.legend if self.legend != None else None)) self.ax.add_line(self.line[i]) self.init = False # add legend if one is specified if self.legend != None: plt.legend(handles=self.line) else: # Add new data to the plot # Updates the x and y data of each line. for i in range(len(self.line)): self.line[i].set_xdata(time) self.line[i].set_ydata(data[i]) # Adjusts the axis to fit all of the data self.ax.relim() self.ax.autoscale()
def animate(k): # clear panels ax.cla() ax2.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 total model on original dataset ####### ax.scatter(self.x_train,self.y_train,color = 'k',s = 60,edgecolor = self.colors[1] ,linewidth = 1.5) ax.scatter(self.x_valid,self.y_valid,color = 'k',s = 60,edgecolor = self.colors[0] ,linewidth = 1.5) # plot fit if k > 0: # plot current fit a = inds[k-1] steps = runner.best_steps[:a+1] self.draw_fit(ax,steps,a) # plot train / valid errors up to this point self.plot_train_valid_errors(ax2,k-1,train_errors,valid_errors,inds) # make panel size ax.set_xlim([xmin,xmax]) ax.set_ylim([ymin,ymax]) return artist,
def animate(k): # clear panels ax.cla() ax1.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() # get current run for cost function history plot a = inds[k] run = runs[a] # pluck out current weights self.draw_fit_trainval(ax, runs, a) # show cost function history if show_history == True: ax1.cla() self.plot_train_valid_errors(ax1, k, train_errors, valid_errors, labels) return artist,
def animate(k): # clear panels ax1.cla() ax2.cla() ax3.cla() ax4.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 a = inds[k] steps = runner.best_steps[:a+1] # produce static img self.draw_boundary(ax1,steps,a) self.static_N2_simple(ax1,train_valid = 'original') self.draw_boundary(ax2,steps,a) self.static_N2_simple(ax2,train_valid = 'train') self.draw_boundary(ax3,steps,a) self.static_N2_simple(ax3,train_valid = 'validate') #### plot training / validation errors #### self.plot_train_valid_errors(ax4,a,train_errors,valid_errors,inds) return artist,
def animate(k): # clear current plots for next frame of animation ax1.cla() ax2.cla() # print rendering update if np.mod(k + 1, 25) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(num_frames)) if k == len(lam_range) - 1: print('animation rendering complete!') time.sleep(1.5) clear_output() # choose current regularization parameter self.lam = lam_range[k] # run gradient descent with two user-chosen initial values and chosen regularization value w_hist_1 = self.gradient_descent(w=w_init_1, alpha=alpha, max_its=max_its) w_hist_2 = self.gradient_descent(w=w_init_2, alpha=alpha, max_its=max_its) # plot data points in panel 1, and cost function in panel 2 self.show_data_and_fit(ax1, w_hist_1[-1], color='lime') self.show_data_and_fit(ax1, w_hist_2[-1], color='magenta') # plot logistic surface, as well as gradient descent paths, in panel 2 self.plot_logistic_surface(ax2) self.show_descent_path(ax2, w_hist_1, color='lime') self.show_descent_path(ax2, w_hist_2, color='magenta') return artist,
def animate(t): # clear panels for next slide ax1.cla() ax2.cla() ax3.cla() # print rendering update if np.mod(t+1,25) == 0: print ('rendering animation frame ' + str(t+1) + ' of ' + str(num_frames)) if t == num_frames - 1: print ('animation rendering complete!') time.sleep(1.5) clear_output() # plot function 1 ax1.plot_surface(w1_vals,w2_vals,g1_vals,alpha = 0.1,color = 'w',rstride=10, cstride=10,linewidth=2,edgecolor = 'k') ax1.set_title("$g_1$",fontsize = 15) ax1.view_init(view[0],view[1]) ax1.axis(set_axis) # plot function 2 ax2.plot_surface(w1_vals,w2_vals,g2_vals,alpha = 0.1,color = 'w',rstride=10, cstride=10,linewidth=2,edgecolor = 'k') ax2.set_title("$g_2$",fontsize = 15) ax2.view_init(view[0],view[1]) ax2.axis(set_axis) # plot combination of both alpha = alpha_vals[t] g_combo = alpha*g1_vals + (1 - alpha)*g2_vals ax3.plot_surface(w1_vals,w2_vals,g_combo,alpha = 0.1,color = 'k',rstride=10, cstride=10,linewidth=2,edgecolor = 'k') ax3.set_title('$\\alpha\,g_1 + (1 - \\alpha)\,g_2$',fontsize = 15) ax3.view_init(view[0],view[1]) ax3.axis(set_axis) return artist,
def animate(k): # clear panels ax1.cla() # current color color = self.colorspec[k] # 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() ###### make left panel - plot data and fit ###### # initialize fit w = self.w_hist[k] y_fit = w[0] + x_fit*w[1] # scatter data self.scatter_pts(ax1) # plot fit to data ax1.plot(x_fit,y_fit,color = color,linewidth = 3) return artist,
def animate(k): ax.cla() # print rendering update if np.mod(k+1,25) == 0: print ('rendering animation frame ' + str(k+1) + ' of ' + str(num_slides)) if k == num_slides - 1: print ('animation rendering complete!') time.sleep(1.5) clear_output() # define beta special_size = size_range[k] special_weight = weight_range[k] beta = np.ones((1,self.y.size)) beta[:,ind1] = special_weight # run optimizer w_hist,g_hist = bits.newtons_method(g,w,self.x,self.y,beta,max_its) w_best = w_hist[-1] self.model = lambda data: bits.model(data,w_best) # scatter plot all data self.plot_data(ax,special_class,special_size) # draw decision boundary self.draw_decision_boundary(ax) return artist,
def animate(k): # clear panels ax.cla() ax1.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() # get current run for cost function history plot a = inds[k] run = runs[a] # pluck out current weights self.draw_fit(ax,ax1,runs,a) # show cost function history if show_history == True: ax1.cla() ax1.scatter(current_ind,cost_history[current_ind],s = 60,color = 'r',edgecolor = 'k',zorder = 3) self.plot_cost_history(ax1,cost_history,start) return artist,
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,
def animate(k): # clear panels ax1.cla() # current color color = self.colorspec[k] # 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() ###### make left panel - plot data and fit ###### # initialize fit w = self.w_hist[k] y_fit = self.sigmoid(w[0] + x_fit*w[1]) # scatter data self.scatter_pts(ax1) # plot fit to data ax1.plot(x_fit,y_fit,color = color,linewidth = 2) ###### make right panel - plot contour and steps ###### if k == 0: ax2.scatter(w[0],w[1],s = 90,facecolor = color,edgecolor = 'k',linewidth = 0.5, zorder = 3) if k > 0 and k < num_frames: self.plot_pts_on_contour(ax2,k,color) if k == num_frames -1: ax2.scatter(w[0],w[1],s = 90,facecolor = color,edgecolor = 'k',linewidth = 0.5, zorder = 3) return artist,
def animate(k): # clear panels ax1.cla() ax2.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() # scatter original data ax1.scatter(self.x.flatten(), self.y.flatten(), color='k', s=40, edgecolor='w', linewidth=0.9) if k > 0: # get current run for cost function history plot a = inds[k - 1] run = runs[a] # pluck out current weights self.draw_fit(ax1, run, a) # cost function history plot self.cost_history_plot(ax2, cost_evals, a) return artist,
def animate(k): # clear panels ax.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() # scatter original data ax.scatter(self.x.flatten(), self.y.flatten(), color='k', s=40, edgecolor='w', linewidth=0.9) if k > 0: # get current run for cost function history plot a = inds[k - 1] model = run.models[a] steps = run.best_steps[:a + 1] # pluck out current weights self.draw_boosting_fit(ax, steps, a) return artist,
def angle_diff(theta1, theta2): ''' An angle subtraction method. ''' if isinstance(theta1, tf.Tensor) or isinstance(theta2, tf.Tensor): delta = tf.mod(theta1 - theta2 - pi, 2 * pi) - pi else: delta = np.mod(theta1 - theta2 - pi, 2 * pi) - pi return delta
def animate(k): # clear panels ax.cla() lam = lams[k] # 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() # run optimization if algo == 'gradient_descent': weight_history, cost_history = self.gradient_descent( g, w, self.x, self.y, lam, alpha_choice, max_its, batch_size) if algo == 'RMSprop': weight_history, cost_history = self.RMSprop( g, w, self.x, self.y, lam, alpha_choice, max_its, batch_size) # choose set of weights to plot based on lowest cost val ind = np.argmin(cost_history) # classification? then base on accuracy if 'counter' in kwargs: # create counting cost history as well counts = [ counter(v, self.x, self.y, lam) for v in weight_history ] if k == 0: ind = np.argmin(counts) count = counts[ind] acc = 1 - count / self.y.size acc = np.round(acc, 2) # save lowest misclass weights w_best = weight_history[ind][1:] # plot ax.axhline(c='k', zorder=2) # make bar plot ax.bar(np.arange(0, len(w_best)), w_best, color='k', alpha=0.5) # dress panel title1 = r'$\lambda = ' + str(np.round(lam, 2)) + '$' costval = cost_history[ind][0] title2 = ', cost val = ' + str(np.round(costval, 2)) if 'counter' in kwargs: title2 = ', accuracy = ' + str(acc) title = title1 + title2 ax.set_title(title) ax.set_xlabel('learned weights') return artist,
def animate(k): # clear panels ax1.cla() # set axis in left panel self.move_axis_left(ax1) # current color color = self.colorspec[k] # 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() ###### make left panel - plot data and fit ###### # initialize fit w = self.w_hist[k] # reshape and plot the surface, as well as where the zero-plane is y_fit = w[0] + w[1]*x1_vals + w[2]*x2_vals # plot cost surface ax1.plot_surface(x1_vals,x2_vals,y_fit,alpha = 0.1,color = color,rstride=25, cstride=25,linewidth=0.25,edgecolor = 'k',zorder = 2) # scatter data self.scatter_pts(ax1) #ax1.view_init(view[0],view[1]) # plot connector between points for visualization purposes if k == 0: w_new = self.w_hist[k] g_new = self.least_squares(w_new)[0] ax2.scatter(k,g_new,s = 0.1,color = 'w',linewidth = 2.5,alpha = 0,zorder = 1) # plot approx if k > 0: w_old = self.w_hist[k-1] w_new = self.w_hist[k] g_old = self.least_squares(w_old)[0] g_new = self.least_squares(w_new)[0] ax2.plot([k-1,k],[g_old,g_new],color = color,linewidth = 2.5,alpha = 1,zorder = 2) # plot approx ax2.plot([k-1,k],[g_old,g_new],color = 'k',linewidth = 3.5,alpha = 1,zorder = 1) # plot approx # set viewing limits for second panel ax2.axhline(y=0, color='k',zorder = 0,linewidth = 0.5) ax2.set_xlabel('iteration',fontsize = 12) ax2.set_ylabel(r'$g(\mathbf{w})$',fontsize = 12,rotation = 0,labelpad = 25) ax2.set_xlim([-0.5,len(self.w_hist)]) # set axis in left panel self.move_axis_left(ax1) return artist,
def animate(k): # clear panels 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() # scatter data ind0 = np.argwhere(self.y == +1) ind0 = [e[1] for e in ind0] ind1 = np.argwhere(self.y == -1) ind1 = [e[1] for e in ind1] for ax in [ax1,ax2,ax3]: ax.scatter(self.x[0,ind0],self.x[1,ind0],s = pt_size, color = self.colors[0], edgecolor = 'k',antialiased=True) ax.scatter(self.x[0,ind1],self.x[1,ind1],s = pt_size, color = self.colors[1], edgecolor = 'k',antialiased=True) if k == 0: ax1.set_title(str(0) + ' units fit to data',fontsize = 14,color = 'w') ax1.set_title(str(0) + ' units fit to data',fontsize = 14,color = 'w') ax1.set_title(str(0) + ' units fit to data',fontsize = 14,color = 'w') ax1.set_xlim([xmin1,xmax1]) ax1.set_ylim([xmin2,xmax2]) ax2.set_xlim([xmin1,xmax1]) ax2.set_ylim([xmin2,xmax2]) ax3.set_xlim([xmin1,xmax1]) ax3.set_ylim([xmin2,xmax2]) # plot fit if k > 0: # get current run a1 = inds1[k-1] a2 = inds2[k-1] a3 = inds3[k-1] run1 = runs1[a1] a1 = len(run1.w_init) - 1 run2 = runs2[a2] model3 = runs3.models[a3] steps = runs3.best_steps[:a3+1] # plot models to data self.draw_fit(ax1,run1,a1) self.draw_fit(ax2,run2,a2 + 1) self.draw_boosting_fit(ax3,steps,a3) return artist,
def animate(k): # clear panels ax1.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 x ax1.plot(np.arange(1, x.size + 1), x, alpha=1, c='k', linewidth=1, zorder=2) # plot moving average - initial conditions if k == 1: ax1.plot(np.arange(1, T + 1), y[:T], alpha=0.75, c='fuchsia', linewidth=3, zorder=3) # make vertical visual guides ax1.axvline(x=1) ax1.axvline(x=T) # plot moving average - everything after and including initial conditions if k > 1: j = k - 1 # plot ax1.plot(np.arange(1, T + j + 1), y[:T + j], alpha=0.7, c='fuchsia', linewidth=3, zorder=3) # make vertical visual guides ax1.axvline(x=j) ax1.axvline(x=j + T - 1) # label axes ax1.set_xlim([xmin, xmax]) ax1.set_ylim([ymin, ymax]) return artist,
def advect(f, vx, vy): """Instead of moving the cell centers forward in time using the velocity fields, we look for the particles which end up exactly at the cell centers by tracing backwards in time from the cell centers. See 'implicit Euler integration.'""" rows, cols = f.shape cell_xs, cell_ys = np.meshgrid(np.arange(cols), np.arange(rows)) center_xs = (cell_xs - vx).ravel() # look backwards one timestep center_ys = (cell_ys - vy).ravel() left_ix = np.floor(center_ys).astype(int) # get locations of source cells. top_ix = np.floor(center_xs).astype(int) rw = center_ys - left_ix # relative weight of cells on the right bw = center_xs - top_ix # same for cells on the bottom 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 cells closest to the source of the cell center. 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 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 _sim_step(self, a): pos = self._qsoc.snd_rcv(a) # Transform the relative cart position to [-0.4, +0.4] if self._calibrated: pos[0] = (pos[0] - self._norm_x_lim[0] ) - 1. / 2. * (self._norm_x_lim[1] - self._norm_x_lim[0]) x_dot = self._vel_filt_x(pos[0:1]) th_dot = self._vel_filt_th(pos[1:2]) # Normalize the angle from 0. to 2.*pi pos[1] = np.mod(pos[1], 2. * np.pi) - np.pi return np.concatenate([pos, x_dot, th_dot])
def animate(k): # clear panels ax.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() #### scatter data #### # plot points in 2d and 3d ind0 = np.argwhere(self.y == +1) ind0 = [e[1] for e in ind0] ax.scatter(self.x[0, ind0], self.x[1, ind0], s=55, color=self.colors[0], edgecolor='k') ind1 = np.argwhere(self.y == -1) ind1 = [e[1] for e in ind1] ax.scatter(self.x[0, ind1], self.x[1, ind1], s=55, color=self.colors[1], edgecolor='k') # plot boundary if k > 0: # get current run for cost function history plot a = inds[k - 1] model = run.models[a] steps = run.best_steps[:a + 1] # pluck out current weights self.draw_boosting_fit(ax, steps, a) # cleanup panel ax.set_yticklabels([]) ax.set_xticklabels([]) ax.set_xticks([]) ax.set_yticks([]) ax.set_xlabel(r'$x_1$', fontsize=15) ax.set_ylabel(r'$x_2$', fontsize=15, rotation=0, labelpad=20) return artist,
def animate(k): # clear panels ax1.cla() ax2.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() #### scatter data #### # plot points in 2d and 3d ind0 = np.argwhere(self.y == +1) ind0 = [e[1] for e in ind0] ax1.scatter(self.x[0,ind0],self.x[1,ind0],s = 55, color = self.colors[0], edgecolor = 'k') ind1 = np.argwhere(self.y == -1) ind1 = [e[1] for e in ind1] ax1.scatter(self.x[0,ind1],self.x[1,ind1],s = 55, color = self.colors[1], edgecolor = 'k') # plot boundary if k > 0: # get current run for cost function history plot a = inds[k-1] run = runs[a] # pluck out current weights self.draw_fit(ax1,run,a) # cost function value ax2.plot(np.arange(1,num_elements + 1),cost_evals,color = 'k',linewidth = 2.5,zorder = 1) ax2.scatter(a + 1,cost_evals[a],color = self.colors[0],s = 70,edgecolor = 'w',linewidth = 1.5,zorder = 3) # cleanup panels ax1.set_yticklabels([]) ax1.set_xticklabels([]) ax1.set_xticks([]) ax1.set_yticks([]) ax1.set_xlabel(r'$x_1$',fontsize = 15) ax1.set_ylabel(r'$x_2$',fontsize = 15,rotation = 0,labelpad = 20) ax2.set_xlabel('number of units',fontsize = 12) ax2.set_title('cost function plot',fontsize = 14) ax2.set_xlim([minxc,maxxc]) ax2.set_ylim([ymin,ymax]) return artist,
def gradient_descent(g, w_unflat, alpha_choice, max_its, version, **kwargs): verbose = False if 'verbose' in kwargs: verbose = kwargs['verbose'] # flatten the input function, create gradient based on flat function g_flat, unflatten, w = flatten_func(g, w_unflat) grad = compute_grad(g) # record history w_hist = [] w_hist.append(w_unflat) # over the line for k in range(max_its): if verbose == True: if np.mod(k, 5) == 0: print('started iteration ' + str(k) + ' of ' + str(max_its)) # check if diminishing steplength rule used if alpha_choice == 'diminishing': alpha = 1 / float(k) else: alpha = alpha_choice # plug in value into func and derivative grad_eval = grad(w_unflat) grad_eval, _ = flatten(grad_eval) ### normalized or unnormalized descent step? ### if version == 'normalized': grad_norm = np.linalg.norm(grad_eval) if grad_norm == 0: grad_norm += 10**-6 * np.sign(2 * np.random.rand(1) - 1) grad_eval /= grad_norm # take descent step w = w - alpha * grad_eval # record weight update w_unflat = unflatten(w) w_hist.append(w_unflat) if verbose == True: print('finished all ' + str(max_its) + ' iterations') return w_hist
def animate(k): # clear panels for next slide 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 function 1 ax1.plot(w_plot, g1_plot, color='k', zorder=1) ax1.set_title(title1, fontsize=15) # plot function 2 ax2.plot(w_plot, g2_plot, color='k', zorder=1) ax2.set_title(title2, fontsize=15) # plot combination of both alpha = alpha_vals[k] if mode == 'regularization': g_combo = g1_plot + alpha * g2_plot else: g_combo = (1 - alpha) * g1_plot + alpha * g2_plot ax3.plot(w_plot, g_combo, color='k', zorder=1) ax3.set_title(title3, fontsize=15) # set vertical limits ax1.set_ylim([g1_min, g1_max]) ax2.set_ylim([g2_min, g2_max]) # set vertical limit markers gmin = np.amin(g_combo) gmax = np.amax(g_combo) g_gap = 0.2 * (gmax - gmin) gmin = gmin - g_gap gmax = gmax + g_gap ax3.set_ylim([gmin, gmax]) return artist,
def animate(k): # clear the panel ax.cla() # print rendering update if np.mod(k + 1, 5) == 0: print('rendering animation frame ' + str(k + 1) + ' of ' + str(self.num_elements)) if k == self.num_elements - 1: print('animation rendering complete!') time.sleep(1) clear_output() ####### plot a stump ###### # pick a stump w = np.zeros((self.num_elements, 1)) w[k] = 1 # produce learned predictor s = np.linspace(xmin, xmax, 400) t = [self.tree_predict(np.asarray([v]), w) for v in s] # plot approximation and data in panel ax.scatter(self.x, self.y, c='k', edgecolor='w', s=50, zorder=2) ax.plot(s, t, linewidth=2.5, color=self.colors[0], zorder=3) # plot horizontal axis and dashed line to split point ax.axhline(c='k', linewidth=1, zorder=0) mid = (self.levels[k][0] + self.levels[k][1]) / float(2) o = np.linspace(0, ymax, 100) e = np.ones((100, 1)) sp = self.splits[k] ax.plot(sp * e, o, linewidth=1.5, color=self.colors[1], linestyle='--', zorder=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))
def animate(k): # clear panels ax1.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 x ax1.plot(np.arange(1, x.size + 1), x, alpha=1, c='k', linewidth=1, zorder=2) # plot running average - everything after and including initial conditions if k >= 1: j = k - 1 # plot a = np.arange(1, j + 2) b = y[:j + 1] #print (a.shape,b.shape) ax1.plot(np.arange(1, j + 2), y[:j + 1], alpha=0.7, c='fuchsia', linewidth=3, zorder=3) ax1.scatter(1, y[0], alpha=0.7, c='fuchsia', linewidth=3, zorder=3) # label axes ax1.set_xlim([xmin, xmax]) ax1.set_ylim([ymin, ymax]) return artist,
def animate(k): # clear panels ax1.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 x ax1.plot(np.arange(1, x.size + 1), x, alpha=1, c='k', linewidth=1, zorder=2) # create y if k == 0: alpha = params[0] y = func(x, alpha) #ax1.plot(np.arange(1,y.size + 1),y,alpha = 1,c = 'fuchsia',linewidth = 3,zorder = 3); ax1.set_title(r'Original data') if k > 0: alpha = params[k - 1] y = func(x, alpha) ax1.plot(np.arange(1, y.size + 1), y, alpha=0.9, c='fuchsia', linewidth=3, zorder=3) ax1.set_title(r'$\alpha = $ ' + str(np.round(alpha, 2))) # label axes ax1.set_xlabel(r'$t$', fontsize=13) ax1.set_xlim([xmin, xmax]) ax1.set_ylim([ymin, ymax]) return artist,
def animate(k): # clear the panels ax1.cla() ax2.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() if k > 0: # pull current tracer tracer = tracer_range[k - 1] tracer = np.array([float(tracer[0]), float(tracer[1])]) tracer.shape = (2, 1) g_tracer = func(tracer) ### draw 3d version ### for ax in [ax1, ax2]: # plot function plot_func(func, view, ax) if k > 0: # scatter anchor point ax.scatter(anchor[0], anchor[1], g_anchor, s=50, c='lime', edgecolor='k', linewidth=1) # plot hyperplane connecting the anchor to tracer secant(func, anchor, tracer, ax) # reset tracer tracer = np.flipud(tracer) return artist,
def test_mod_arg1(): fun = lambda x, y : np.mod(x, y) d_fun = grad(fun, 1) check_grads(fun, npr.rand(), npr.rand()) check_grads(d_fun, npr.rand(), npr.rand())
def test_mod_arg1(): fun = lambda x, y : np.mod(x, y) check_grads(fun)(npr.rand(), npr.rand())