def score(self, X, y): """ Check the prediction error for the specified input samples and their targets. Parameters ---------- X : array-like y : array-like Returns ------- float Prediction error. """ X = self.format_input(X) y = self.format_target(y) return iters.apply_batches( function=self.functions.score, inputs=as_tuple(X, y), batch_size=self.batch_size, show_output=True, show_progressbar=self.logs.enable, average_outputs=True, )
def test_apply_batches_with_progressbar(self): # So far we just make sure that test didn't trigger any error # In the future, we need to check content of the terminal output outputs = iters.apply_batches( function=lambda x: x * 2, inputs=np.arange(20), batch_size=8, show_progressbar=True, ) self.assertEqual(len(outputs), 3)
def test_apply_batches(self): def mse(y_actual, y_predicted): return np.mean((y_actual - y_predicted) ** 2) y_actual = np.arange(20) y_predicted = np.ones(20) * 10 outputs = iters.apply_batches( function=mse, inputs=[y_actual, y_predicted], batch_size=7, ) np.testing.assert_array_almost_equal( outputs, np.array([53, 4, 45.1666666])) avg_loss = iters.apply_batches( function=mse, inputs=[y_actual, y_predicted], batch_size=7, average_outputs=True, ) self.assertEqual(avg_loss, mse(y_actual, y_predicted))
def predict(self, X): """ Predicts class from the input data. Parameters ---------- X : array-like (n_samples, n_features) Returns ------- array-like (n_samples,) """ outputs = iters.apply_batches( function=self.predict_raw, inputs=format_data(X), batch_size=self.batch_size, show_progressbar=self.logs.enable, ) raw_output = np.concatenate(outputs, axis=1) return self.classes[raw_output.argmax(axis=0)]
def predict(self, *inputs, **kwargs): session = tf_utils.tensorflow_session() batch_size = kwargs.pop('batch_size', None) verbose = kwargs.pop('verbose', True) # We require do to this check for python 2 compatibility if kwargs: raise TypeError("Unknown arguments: {}".format(kwargs)) def single_batch_predict(*inputs): feed_dict = dict(zip(as_tuple(self.inputs), inputs)) return session.run(self.outputs, feed_dict=feed_dict) outputs = iters.apply_batches( function=single_batch_predict, inputs=inputs, batch_size=batch_size, show_progressbar=verbose, ) return np.concatenate(outputs, axis=0)
def predict_proba(self, X): """ Predict probabilities for each class. Parameters ---------- X : array-like (n_samples, n_features) Returns ------- array-like (n_samples, n_classes) """ outputs = iters.apply_batches( function=self.predict_raw, inputs=format_data(X), batch_size=self.batch_size, show_progressbar=self.logs.enable, ) raw_output = np.concatenate(outputs, axis=1) total_output_sum = raw_output.sum(axis=0).reshape((-1, 1)) return raw_output.T / total_output_sum
def score(self, X, y=None): """ Compute the pseudo-likelihood of input samples. Parameters ---------- X : array-like Values of the visible layer Returns ------- float Value of the pseudo-likelihood. """ return iters.apply_batches( function=self.score_func, inputs=format_data(X, is_feature1d=(self.n_visible == 1)), batch_size=self.batch_size, show_output=True, show_progressbar=self.logs.enable, average_outputs=True, )
def hidden_to_visible(self, hidden_input): """ Propagates output from the hidden layer backward to the visible. Parameters ---------- hidden_input : array-like (n_samples, n_hidden_features) Returns ------- array-like """ is_input_feature1d = (self.n_hidden == 1) hidden_input = format_data(hidden_input, is_input_feature1d) outputs = iters.apply_batches( function=self.hidden_to_visible_one_step, inputs=hidden_input, batch_size=self.batch_size, show_progressbar=self.logs.enable, ) return np.concatenate(outputs, axis=0)
def visible_to_hidden(self, visible_input): """ Propagates data through the network and returns output from the hidden layer. Parameters ---------- visible_input : array-like (n_samples, n_visible_features) Returns ------- array-like """ is_input_feature1d = (self.n_visible == 1) visible_input = format_data(visible_input, is_input_feature1d) outputs = iters.apply_batches( function=self.visible_to_hidden_one_step, inputs=visible_input, batch_size=self.batch_size, show_progressbar=self.logs.enable, ) return np.concatenate(outputs, axis=0)