def pre_train(self, train_round, size, rate, dr, scale): #for each pair of neighbor layers, training RBM for k in range(self.depth - 1): rbm = RBM(self.layers[k], self.layers[k+1], self.weightLayers[k], rate) # mini-batch training times for tr in range(train_round): #mini-batch size for i in range(size): index = random.randint(0, scale) im = dr.readOne(index) for p in range(len(im)): if im[p] >= 128: self.layers[0].outs[p] = 1.0 else: self.layers[0].outs[p] = 0.0 for j in range(k): toHiddenLayer(self.layers[j], self.weightLayers[j], self.layers[j+1]) #self.layers[j+1].outs = sample(self.layers[j+1].outs) sample(self.layers[j+1].outs) rbm.training(1) rbm.update_Parameters(size) #if tr%100 == 0: #print tr print "Layer: ", k+1, " Completed!" return
def result(self, im, label): self.layers[0].outs = im.copy() self.layers[0].nets = im.copy() for i in range(self.depth-2): toHiddenLayer(self.layers[i], self.weightLayers[i], self.layers[i+1]) toOutputLayer(self.layers[self.depth-2], self.weightLayers[self.depth-2], self.layers[self.depth-1]) return self.layers[self.depth-1].outs[label]
def result_print(self, im, label): self.layers[0].outs = im.copy() self.layers[0].nets = im.copy() for i in range(self.depth-2): toHiddenLayer(self.layers[i], self.weightLayers[i], self.layers[i+1]) toOutputLayer(self.layers[self.depth-2], self.weightLayers[self.depth-2], self.layers[self.depth-1]) r = findMax(self.layers[self.depth-1]) if label == r : return 1 else: ''' for i in range(10): print i, " ", self.layers[self.depth-1].outs[i] print label print "------------------------------------------------------------------------------" ''' return 0