def sample_binomial(p): """Samples elementwise from the binomial distribution with probability p""" if use_debug_rng: r = myrand.rand(p.shape) else: r = gp.rand(p.shape) # n = np.random.random(p.shape) # n = gp.rand(p.shape) # r = gp.zeros(p.shape) return r < p
n_cols = 784 n_iters = 60000 / n_rows * 3 def write_bytes_to_file(file, data): for x in data: file.write(chr(int(x))) # use gnumpy rng gp.seed_rand(1) with open("rng_gnumpy.dat", "wb") as file: for i in range(n_iters): print "%d / %d\r" % (i, n_iters), gx = gp.rand((n_rows, n_cols)) x = gp.as_numpy_array(gx) fx = np.reshape(x, -1) bx = np.floor(fx * 256) write_bytes_to_file(file, bx) # use lcg rng mr.seed(1) with open("rng_lcg.dat", "wb") as file: for i in range(n_iters): print "%d / %d\r" % (i, n_iters), gx = mr.rand((n_rows, n_cols)) x = gp.as_numpy_array(gx) fx = np.reshape(x, -1) bx = np.floor(fx * 256) write_bytes_to_file(file, bx)
X, TX = rbmutil.load_mnist(False) # load ruslan's training set mdata = scipy.io.loadmat("mnist.mat") X = gp.as_garray(mdata['fbatchdata']) # create output directory rbmutil.enter_rbm_plot_directory("mnist", cfg.n_hid, cfg.use_pcd, cfg.n_gibbs_steps, "training_from.txt", clean=False) # Build RBM rbm = RestrictedBoltzmannMachine(cfg.batch_size, cfg.n_vis, cfg.n_hid, cfg.n_gibbs_steps) mr.seed(30) rbm.weights = 0.01 * (0.5 - mr.rand(rbm.weights.shape)) # load weights if use_ruslans_start_weights: filename = "matlab_epoch%d.mat" % (from_epoch-1 + 1) print "Loading Ruslan's start weights from %s" % filename mdata = scipy.io.loadmat(filename) ml.rbm.weights = gp.as_garray(mdata['vishid']) ml.rbm.bias_vis = gp.as_garray(mdata['visbiases']) ml.rbm.bias_hid = gp.as_garray(mdata['hidbiases']) else: print "Loading python start weights" filename = "weights-%02d.npz" % (from_epoch-1) rbmutil.load_parameters(rbm, filename) # initialize momentums