def TrainNet(): total_device_num = args.node_num * args.gpu_num_per_node batch_size = total_device_num * args.batch_size_per_device if args.data_dir: assert os.path.exists(args.data_dir) print("Loading data from {}".format(args.data_dir)) (labels, images) = data_loader.load_imagenet( args.data_dir, args.image_size, batch_size, args.data_part_num, args.gpu_image_decoder, ) else: print("Loading synthetic data.") (labels, images) = data_loader.load_synthetic(args.image_size, batch_size) logits = model_dict[args.model](images) loss = flow.nn.sparse_softmax_cross_entropy_with_logits( labels, logits, name="softmax_loss") flow.losses.add_loss(loss) return loss
def run_synthetic_SGLD(): """Run synthetic SGLD""" theta1 = 0 theta2 = 1 sigma1 = numpy.sqrt(10) sigma2 = 1 sigmax = numpy.sqrt(2) X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100) minibatch_size = 1 total_iter_num = 1000000 lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num, factor=0.55) optimizer = mx.optimizer.create('sgld', learning_rate=None, rescale_grad=1.0, lr_scheduler=lr_scheduler, wd=0) updater = mx.optimizer.get_updater(optimizer) theta = mx.random.normal(0, 1, (2,), mx.cpu()) grad = nd.empty((2,), mx.cpu()) samples = numpy.zeros((2, total_iter_num)) start = time.time() for i in range(total_iter_num): if (i + 1) % 100000 == 0: end = time.time() print("Iter:%d, Time spent: %f" % (i + 1, end - start)) start = time.time() ind = numpy.random.randint(0, X.shape[0]) synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad=X.shape[0] / float(minibatch_size), grad=grad) updater('theta', grad, theta) samples[:, i] = theta.asnumpy() plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet) plt.colorbar() plt.show()
def InferenceNet(): total_device_num = args.node_num * args.gpu_num_per_node batch_size = total_device_num * args.batch_size_per_device if args.data_dir: assert os.path.exists(args.data_dir) print("Loading data from {}".format(args.data_dir)) (labels, images) = data_loader.load_imagenet(args.data_dir, args.image_size, batch_size, args.data_part_num) else: print("Loading synthetic data.") (labels, images) = data_loader.load_synthetic(args.image_size, batch_size) logits = model_dict[args.model](images) softmax = flow.nn.softmax(logits) return softmax
def run_synthetic_SGLD(): """Run synthetic SGLD""" theta1 = 0 theta2 = 1 sigma1 = numpy.sqrt(10) sigma2 = 1 sigmax = numpy.sqrt(2) X = load_synthetic(theta1=theta1, theta2=theta2, sigmax=sigmax, num=100) minibatch_size = 1 total_iter_num = 1000000 lr_scheduler = SGLDScheduler(begin_rate=0.01, end_rate=0.0001, total_iter_num=total_iter_num, factor=0.55) optimizer = mx.optimizer.create('sgld', learning_rate=None, rescale_grad=1.0, lr_scheduler=lr_scheduler, wd=0) updater = mx.optimizer.get_updater(optimizer) theta = mx.random.normal(0, 1, (2, ), mx.cpu()) grad = nd.empty((2, ), mx.cpu()) samples = numpy.zeros((2, total_iter_num)) start = time.time() for i in range(total_iter_num): if (i + 1) % 100000 == 0: end = time.time() print("Iter:%d, Time spent: %f" % (i + 1, end - start)) start = time.time() ind = numpy.random.randint(0, X.shape[0]) synthetic_grad(X[ind], theta, sigma1, sigma2, sigmax, rescale_grad=X.shape[0] / float(minibatch_size), grad=grad) updater('theta', grad, theta) samples[:, i] = theta.asnumpy() plt.hist2d(samples[0, :], samples[1, :], (200, 200), cmap=plt.cm.jet) plt.colorbar() plt.show()