Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from shengshuyang/master
create a SaveLogger.save() method.
- Loading branch information
Showing
2 changed files
with
57 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import numpy as np | ||
from tempfile import mkstemp | ||
|
||
from sklearn.datasets import load_iris | ||
from sklearn.cross_validation import train_test_split | ||
|
||
from pystruct.models import GraphCRF | ||
from pystruct.learners import NSlackSSVM | ||
from pystruct.utils import SaveLogger | ||
from pystruct.inference import get_installed | ||
|
||
from nose.tools import assert_less, assert_almost_equal | ||
|
||
# we always try to get the fastest installed inference method | ||
inference_method = get_installed(["qpbo", "ad3", "lp"])[0] | ||
|
||
def test_logging(): | ||
iris = load_iris() | ||
X, y = iris.data, iris.target | ||
|
||
X_ = [(np.atleast_2d(x), np.empty((0, 2), dtype=np.int)) for x in X] | ||
Y = y.reshape(-1, 1) | ||
|
||
X_train, X_test, y_train, y_test = train_test_split(X_, Y, random_state=1) | ||
_, file_name = mkstemp() | ||
|
||
pbl = GraphCRF(n_features=4, n_states=3, inference_method='lp') | ||
logger = SaveLogger(file_name) | ||
svm = NSlackSSVM(pbl, C=100, n_jobs=1, logger=logger) | ||
svm.fit(X_train, y_train) | ||
|
||
score_current = svm.score(X_test, y_test) | ||
score_auto_saved = logger.load().score(X_test, y_test) | ||
|
||
alt_file_name = file_name + "alt" | ||
logger.save(svm, alt_file_name) | ||
logger.file_name = alt_file_name | ||
logger.load() | ||
score_manual_saved = logger.load().score(X_test, y_test) | ||
|
||
assert_less(.97, score_current) | ||
assert_less(.97, score_auto_saved) | ||
assert_less(.97, score_manual_saved) | ||
assert_almost_equal(score_auto_saved, score_manual_saved) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters