def fit(self, X, y=None): """Fit the model to the data X. Parameters ---------- X : {array-like, sparse matrix} shape (n_samples, n_features) Training data. Returns ------- self : BernoulliRBM The fitted model. """ self.h_samples_ *= 0 begin = time.time() for self.current_epoch in xrange(self.n_iter): for batch_slice in generate_slices(X.shape[0], self.batch_size): self.partial_fit(X[batch_slice]) if self.verbose: end = time.time() H = self.transform(X) R = self._mean_visibles(H) d = np.sqrt((op.sum((R-X)**2))/X.shape[0]) print("[%s] Iteration %d, ReconstructionRMSE %.4f time = %.2fs" % (type(self).__name__, self.current_epoch, d, end - begin)) begin = end return self
def fit(self, X, y=None): """Fit the model to the data X. Parameters ---------- X : {array-like, sparse matrix} shape (n_samples, n_features) Training data. Returns ------- self : BernoulliRBM The fitted model. """ self.h_samples_ *= 0 begin = time.time() for self.current_epoch in xrange(self.n_iter): for batch_slice in generate_slices(X.shape[0], self.batch_size): self.partial_fit(X[batch_slice]) if self.verbose: end = time.time() H = self.transform(X) R = self._mean_visibles(H) d = np.sqrt((op.sum((R - X)**2)) / X.shape[0]) print( "[%s] Iteration %d, ReconstructionRMSE %.4f time = %.2fs" % (type(self).__name__, self.current_epoch, d, end - begin)) begin = end return self
def transform(self, X): '''Transforms the input X into predictions. Note: this essentially runs the forward pass, but without using dropout. ''' # We run in batch mode so we're sure not to use more memory than # the training forward passes. use_gpu = isinstance(self.layers[-1].W, op.gpuarray.GPUArray) out = op.empty((X.shape[0], self.layers[-1].size), dtype=self.dtype, use_gpu=use_gpu) self._disable_dropout() try: for s in generate_slices(X.shape[0], self.batch_size): a = X[s] if sparse.isspmatrix_csr(X) and use_gpu: alloc = op.cuda_memory_pool.allocate a = op.GPUCSRArray(a, allocator=alloc, stream=op.streams[0]) a = a.todense(allocator=alloc, stream=op.streams[0]) out[s] = self.forward_pass(a) finally: self._enable_dropout() return out
def partial_fit(self, X, y, encode_labels=True): ''' Runs one epoch of minibatch-backprop on the given data. Note: Input-Dropout might overwrite parts of X! Expects y in One-Hot format''' if not sparse.isspmatrix_csr(X): assert (X.flags.c_contiguous) cur_lr, cur_momentum = self._get_current_learningrate( self.current_epoch) err = 0.0 nbatches = 0 for s in generate_slices(X.shape[0], self.batch_size, \ self.ignore_last_minibatch_if_smaller): Xtemp = X[s] ytemp = y[s] # for sparse matrices, the fastest option is to convert to # dense on the GPU and then operate in dense if sparse.isspmatrix_csr(X) and isinstance(self.layers[0].W, op.gpuarray.GPUArray): a = op.cuda_memory_pool.allocate #Xtemp = op.to_gpu(Xtemp.A, stream=op.streams[0]) #ytemp = op.to_gpu(ytemp, stream=op.streams[1]) Xtemp = op.GPUCSRArray(Xtemp, allocator=a, stream=op.streams[0]) #Xtemp = Xtemp.todense(allocator=a, stream=op.streams[0]) if sparse.isspmatrix_csr(ytemp): ytemp = op.to_gpu(ytemp.toarray(), stream=op.streams[1]) else: ytemp = op.to_gpu(ytemp, stream=op.streams[1]) out = self.forward_pass(Xtemp) op.streams[1].synchronize() self.backward_pass(out, ytemp, cur_momentum) op.streams[2].synchronize() for i, l in enumerate(self.layers): l.update(cur_lr[i], stream=op.streams[i % len(op.streams)]) self.update_count += 1 batch_error = self._get_loss(ytemp, out) err += batch_error nbatches += 1 for cb in self._minibatch_callbacks: cb(self, batch_error, Xtemp, ytemp) self.current_epoch += 1 return err / nbatches
def partial_fit(self, X, y, encode_labels=True): ''' Runs one epoch of minibatch-backprop on the given data. Note: Input-Dropout might overwrite parts of X! Expects y in One-Hot format''' if not sparse.isspmatrix_csr(X): assert(X.flags.c_contiguous) cur_lr, cur_momentum = self._get_current_learningrate(self.current_epoch) err = 0.0 nbatches = 0 for s in generate_slices(X.shape[0], self.batch_size, \ self.ignore_last_minibatch_if_smaller): Xtemp = X[s] ytemp = y[s] # for sparse matrices, the fastest option is to convert to # dense on the GPU and then operate in dense if sparse.isspmatrix_csr(X) and isinstance(self.layers[0].W, op.gpuarray.GPUArray): a = op.cuda_memory_pool.allocate #Xtemp = op.to_gpu(Xtemp.A, stream=op.streams[0]) #ytemp = op.to_gpu(ytemp, stream=op.streams[1]) Xtemp = op.GPUCSRArray(Xtemp, allocator=a, stream=op.streams[0]) #Xtemp = Xtemp.todense(allocator=a, stream=op.streams[0]) if sparse.isspmatrix_csr(ytemp): ytemp = op.to_gpu(ytemp.toarray(), stream=op.streams[1]) else: ytemp = op.to_gpu(ytemp, stream=op.streams[1]) out = self.forward_pass(Xtemp) op.streams[1].synchronize() self.backward_pass(out, ytemp, cur_momentum) op.streams[2].synchronize() for i, l in enumerate(self.layers): l.update(cur_lr[i], stream=op.streams[i % len(op.streams)]) self.update_count += 1 batch_error = self._get_loss(ytemp, out) err += batch_error nbatches += 1 for cb in self._minibatch_callbacks: cb(self, batch_error, Xtemp, ytemp) self.current_epoch += 1 return err / nbatches
def transform(self, X): '''Transforms the input X into predictions. Note: this essentially runs the forward pass, but without using dropout. ''' # We run in batch mode so we're sure not to use more memory than # the training forward passes. out = op.empty((X.shape[0], self.layers[-1].size), dtype=self.dtype, use_gpu=type(X) == op.gpuarray.GPUArray) for s in generate_slices(X.shape[0], self.batch_size): a = X[s] for i, l in enumerate(self.layers): odr = 0.0 if l.dropout > 0: #if i > 0: # dont scale in the input layer l.W *= (1.0 - l.dropout) odr, l.dropout = l.dropout, 0.0 a = l.fprop(a, stream=op.streams[0]) if odr > 0: l.dropout = odr #if i > 0: l.W /= (1.0 - l.dropout) out[s] = a return out