def fit(self, dataset, nb_epoch=10, batch_size=50, pad_batches=False, **kwargs): """ Fits a model on data in a Dataset object. """ # TODO(rbharath/enf): We need a structured way to deal with potential GPU # memory overflows. for epoch in range(nb_epoch): log("Starting epoch %s" % str(epoch + 1), self.verbosity) losses = [] for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches(batch_size, pad_batches=pad_batches): if self.fit_transformers: X_batch, y_batch, w_batch = self.transform_on_batch( X_batch, y_batch, w_batch) if pad_batches: X_batch, y_batch, w_batch, ids_batch = pad_batch( batch_size, X_batch, y_batch, w_batch, ids_batch) losses.append(self.fit_on_batch(X_batch, y_batch, w_batch)) log( "Avg loss for epoch %d: %f" % (epoch + 1, np.array(losses).mean()), self.verbosity)
def predict_proba_on_batch(self, support, test_batch): """Make predictions on batch of data.""" n_samples = len(test_batch) X, y, w, ids = pad_batch(self.test_size, test_batch.X, test_batch.y, test_batch.w, test_batch.ids) padded_test_batch = NumpyDataset(X, y, w, ids) feed_dict = self.construct_feed_dict(padded_test_batch, support) # Get scores pred = self.sess.run(self.pred_op, feed_dict=feed_dict) # Remove padded elements pred = np.asarray(pred[:n_samples]) return pred
def predict_on_batch(self, support, test_batch): """Make predictions on batch of data.""" n_samples = len(test_batch) padded_test_batch = NumpyDataset(*pad_batch( self.test_batch_size, test_batch.X, test_batch.y, test_batch.w, test_batch.ids)) feed_dict = self.construct_feed_dict(padded_test_batch, support) # Get scores pred, scores = self.sess.run([self.pred_op, self.scores_op], feed_dict=feed_dict) y_pred_batch = np.round(pred) # Remove padded elements y_pred_batch = y_pred_batch[:n_samples] return y_pred_batch
def predict_on_batch(self, support, test_batch): """Make predictions on batch of data.""" n_samples = len(test_batch) X, y, w, ids = pad_batch(self.test_batch_size, test_batch.X, test_batch.y, test_batch.w, test_batch.ids) padded_test_batch = NumpyDataset(X, y, w, ids) feed_dict = self.construct_feed_dict(padded_test_batch, support) # Get scores pred, scores = self.sess.run([self.pred_op, self.scores_op], feed_dict=feed_dict) y_pred_batch = np.round(pred) # Remove padded elements y_pred_batch = y_pred_batch[:n_samples] return y_pred_batch
def predict_proba_on_batch(self, support, test_batch): """Make predictions on batch of data.""" n_samples = len(test_batch) padded_test_batch = NumpyDataset(*pad_batch( self.test_batch_size, test_batch.X, test_batch.y, test_batch.w, test_batch.ids)) feed_dict = self.construct_feed_dict(padded_test_batch, support) # Get scores pred, scores = self.sess.run([self.pred_op, self.scores_op], feed_dict=feed_dict) # pred corresponds to prob(example == 1) y_pred_batch = np.zeros((n_samples, 2)) # Remove padded elements pred = pred[:n_samples] y_pred_batch[:, 1] = pred y_pred_batch[:, 0] = 1-pred return y_pred_batch
def predict_proba_on_batch(self, support, test_batch): """Make predictions on batch of data.""" n_samples = len(test_batch) X, y, w, ids = pad_batch(self.test_batch_size, test_batch.X, test_batch.y, test_batch.w, test_batch.ids) padded_test_batch = NumpyDataset(X, y, w, ids) feed_dict = self.construct_feed_dict(padded_test_batch, support) # Get scores pred, scores = self.sess.run([self.pred_op, self.scores_op], feed_dict=feed_dict) # pred corresponds to prob(example == 1) y_pred_batch = np.zeros((n_samples, 2)) # Remove padded elements pred = pred[:n_samples] y_pred_batch[:, 1] = pred y_pred_batch[:, 0] = 1 - pred return y_pred_batch
def fit(self, dataset, nb_epoch=10, batch_size=50, pad_batches=False, **kwargs): """ Fits a model on data in a Dataset object. """ # TODO(rbharath/enf): We need a structured way to deal with potential GPU # memory overflows. for epoch in range(nb_epoch): log("Starting epoch %s" % str(epoch+1), self.verbosity) losses = [] for (X_batch, y_batch, w_batch, ids_batch) in dataset.iterbatches( batch_size, pad_batches=pad_batches): if self.fit_transformers: X_batch, y_batch, w_batch = self.transform_on_batch(X_batch, y_batch, w_batch) if pad_batches: X_batch, y_batch, w_batch, ids_batch = pad_batch( batch_size, X_batch, y_batch, w_batch, ids_batch) losses.append(self.fit_on_batch(X_batch, y_batch, w_batch)) log("Avg loss for epoch %d: %f" % (epoch+1,np.array(losses).mean()),self.verbosity)