def main(): # Set parameters and print them args = parse_args() print_args(args) epochs = args['epochs'] visibleUnits = args['visibleUnits'] hiddenUnits = args['hiddenUnits'] approxMethod = args['approxMethod'] approxSteps = args['approxSteps'] learnRate = args['learnRate'] persistStart = args['persistStart'] momentum = args['momentum'] decayMagnitude = args['decayMagnitude'] decayType = args['decayType'] sigma = args['sigma'] batchSize = args['batchSize'] binarization = args['binarization'] update = args['update'] trace_file = args['trace_file'] # saving results save_file = args['save_file'] # saving parameters load_file = args['load_file'] # loading parameters print('Loading data') with gzip.open('../data/mnist.pkl.gz', 'r') as f: (TrainSet, y_train), (x_test, y_test), (ValidSet, y_Valid) = pickle.load(f, encoding='latin1') # combine train and valid data TrainSet = np.concatenate((TrainSet, x_test), axis=0) # Create binary data TrainSet = sklearn.preprocessing.binarize(TrainSet, threshold=binarization).T ValidSet = sklearn.preprocessing.binarize(ValidSet, threshold=binarization).T if len(load_file) == 0: print('Initializing model') model = RBM.RBM(n_vis=visibleUnits, n_hid=hiddenUnits, momentum = momentum, sigma = sigma, trainData = TrainSet, wiseStart = True, ) print('Start of training') Training.fit(model, TrainSet, ValidSet, n_epochs = epochs, weight_decay = decayType, decay_magnitude = decayMagnitude, lr = learnRate, batch_size = batchSize, NormalizationApproxIter = approxSteps, approx = approxMethod, update = update, persistent_start = persistStart, trace_file = trace_file, save_file = save_file ) else: params = RBM.RBM.load(load_file) model = RBM.RBM(params=params) model.reconstructionArray(ValidSet[:, 0:10])
def __init__(self, input_data=None, label=None, input_size=2, hidden_layer_sizes=[3, 3], output_size=1, batch_size=10, learning_rate=0.1, epochs=50, C=1, rng=None): self.input = input_data.astype(np.float32) self.y = label.astype(np.float32) self.rbm_layers = [] self.n_hidden_layers = len( hidden_layer_sizes) # nombre de couches cachées du DBN self.batch_size = batch_size self.layer_svm = None self.model_finetune = None self.nepochs = epochs self.learning_rate = learning_rate self.C = C self.rng = rng #W,hbias,vbias = self.init_weights(input_size,hidden_layer_sizes[0]) # construct rbm_layer rbm_layer = RBM(input=self.input, n_visible=input_size, n_hidden=hidden_layer_sizes[0], batch_size=10, learning_rate=0.1, epochs=1000, rng=self.rng) self.rbm_layers.append(rbm_layer) sample_input = self.input for rbm_index in range(self.n_hidden_layers - 1): _, sample_input = self.rbm_layers[rbm_index].sample_h_given_v( sample_input) n_hid = hidden_layer_sizes[rbm_index + 1] n_vis = hidden_layer_sizes[rbm_index] #W,hbias,vbias = self.init_weights(n_vis,n_hid) rbm_i = RBM(input=sample_input, n_visible=n_vis, n_hidden=n_hid, batch_size=10, learning_rate=0.09, epochs=100, rng=self.rng) self.rbm_layers.append(rbm_i)
def __init__(self, architecture): self.stack = [] previous_layer_size = architecture[0] if len(architecture) > 1: for layer_size in architecture[1:]: self.stack.append(RBM(previous_layer_size, layer_size)) previous_layer_size = layer_size
def __init__(self, title, layerSizes, types, modelDir): #checking DBN configuration if not len(types) == len(layerSizes) - 1: print 'DBN initialize error: layerSizes types length mismatch' exit() for i in range(len(types) - 1): if not (types[i] in ['BB', 'GB', 'BG'] and types[i + 1] in ['BB', 'GB', 'BG']): print "DBN initialize error: RBM types error, RBM types must be ['BB','GB','BG']" exit() if not types[i][1] == types[i + 1][0]: print 'DBN initialize error: RBM types sequence error' exit() print '\nInitializing DBN: %s, layerSizes: %s, layerTypes: %s' % ( title, layerSizes, types) self.title = title self.layerSizes = layerSizes self.layerNum = len(self.layerSizes) - 1 self.modelDir = modelDir self.rbms = [] self.rbmFiles = [] for i in range(self.layerNum): self.rbms.append( RBM(input=None, n_visible=self.layerSizes[i], n_hidden=self.layerSizes[i + 1], type=types[i])) self.rbmFiles.append(modelDir + os.sep + '%s_layer%dRBM_%d-%d.rbm' % (self.title, i + 1, self.layerSizes[i], self.layerSizes[i + 1])) '''
def get_RBM(): r = RBM(6, 2) k = 9 dataset = [[1, 1, 1, 0, 0, 0], [1, 0, 1, 0, 0, 0], [1, 1, 1, 0, 0, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0], [0, 0, 1, 1, 1, 0], [0, 1, 0, 0, 0, 1], [0, 1, 0, 0, 0, 1], [0, 1, 1, 0, 0, 1]] # Train at the following learning rates: print "Training..." learning_rates = [0.1, 0.05, 0.01, 0.005, 0.001, 0.0005, 0.0001] # learning_rates = [0.1] for rate in learning_rates: print "Learning rate =", rate for i in range(100): print ' ' + str(i) + '...', r.train_epoch(dataset, rate, k) err = np.sum([r.free_energy(data) for data in dataset]) print 'Energy = ' + str(err) + '...', PL = r.pseudolikelihood(dataset) print 'Pseudolikelihood = ' + str(PL) + '...', L = r.likelihood(dataset) print 'Likelihood = ' + str(L) + '...', print 'Done' return r, dataset
def pretrain(self, x, epochs, num_samples=50000): ''' Greedy layer-wise training The last layer is a RBM with linear hidden units shape(x) = (v_dim, number_of_examples) ''' RBM_layers = [] for i in range(self.num_hidden_layers): # initialize RBM's if (i < self.num_hidden_layers - 1): RBM_layers.append( RBM(self.layer_dims[i], self.layer_dims[i + 1])) else: RBM_layers.append( RBM_with_linear_hidden_units(self.layer_dims[i], self.layer_dims[i + 1])) for i in range(self.num_hidden_layers): # train RBM's print("Training RBM layer %i" % (i + 1)) RBM_layers[i].train(x, epochs) # train the ith RBM if not (i == self.num_hidden_layers - 1): # generate samples to train next layer _, x = RBM_layers[i].gibbs_sampling(2, num_samples) self.W.append(RBM_layers[i].W) # save trained weights self.b.append(RBM_layers[i].b) self.a.append(RBM_layers[i].a) self.pretrained = True return
def train_rbm(nv, nh, batch_size, epochs, val_set, gibbs_sampling_nb, rbm=None): if (rbm == None): rbm = RBM(nv, nh) for epoch in range(1, epochs + 1): training_loss = 0 s = 0.0 for user in range(0, nb_users - batch_size, batch_size): vk = val_set[user:user + batch_size] v0 = val_set[user:user + batch_size] for sample in range(gibbs_sampling_nb): _, hk = rbm.sample_h(vk) _, vk = rbm.sample_v(hk) vk[v0 < 0] = v0[v0 < 0] phk, _ = rbm.sample_h(vk) ph0, _ = rbm.sample_h(v0) rbm.train(v0, vk, ph0, phk) training_loss += torch.mean(torch.abs(v0[v0 >= 0] - vk[v0 >= 0])) s += 1 # print("Epoch:", epoch, "Training Loss:", training_loss/s) return float(training_loss / s), rbm
def build_rbm(input_var=None,d_x=21,d_y=21): l_in = lasagne.layers.InputLayer(shape=(None, 1, d_x, d_y), input_var=input_var) l_in_drop = lasagne.layers.DropoutLayer(l_in, p=0.2) l_hid = RBM.RBM(l_in_drop,num_units=128,kGibbs=1) return l_hid
def initRBMs(self): self.RBMList = [] for i in range(self.layers): currVisDim = self.dimensionList[i] currHidDim = self.dimensionList[i + 1] currWR = self.trainInfoList[i][0] groupWeights = False currRBM = RBM(currVisDim, currHidDim, currWR, groupWeights) self.RBMList.append(currRBM)
def train_and_sample(T, lr): temp_name = 'T = ' + format(T, '.2f') + '.npy' file_name = 'T = ' + format(T, '.2f') + ', lr = ' + str(lr) + '.npy' completeLoad = os.path.join(load_path, temp_name) samples = (np.load(completeLoad) + 1) / 2 # convert to 0, 1 sz, N, N1 = samples.shape samples_flat = np.reshape(samples, (sz, N * N1)) r = RBM(num_visible=64, num_hidden=nH) r.train(samples_flat, max_epochs=me, learning_rate=lr, batch_size=bs) print("Wights at T = " + format(T, '.2f') + ": ", r.weights) RBM_data_flat = r.daydream(ns) * 2 - 1 # convert back to -1, 1 RBM_data = np.reshape(RBM_data_flat, (ns, N, N1)) completeSaveData = os.path.join(save_data_path, file_name) np.save(completeSaveData, RBM_data) completeSaveWeight = os.path.join(save_weight_path, file_name) np.save(completeSaveWeight, r.weights) completeSaveError = os.path.join(save_error_path, file_name) np.save(completeSaveError, r.errors)
def fit(self, trainset): AlgoBase.fit(self, trainset) numUsers = trainset.n_users numItems = trainset.n_items trainingMatrix = np.zeros([numUsers, numItems, 10], dtype=np.float32) for (uid, iid, rating) in trainset.all_ratings(): adjustedRating = int(float(rating) * 2.0) - 1 trainingMatrix[int(uid), int(iid), adjustedRating] = 1 # Flatten to a 2D array, with nodes for each possible rating type on each possible item, for every user. trainingMatrix = np.reshape(trainingMatrix, [trainingMatrix.shape[0], -1]) # Create an RBM with (num items * rating values) visible nodes rbm = RBM.RBM(trainingMatrix.shape[1], hiddenDimensions=self.hiddenDim, learningRate=self.learningRate, batchSize=self.batchSize, epochs=self.epochs) rbm.Train(trainingMatrix) self.predictedRatings = np.zeros([numUsers, numItems], dtype=np.float32) for uiid in range(trainset.n_users): if (uiid % 50 == 0): print("Processing user ", uiid) recs = rbm.GetRecommendations([trainingMatrix[uiid]]) recs = np.reshape(recs, [numItems, 10]) for itemID, rec in enumerate(recs): # The obvious thing would be to just take the rating with the highest score: #rating = rec.argmax() # ... but this just leads to a huge multi-way tie for 5-star predictions. # The paper suggests performing normalization over K values to get probabilities # and take the expectation as your prediction, so we'll do that instead: normalized = self.softmax(rec) rating = np.average(np.arange(10), weights=normalized) self.predictedRatings[uiid, itemID] = (rating + 1) * 0.5 return self
def gen_sample(self, k, x=None): with tf.variable_scope('dbn_gen_sample'): # Get input value for the top rbm's visible layer if x is None: x = tf.zeros([1, int(self.bs[-2].shape[-1])]) else: # Propagate input value up to visible layer of top rbm for i in range(len(self.ws) - 1): x_prob = tf.sigmoid(tf.matmul(x, self.ws[i]) + self.bs[i+1]) x = RBM.sample(x_prob) # Sample from the top layer top_rbm = RBM.RBM(self.ws[-1], self.bs[-2], self.bs[-1]) _, x_sample = top_rbm.gibbs_sample(x, k) # Propagate sample down to visible layer for i in reversed(range(len(self.ws) - 1)): x_prob = tf.sigmoid(tf.matmul(x_sample, tf.transpose(self.ws[i])) + self.bs[i]) x_sample = RBM.sample(x_prob) return x_sample
def pretrain(self, x, epochs, num_samples=50000): ''' Greedy layer-wise training The last layer is a RBM with linear hidden units shape(x) = (v_dim, number_of_examples) ''' RBM_layers = [] # initialize RBMs for i in range(self.num_hidden_layers): if i == 0: RBM_layers.append( RBM_with_linear_visible_units( self.layer_dims[i], self.layer_dims[i + 1])) # linear input elif (i < self.num_hidden_layers - 1): RBM_layers.append( RBM(self.layer_dims[i], self.layer_dims[i + 1])) # else: RBM_layers.append( RBM_with_linear_hidden_units( self.layer_dims[i], self.layer_dims[i + 1])) # linear output # train stack of RBMs for i in range(self.num_hidden_layers): # train RBM's print("Training RBM layer %i" % (i + 1)) x = RBM_layers[i].train(x, epochs) # train the ith RBM self.W.append(RBM_layers[i].W) # save trained weights self.b.append(RBM_layers[i].b) self.a.append(RBM_layers[i].a) self.pretrained = True return
def get_RBM(): print "Loading digits...." digits, classes = load_digits('digits_train.txt') digits = [digit_gray_to_binary(d) for d in digits] r = RBM(196,25) # Train at the following learning rates: print "Training..." learning_rates = [0.1, 0.05] #, 0.01, 0.005, 0.001] for rate in learning_rates: print "Learning rate =", rate for i in range(20): print ' ' + str(i) + '...', r.train_epoch(digits, rate, 5) err = np.sum([r.free_energy(digit) for digit in digits]) print 'Energy = ' + str(err) + '...', PL = r.pseudolikelihood(digits) print 'Pseudolikelihood = ' + str(PL) + '...', print 'Done' return r, digits
from RBM import * mnist = input_data.read_data_sets('../MNIST_data', one_hot=True) batch_size = 10 batch = mnist.train.next_batch(batch_size) rbm_input = np.concatenate((to_binary_2D(batch[0]), batch[1]), axis = 1) print(rbm_input.shape) r = RBM(data_num = batch_size, m = 28*28+10, n = 20, k = 100) r.print_tensors()
def train_and_plot(nH, T, lr, me, steps, bs, gs): T_pos = np.where(T_range == T)[0][0] epoch_int = int(me / steps) epoch_list = [] load_path = os.path.join('Data', 'Training Data') E_training = np.load(os.path.join(load_path, 'Observables', 'E_vals.npy'))[T_pos] M_training = np.load(os.path.join(load_path, 'Observables', 'M_vals.npy'))[T_pos] Cv_training = np.load(os.path.join(load_path, 'Observables', 'Cv_vals.npy'))[T_pos] X_training = np.load(os.path.join(load_path, 'Observables', 'X_vals.npy'))[T_pos] file_name = 'T = ' + format(T, '.2f') + '.npy' completeLoad = os.path.join(load_path, file_name) samples = (np.load(completeLoad) + 1) / 2 # convert to 0, 1 sz, N, N1 = samples.shape samples_flat = np.reshape(samples, (sz, N * N1)) # lr = lr_nH_64['T = ' + format(T, '.2f')] r = RBM(num_visible=64, num_hidden=nH) Engs = [] # energy Mags = [] # magnetisation SH_Cv = [] # specific heat MS_X = [] # magnetic susceptibility for step in range(steps): epoch_list.append((step + 1) * epoch_int) r.train(training_vis=samples_flat, max_epochs=epoch_int, learning_rate=lr, batch_size=bs, gibbs_steps=gs) RBM_data_flat = r.daydream( num_samples=ns, gibbs_steps=1) * 2 - 1 # convert back to -1, 1 RBM_data = np.reshape(RBM_data_flat, (ns, N, N1)) Engs.append(E(RBM_data)) Mags.append(M(RBM_data)) SH_Cv.append(Cv(RBM_data, T)) MS_X.append(X(RBM_data, T)) [E_vals, E_errs] = np.transpose(Engs) [M_vals, M_errs] = np.transpose(Mags) [Cv_vals, Cv_errs] = np.transpose(SH_Cv) [X_vals, X_errs] = np.transpose(MS_X) fig1, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2, figsize=(8, 7)) fig1.suptitle('Thermal Observables \n \n nH = ' + str(nH) + ', lr = ' + str(lr) + ', bs = ' + str(bs) + ', T = ' + format(T, '.2f') + ', gs = ' + str(gs), fontweight='bold') ax1.axhline(E_training) ax1.errorbar(epoch_list, E_vals, yerr=E_errs, marker='o', ls='-', lw=1.4, capsize=2, ecolor='C7', elinewidth=1.5, label='Energy') ax1.set_title('Energy', fontdict=myfont_m) ax1.set_xlabel('Steps', fontdict=myfont_s) ax1.set_ylabel('E', fontdict=myfont_s) ax2.errorbar(epoch_list, M_vals, yerr=M_errs, marker='o', ls='-', lw=1.4, capsize=2, ecolor='C7', elinewidth=1.5, label='Magnetisation') ax2.axhline(M_training) ax2.set_title('Magnetisation', fontdict=myfont_m) ax2.set_xlabel('Steps', fontdict=myfont_s) ax2.set_ylabel('M', fontdict=myfont_s) ax3.errorbar(epoch_list, Cv_vals, yerr=Cv_errs, marker='o', ls='-', lw=1.4, capsize=2, ecolor='C7', elinewidth=1.5, label='Specific Heat') ax3.axhline(Cv_training) ax3.set_title('Specific Heat', fontdict=myfont_m) ax3.set_xlabel('Steps', fontdict=myfont_s) ax3.set_ylabel('Cv', fontdict=myfont_s) ax4.errorbar(epoch_list, X_vals, yerr=X_errs, marker='o', ls='-', lw=1.4, capsize=2, ecolor='C7', elinewidth=1.5, label='Magetic Susceptibility') ax4.axhline(X_training) ax4.set_title('Magetic Susceptibility', fontdict=myfont_m) ax4.set_xlabel('Steps', fontdict=myfont_s) ax4.set_ylabel('X', fontdict=myfont_s) plt.tight_layout(pad=1.5) figname = os.path.join( 'Plots', 'RBM Training', 'nH = ' + str(nH), 'lr = ' + str(lr) + ', bs = ' + str(bs) + ', T = ' + format(T, '.2f') + ', gs = ' + str(gs) + '.jpg') if os.path.isfile(figname): os.remove(figname) fig1.savefig(figname, dpi=600) fig1.show() fig2, ax5 = plt.subplots() ax5.plot(r.errors) fig2.show() winsound.Beep(440, 1000)
def get_RBM(): print "Loading digits...." digits, classes = load_digits('digits_train.txt') r = RBM(196,50)
res_dir = "RBMresults/" prefix = "RBM_h100_100epoch_seed" + str(seed) + "_k" + str(k) + "_" W = np.loadtxt(res_dir + prefix + "W.csv", delimiter=",") bias_input = np.loadtxt(res_dir + prefix + "bias_input.csv", delimiter=",") bias_hidden = np.loadtxt(res_dir + prefix + "bias_hidden.csv", delimiter=",") #################################### ## sample image #################################### ## An RBM with trained parameters (dim_hidden, dim_feature) = W.shape RBMsample = RBM(dim_feature, dim_hidden, W=W, bias_input=bias_input[:, np.newaxis], bias_hidden=bias_hidden[:, np.newaxis]) ## Generate gibbs chains starting from randomly initialized inputs num_image = 100 k_sample = 1000 sample_images = np.zeros((num_image, dim_feature)) for n in xrange(num_image): ## gibbs sampling (h, x) = RBMsample.gibbs_chain(k_CD=k_sample) sample_images[n, :] = x.flatten() ############################ ## save results ############################
dataset = "../data/um/all.dta" def onehot(rating): """ Creates one-vector for a certain rating """ vec = [0 for i in range(5)] vec[rating - 1] = 1 return vec if __name__ == '__main__': print("Creating RBM...") rbm = RBM(M, latent_factors, num_ratings, learning_rate) print("RBM created.") print("Beginning training...") input_data = np.zeros((batch_size, M, num_ratings)) for d in range(full_iters): print("Starting iteration {0}/{1}".format(str(d + 1), str(full_iters))) # Training phase - train with 10 users at a time # TODO - edit RBM to accept numpy data, in general suck less with open(dataset) as f: max_index = batch_size overflow_line = "" while (max_index < U + batch_size): print(" At user {0}/{1}".format(str(min(max_index, U)), str(U)))
def sample_rbm(rbm, test_set_x, n_chains=1, n_samples=100, n_step=1000, percentage_noise=5, n_repeat=1): rng = numpy.random.RandomState() if os.path.isdir('rbm_plots'): os.chdir('rbm_plots') if rbm == None: x = T.matrix('x') theano_rng = RandomStreams(rng.randint(2**30)) n_visable, n_hidden, W, hbias, vbias = get_data('tmp') rbm = RBM(input=x, n_visible=n_visable, n_hidden=n_hidden, W=W, hbias=hbias, vbias=vbias, numpy_rng=rng, theano_rng=theano_rng) # find out the number of test samples number_of_test_samples = test_set_x.get_value(borrow=True).shape[0] # pick random test examples, with which to initialize the persistent chain test_idx = rng.randint(number_of_test_samples - n_chains) persistent_vis_chain = theano.shared( numpy.asarray(minipulate_and_save_image( test_set_x.get_value(borrow=True), test_idx, n_chains, percentage_noise, n_repeat), dtype=theano.config.floatX)) # define one step of Gibbs sampling (mf = mean-field) define a # function that does `plot_every` steps before returning the # sample for plotting ([presig_hids, hid_mfs, hid_samples, presig_vis, vis_mfs, vis_samples], updates) = theano.scan( rbm.gibbs_vhv, outputs_info=[None, None, None, None, None, persistent_vis_chain], n_steps=n_step) # add to updates the shared variable that takes care of our persistent # chain :. updates.update({persistent_vis_chain: vis_samples[-1]}) # construct the function that implements our persistent chain. # we generate the "mean field" activations for plotting and the actual # samples for reinitializing the state of our persistent chain sample_fn = theano.function([], [vis_mfs[-1], vis_samples[-1]], updates=updates, name='sample_fn') # create a space to store the image for plotting ( we need to leave # room for the tile_spacing as well) image_data = numpy.zeros( (29 * n_samples + 1, 29 * n_chains * n_repeat - 1), dtype='uint8') for idx in xrange(n_samples): # generate `plot_every` intermediate samples that we discard, # because successive samples in the chain are too correlated vis_mf, vis_sample = sample_fn() if idx * n_step < 25: print("normalizing") data = persistent_vis_chain.get_value() normal = test_normalize(data) data[len(data) - 1] = test_set_x.get_value( borrow=True)[rng.randint(number_of_test_samples - n_chains)] persistent_vis_chain = theano.shared( numpy.asarray(data, dtype=theano.config.floatX)) print(' ... plotting sample ', idx) image_data[29 * idx:29 * idx + 28, :] = tile_raster_images( X=vis_mf, img_shape=(28, 28), tile_shape=(1, n_chains * n_repeat), tile_spacing=(1, 1)) # construct image image = Image.fromarray(image_data) image.save('samples.png')
if __name__ == '__main__': songs = get_songs('../Jazz_Music_Midi') print "{} songs processed".format(len(songs)) ### HyperParameters lowest_note = 24 highest_note = 102 note_range = highest_note - lowest_note num_timesteps = 5 # Size of input layer input_len = 2 * note_range * num_timesteps model = RBM(n_epochs=200) X = [] for song in songs: song = np.array(song) # Round down to nearest multiple song = song[:int( np.floor((song.shape[0] / num_timesteps) * num_timesteps))] # Reshape into blocks of num_timesteps song = np.reshape( song, [song.shape[0] / num_timesteps, song.shape[1] * num_timesteps]) X.extend(song) X = np.array(X) model.fit(X)
## parameters dim_hidden = int(sys.argv[1]) ## hidden layer size k_CD = int(sys.argv[2]) ## steps for contrastive divergence dim_feature = 784 ## input feature dimension max_epoch = 100 rate = 0.01 ## learning rate in gradient descent seed = 77 ## seed for initialize weights ## load data and convert to binary (trainLabel, trainData) = get_data("data/digitstrain.txt", dim_feature=dim_feature) (valLabel, valData) = get_data("data/digitsvalid.txt", dim_feature=dim_feature) ## RBM RBMtrain = RBM(dim_feature, dim_hidden, k_CD=k_CD, k_eval=1, seed=seed) (train_loss, val_loss) = RBMtrain.train(trainData=trainData, valData=valData, max_epoch=max_epoch, rate=rate) ## save results save_dir = "RBMresults/" prefix = "RBM" + "_h" + str(dim_hidden) + "_" + str(max_epoch) + \ "epoch_seed" + str(seed) + "_k" + str(k_CD) + "_" if not os.path.exists(save_dir): os.mkdir(save_dir) save_model(RBMtrain, save_dir=save_dir, prefix=prefix) np.savetxt(save_dir + prefix + "loss.csv",
# environment = LogicGate(np.array([[0], [1], [1], [0]])) # environment = LogicGate(np.array([[0], [1], [1], [0], [1], [0], [0], [0]])) environment = LogicGate(np.array([[1], [0], [0], [1], [0], [0], [1], [0]])) # ~~~ Create the network ~~~ network_params = { # Shape of network 'input_nodes': environment.size_input(), 'output_nodes': environment.size_output(), "basis": basis_logistic, # Weight initialization distribution "distribute": dist_normal } network = RBM(**network_params) # ~~~ Train the network ~~~ optimizer_params = { "batch_size": 1, # Learning rate "learn_step": .001, "learn_anneal": anneal_fixed, "epsilon": 0.04, # error allowance "iteration_limit": None, # limit on number of iterations to run "debug": True, "graph": True } ContrastiveDivergence(network, environment, **optimizer_params).minimize()