def find_update_available_drivers(self, current_time): """Return and Update drivers who is available at current_time""" available_drivers = [] self.drivers_table = {} count = 0 for driver in self.drivers: if util.check_available(driver, current_time): count += 1 self.drivers_table[count] = driver if "current_time" not in driver: driver["current_time"] = driver["start_time"] t = current_time - driver["current_time"] driver["current_time"] = current_time # update driver location if driver["aid"] in self.routes: # driver's latest route route = self.routes[driver["aid"]][-1] # find next point B for cur in route["route_plan"]: # check if driver already passed cur location if cur["drop_by_end_time"] < current_time: util.update_driver_order(driver, cur) driver["lat"], driver["lng"] = cur["drop_by"] else: # time passed driver["lat"], driver[ "lng"] = util.calculate_new_location( driver, cur, t) break # if driver has no order, then they can drive to anywhere they want. For now, they drive to the closest plaza. if not driver["orders"]: driver["lat"], driver["lng"] = util.random_walk( driver, self.plaza, t) available_drivers.append(driver) return available_drivers
def create_json_from_file(file): G = nx.read_edgelist('../data/input/' + file + '.txt') edges = list(util.random_walk(graph=G, size=2000, metropolized=False)) G1 = nx.Graph() G1.add_path(edges) for n in G1: G1.node[n]['name'] = n d = json_graph.node_link_data(G1) json.dump(d, open('./static/' + file + '.json', 'w')) print('Wrote node-link JSON data to static/' + file + '.json')
#Make a dataset n_unlabeled, n_labeled, n_train = 500, 100, 100 X_labeled, y_labeled = generate_data2(n_labeled) X_unlabeled, y_unlabeled = generate_data2(n_unlabeled) X_train, y_train = generate_data2(n_train) #Plot some data total_data = np.concatenate((X_labeled, X_unlabeled, X_train), 0) total_targets = np.concatenate((y_labeled, y_unlabeled, y_train), 0) plt.scatter(total_data[:, 0], total_data[:, 1], c=total_targets) """Run the algorithms""" #Evaluate Random Walk y_pred = random_walk(y_labeled, X_labeled, X_unlabeled, X_train, gaussian_kernel(0.1)) MAE = np.mean(np.abs(y_pred - y_train)) accuracy = np.mean(np.equal(y_pred > 0.0, y_train > 0.0)) print('We have MAE %5.3f and accuracy %5.3f' % (MAE, accuracy)) #Evaluate Label Prop y_pred = label_propagation(y_labeled, X_labeled, X_unlabeled, X_train, gaussian_kernel(0.1), mu=0.4) MAE = np.mean(np.abs(y_pred - y_train)) accuracy = np.mean(np.equal(y_pred > 0.0, y_train > 0.0)) print('We have MAE %5.3f and accuracy %5.3f' % (MAE, accuracy)) """Experiment with different sizes of unlabeled data"""
def run(): mode = 'vaegan' vae_grad_scale = 0.0001 kld_weight = 1.0 z_gan_prop = False experiment_name = mode experiment_name += '_scale%.1e' % vae_grad_scale experiment_name += '_kld%.2f' % kld_weight if z_gan_prop: experiment_name += '_zprop' filename = 'savestates/lfw_' + experiment_name + '.pickle' in_filename = None print('experiment_name', experiment_name) print('in_filename', in_filename) print('filename', filename) # Fetch dataset x_train = lfw.lfw_imgs(alignment='deepfunneled', size=64, crop=50, shuffle=True) img_shape = x_train.shape[1:] # Normalize pixel intensities scaler = dp.UniformScaler(low=-1, high=1) x_train = scaler.fit_transform(x_train) # Setup network if in_filename is None: print('Creating new model') expressions = model_expressions(img_shape) else: print('Starting from %s' % in_filename) with open(in_filename, 'rb') as f: expressions = pickle.load(f) encoder, sampler, generator, discriminator = expressions model = vaegan.VAEGAN( encoder=encoder, sampler=sampler, generator=generator, discriminator=discriminator, mode=mode, vae_grad_scale=vae_grad_scale, kld_weight=kld_weight, ) # Prepare network inputs batch_size = 64 train_input = dp.Input(x_train, batch_size=batch_size, epoch_size=250) # Plotting n_examples = 100 examples = x_train[:n_examples] samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden)) samples_z = samples_z.astype(dp.float_) recon_video = Video('plots/lfw_' + experiment_name + '_reconstruction.mp4') sample_video = Video('plots/lfw_' + experiment_name + '_samples.mp4') sp.misc.imsave('lfw_examples.png', img_tile(dp.misc.to_b01c(examples))) def plot(): model.phase = 'test' examples_z = model.embed(examples) reconstructed = clip_range(model.reconstruct(examples_z)) recon_video.append(img_tile(dp.misc.to_b01c(reconstructed))) z = model.embed(x_train) z_mean = np.mean(z, axis=0) z_std = np.std(z, axis=0) model.hidden_std = z_std z_std = np.diagflat(z_std) samples_z = np.random.multivariate_normal(mean=z_mean, cov=z_std, size=(n_examples,)) samples_z = samples_z.astype(dp.float_) samples = clip_range(model.reconstruct(samples_z)) sample_video.append(img_tile(dp.misc.to_b01c(samples))) model.phase = 'train' model.setup(**train_input.shapes) # Train network runs = [ (150, dp.RMSProp(learn_rate=0.05)), (250, dp.RMSProp(learn_rate=0.03)), (100, dp.RMSProp(learn_rate=0.01)), (15, dp.RMSProp(learn_rate=0.005)), ] try: import timeit for n_epochs, learn_rule in runs: if mode == 'vae': vaegan.train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) else: vaegan.margin_train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) except KeyboardInterrupt: pass raw_input('\n\nsave model to %s?\n' % filename) with open(filename, 'wb') as f: expressions = encoder, sampler, generator, discriminator pickle.dump(expressions, f) model.phase = 'test' batch_size = 128 model.sampler.batch_size=128 z = model.embed(x_train) z_mean = np.mean(z, axis=0) z_std = np.std(z, axis=0) z_cov = np.cov(z.T) print(np.mean(z_mean), np.std(z_mean)) print(np.mean(z_std), np.std(z_std)) print(z_mean.shape, z_std.shape, z_cov.shape) model.sampler.batch_size=100 samples_z = model.embed(examples) print('Generating latent space video') walk_video = Video('plots/lfw_' + experiment_name + '_walk.mp4') for z in random_walk(samples_z, 500, n_dir_steps=10, mean=z_mean, std=z_cov): samples = clip_range(model.reconstruct(z)) walk_video.append(img_tile(dp.misc.to_b01c(samples)))
def run(): mode = 'vaegan' vae_grad_scale = 0.025 experiment_name = mode + 'scale_%.5f' % vae_grad_scale filename = 'savestates/mnist_' + experiment_name + '.pickle' in_filename = filename in_filename = None print('experiment_name', experiment_name) print('in_filename', in_filename) print('filename', filename) # Fetch dataset dataset = dp.dataset.MNIST() x_train, y_train, x_test, y_test = dataset.arrays(dp_dtypes=True) n_classes = dataset.n_classes img_shape = x_train.shape[1:] # Normalize pixel intensities scaler = dp.UniformScaler() x_train = scaler.fit_transform(x_train) x_test = scaler.transform(x_test) y_train = one_hot(y_train, n_classes).astype(dp.float_) y_test = one_hot(y_test, n_classes).astype(dp.float_) x_train = np.reshape(x_train, (x_train.shape[0], -1)) x_test = np.reshape(x_test, (x_test.shape[0], -1)) # Setup network if in_filename is None: print('Creating new model') expressions = model_expressions(img_shape) else: print('Starting from %s' % in_filename) with open(in_filename, 'rb') as f: expressions = pickle.load(f) encoder, sampler, generator, discriminator = expressions model = cond_vaegan.ConditionalVAEGAN( encoder=encoder, sampler=sampler, generator=generator, discriminator=discriminator, mode=mode, reconstruct_error=expr.nnet.BinaryCrossEntropy(), vae_grad_scale=vae_grad_scale, ) # Prepare network inputs batch_size = 128 train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size, epoch_size=250) # Plotting n_examples = 100 examples = x_test[:n_examples] examples_y = y_test[:n_examples] samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden)) samples_z = samples_z.astype(dp.float_) samples_y = ((np.arange(n_examples) // 10) % n_classes) samples_y = one_hot(samples_y, n_classes).astype(dp.float_) recon_video = Video('plots/mnist_' + experiment_name + '_reconstruction.mp4') sample_video = Video('plots/mnist_' + experiment_name + '_samples.mp4') sp.misc.imsave('plots/mnist_examples.png', img_tile(to_b01c(examples, img_shape))) def plot(): model.phase = 'test' model.sampler.batch_size=100 examples_z = model.embed(examples, examples_y) examples_recon = model.reconstruct(examples_z, examples_y) recon_video.append(img_tile(to_b01c(examples_recon, img_shape))) samples = model.reconstruct(samples_z, samples_y) sample_video.append(img_tile(to_b01c(samples, img_shape))) model.setup(**train_input.shapes) model.phase = 'train' # Train network runs = [ (75, dp.RMSProp(learn_rate=0.075)), (25, dp.RMSProp(learn_rate=0.05)), (5, dp.RMSProp(learn_rate=0.01)), (5, dp.RMSProp(learn_rate=0.005)), ] try: for n_epochs, learn_rule in runs: if mode == 'vae': vaegan.train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) else: vaegan.margin_train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) except KeyboardInterrupt: pass raw_input('\n\nsave model to %s?\n' % filename) with open(filename, 'wb') as f: expressions = encoder, sampler, generator, discriminator pickle.dump(expressions, f) model.phase = 'test' batch_size = 128 model.sampler.batch_size=128 z = [] i = 0 z = model.embed(x_train, y_train) print(z.shape) z_mean = np.mean(z, axis=0) z_std = np.std(z, axis=0) z_cov = np.cov(z.T) print(np.mean(z_mean), np.std(z_mean)) print(np.mean(z_std), np.std(z_std)) print(z_mean.shape, z_std.shape, z_cov.shape) raw_input('\n\ngenerate latent space video?\n') print('Generating latent space video') walk_video = Video('plots/mnist_' + experiment_name + '_walk.mp4') for z in random_walk(samples_z, 500, n_dir_steps=10, mean=z_mean, std=z_cov): samples = model.reconstruct(z, samples_y) walk_video.append(img_tile(to_b01c(samples, img_shape))) print('Generating AdversarialMNIST dataset') _, y_train, _, y_test = dataset.arrays(dp_dtypes=True) n = 0 batch_size = 512 advmnist_size = 1e6 x_advmnist = np.empty((advmnist_size, 28*28)) y_advmnist = np.empty((advmnist_size,)) while n < advmnist_size: samples_z = np.random.multivariate_normal(mean=z_mean, cov=z_cov, size=batch_size) samples_z = samples_z.astype(dp.float_) start_idx = n % len(y_train) stop_idx = (n + batch_size) % len(y_train) if start_idx > stop_idx: samples_y = np.concatenate([y_train[start_idx:], y_train[:stop_idx]]) else: samples_y = y_train[start_idx:stop_idx] y_advmnist[n:n+batch_size] = samples_y[:advmnist_size-n] samples_y = one_hot(samples_y, n_classes).astype(dp.float_) samples = model.reconstruct(samples_z, samples_y) x_advmnist[n:n+batch_size] = samples[:advmnist_size-n] n += batch_size x_train = x_advmnist y_train = y_advmnist import sklearn.neighbors clf = sklearn.neighbors.KNeighborsClassifier(n_neighbors=1, algorithm='brute', n_jobs=-1) clf.fit(x_train, y_train) print('KNN predict') step = 2500 errors = [] i = 0 while i < len(x_test): print(i) errors.append(clf.predict(x_test[i:i+step]) != y_test[i:i+step]) i += step error = np.mean(errors) print('Test error rate: %.4f' % error) print('DONE ' + experiment_name)
def run(): mode = 'gan' experiment_name = mode + '_stride_local_discrimination' filename = 'savestates/cifar_cond_' + experiment_name + '.pickle' in_filename = filename in_filename = None print('experiment_name', experiment_name) print('in_filename', in_filename) print('filename', filename) # Fetch dataset dataset = dp.dataset.CIFAR10() x_train, y_train, x_test, y_test = dataset.arrays(dp_dtypes=True) n_classes = dataset.n_classes # Normalize pixel intensities scaler = dp.StandardScaler() x_train = scaler.fit_transform(x_train) x_test = scaler.transform(x_test) y_train = one_hot(y_train, n_classes).astype(dp.float_) y_test = one_hot(y_test, n_classes).astype(dp.float_) # Setup network if in_filename is None: print('Creating new model') img_shape = x_train.shape[1:] expressions = model_expressions(img_shape) else: print('Starting from %s' % in_filename) with open(in_filename, 'rb') as f: expressions = pickle.load(f) encoder, sampler, generator, discriminator = expressions model = cond_vaegan.ConditionalVAEGAN( encoder=encoder, sampler=sampler, generator=generator, discriminator=discriminator, mode=mode, ) # Prepare network inputs batch_size = 64 train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size, epoch_size=150) # Plotting n_examples = 100 examples = x_test[:n_examples] examples_y = y_test[:n_examples] samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden)) samples_z = samples_z.astype(dp.float_) samples_y = ((np.arange(n_examples) // 10) % n_classes) samples_y = one_hot(samples_y, n_classes).astype(dp.float_) recon_video = Video('plots/cifar_' + experiment_name + '_reconstruction.mp4') sample_video = Video('plots/cifar_' + experiment_name + '_samples.mp4') sp.misc.imsave('cifar_examples.png', img_tile(dp.misc.to_b01c(examples))) def plot(): examples_z = model.embed(examples, examples_y) examples_recon = model.reconstruct(examples_z, examples_y) examples_recon = clip_range(examples_recon) recon_video.append(img_tile(dp.misc.to_b01c(examples_recon))) samples = clip_range(model.reconstruct(samples_z, samples_y)) sample_video.append(img_tile(dp.misc.to_b01c(samples))) model.setup(**train_input.shapes) # Train network runs = [ # (10, dp.RMSProp(learn_rate=0.08)), # (25, dp.RMSProp(learn_rate=0.12)), # (100, dp.RMSProp(learn_rate=0.1)), (150, dp.RMSProp(learn_rate=0.075)), (150, dp.RMSProp(learn_rate=0.06)), (150, dp.RMSProp(learn_rate=0.05)), (150, dp.RMSProp(learn_rate=0.04)), (25, dp.RMSProp(learn_rate=0.01)), ] try: for n_epochs, learn_rule in runs: if mode == 'vae': vaegan.train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) else: vaegan.margin_train(model, train_input, learn_rule, n_epochs, epoch_callback=plot) except KeyboardInterrupt: pass raw_input('\n\nsave model to %s?\n' % filename) with open(filename, 'wb') as f: expressions = encoder, sampler, generator, discriminator pickle.dump(expressions, f) print('Generating latent space video') walk_video = Video('plots/cifar_' + experiment_name + '_walk.mp4') for z in random_walk(samples_z, 500, step_std=0.15): samples = clip_range(model.reconstruct(z, samples_y)) walk_video.append(img_tile(dp.misc.to_b01c(samples)))
runs_per_trial = 10 best_MAE = 1000 for trial in range(trials): # Generate data n_labeled = 10 n_train = 100 # n_test = 200 X_labeled, y_labeled = generate_data2(n_labeled) X_unlabeled, _ = generate_data2(n_unlabeled) X_train, y_train = generate_data2(n_train) # X_test, y_test = generate_data2(n_test) for run in range(runs_per_trial): std = 0.01 + np.random.rand() / 4 try: y_pred = random_walk(y_labeled, X_labeled, X_unlabeled, X_train, gaussian_kernel(std)) except np.linalg.linalg.LinAlgError: y_pred = np.zeros_like(y_train) MAE = np.mean(np.abs(y_pred - y_train)) accuracy = np.mean(np.equal(y_pred > 0.0, y_train > 0.0)) if MAE > 1.0: continue f.write('%5.3f,%5.3f\n' % (std, MAE)) fig, axarr = plt.subplots(len(nums_unlabeled), 1) for ax in axarr: ax.set_ylim(0, 1) for i, n_unlabeled in enumerate(nums_unlabeled): with open('log/%s.txt' % n_unlabeled, 'r') as f: for line in f: data = list(map(float, line.split(',')))